I am self taught in VBA, which is probably the worst kind, and I still consider myself a novice. There are some things I just can't understand and this site has been a godsend. This code is not my own, so I barely understand what it is doing, which makes it difficult to alter. It's been working for the past two years. The company is now slowly upgrading computers and we now have some running Office 2010 and some running 2013. That means I can no longer use Office Tool references in my code, because it breaks between systems. So long story short, I need to do a late binding on this bit of code and I have no idea how.
I get an error on msoFileDialogFilePicker and msoFileDialogViewList
Please Overflow community, HELP!
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False 'Select only one file
.Title = "Locate file" 'Set dialog title
.ButtonName = "Choose" 'Set the button caption
.Filters.Clear 'Make sure the filter list is clear
.InitialFileName = "\\PUSNBF02\common\Admin\New EA's\" 'Initial file search location
.InitialView = msoFileDialogViewList 'View as list
'Show the dialog and test the return
If .Show = 0 Then 'didn't pick a file - exit sub
Exit Function
Else
'Should be only one file name - grab it
SourceFile = Trim(.SelectedItems(1)) 'Full path of file
strSource = SourceFile
strSourcePath = Left(strSource, InStrRev(strSource, "\"))
strSourceFileName = Mid(strSource, InStrRev(strSource, "\") + 1)
strSourceExtension = Right(strSource, Len(strSource) - InStrRev(strSource, "."))
'Debug.Print strSourceExtension
End If
End With
Just replace them with the appropriate numbers.
You can look up these numbers either by typing ?msoFileDialogFilePicker in the immediate window if you have it early-bound, by looking them up in the object browser, or, plain Google.
The corresponding number for msoFileDialogFilePicker is 3, so you can replace it with that:
With Application.FileDialog(3)
'...
.InitialView = 1 'View as list
'...
Related
I'm trying to set up a button in MS Word to update the linked Excel data source behind charts. I'll say upfront that I know this is easy with built-in functionality, but I'd like to use a button for those uncomfortable navigating the menus. Also, I've read and drawn from some great articles on this topic in general, but am getting an error not referenced in those pieces.
Testing has shown that Word a) identifies the Excel link as a field (vs a shape) and b) flags this field as type 87 (OCX). When I run the code below, it generates an "object variable or With block variable not set" error. However this appears to possibly be rooted in the field type based on this Microsoft article.
I'm struggling to find an alternative method and would appreciate help... this is my first foray into VBA for Word. Thanks!
Sub CommandButton1_Click()
Dim MyNewFile As Variant
Dim fDialog As FileDialog, result As Integer
Dim fieldCount As Integer
'Set up file dialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = False
.Title = "Select a file"
.Filters.Clear
.Filters.Add "Excel files", "*.xlsx,*.xlsb,*.xlsm,*.xls"
End With
'Show the dialog, store the file name
If fDialog.Show = -1 Then
MyNewFile = fDialog.SelectedItems(1)
End If
fieldCount = ActiveDocument.Fields.Count
For k = 1 To fieldCount
With ActiveDocument.Fields(k)
If .Type = 87 Then
With .LinkFormat
.SourceFullName = MyNewFile 'BLOWS UP HERE
.AutoUpdate = True
End With
End If
End With
Next k
End Sub
This is my first time posting and before I get into my question I just want to say how much I appreciate all the time and effort everyone puts into solving these problems.
I'm working on setting up a simple project management system which uses both MS Access and Excel. Through various forms in Access a user can set up a new project with name, address, logo etc. For the logo a user can double click on an image control box and choose a jpeg from any folder they choose.
My problem is with saving the logo. When the user clicks on the Save button, I would like to save the picture into a specific folder, not the one the user choose from.
This code, which works as expected, gives me the file path of where the user chose the picture from and displays the picture.
Private Sub CompLogo_DblClick(Cancel As Integer)
Dim sFile As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Choose Logo"
.Filters.Clear
.Filters.Add "JPEG", "*.jpg"
If .Show = -1 Then
sFile = .SelectedItems(1)
End If
End With
If sFile <> "" Then
Me.CompLogo.Picture = sFile
End If
End Sub
I know I can save the path as text in an Access table...
Sub SaveNewProject()
Set rst = CurrentDb.OpenRecordset("ProjectsTbl", dbOpenTable)
rst.AddNew
rst!Logo = LogoFilePath
rst.Update
rst.Close
Set rst = Nothing
End Sub
... but I need the actual jpeg to be saved in the specific folder so it can be used later in the Excel side of the program.
Any help you can give is much appreciated.
I'm running into an error when I attempt to add filters to a FileDialog.
I've tried to troubleshoot this with multiple posts / articles but nothing seems to have solved it. The online documentation identifies using .Filters.Add but it doesn't seem that .Filters is an appropriate method. .Filter.Add is at least recognized but is still invalid. .Filter.Clear is also recognized but is invalid as well.
Code:
Private Sub Command93_Click()
Dim objFD As Object, strOut As String
Set objFD = Application.FileDialog(2)
With objFD
.Filter.Clear
.Filter.Add "Excel File", ".xls"
If objFD.show = -1 Then
strOut = objFD.selecteditems(1)
End If
End With
Set objFD = Nothing
Me.txtSaveFile = strOut
Me.Refresh
End Sub
Ok here is the answer. First, thanks to the above shots at solving for the issue as they did lead to me reviewing the documentation (again) and making another attempt. The first answer put me on to the issue as .Filters simply won't work with a msoFileSaveAs dialog. However, you can't type in a different file name on an Open dialog (either 1 or 3).
So the answer is to use msoFileSaveAs or (2) and use the .Initialfilename property to set the extension which, in this case, will show a list of excel files which can be selected, or selected and modified and then you can click on "Save"
Working code:
Private Sub Command93_Click()
Dim objFD As Object
Dim strOut As String
Set objFD = Application.FileDialog(2)
With objFD
'.Filters.Clear
'.Filters.Add "Excel File", "*.xls; *.xlsx", 1
.Initialfilename = "*.xlsx"
If .show = -1 Then
strOut = objFD.selecteditems(1)
End If
End With
Set objFD = Nothing
Me.txtSaveFile = strOut
Me.Refresh
End Sub
Thanks all!
You are using the the filters with the wrong dialog box. .Filters will work with msoFileDialogOpen which has a value of 1 or with msofiledialogfilepicker which has a value of 3. So try .Filters with Application.FileDialog(1) or Application.FileDialog(3) as shown below and it will work but it will not work with the msofiledialogsaveas which has a value of 2
The constants msoFileDialogOpen and msoFileDialogSaveAs are not supported in Microsoft Access. What it doesn't mean: is that the Application.FileDialog is NOT suported. What it means: is the constants are not supported. If you type ?msoFileDialogOpen in Immediate Window in MS Access, you will not see any value and hence we have to pass those values literally or declare them.
You have to use .Filters instead of .Filter
See this example
Option Explicit
Const msoFileDialogOpen As Integer = 1
Sub Sample()
Dim f As Object
Dim i As Long
Set f = Application.FileDialog(msoFileDialogOpen)
With f
.Filters.Clear
.Filters.Add "Excel File", "*.xls*"
.AllowMultiSelect = True
If .Show Then
For i = 1 To .SelectedItems.Count
MsgBox .SelectedItems(i)
Next
End If
End With
End Sub
Screenshot
EDIT:
If you want to use Filters in while saving then check out the link Display Open and Save As Dialog Boxes in Access with API Functions
Here you will see how to use filters in the using the .SaveFileDialog using the CommonDialogAPI
Say you want to have a button that the user can click and save a copy of the current file as a PDF(Documentation):
Application.ActiveDocument.SaveAs2 fileName:="fileName.pdf", FileFormat:=wdFormatPDF
This works fine, the user is presented with a save dialog, selects a location and the file is saved, however a few things are not correct:
The type displayed does not match what was specified in the VBA, how can this be correct? It still saves as type "PDF" without issue, even after showing "DOCX" as the file type in the "Save as Type" drop down. Also the "fileName.pdf" is not placed in the "File Name" box, its as if the dialog box is unaware of the options set in the VBA code(This same issue is also referenced in this post).
UPDATE 1
After taking a second look at my code I now realize that the SaveAs2 Method was not displaying the dialog menu, the correct version of the code(simplified) can be described as:
Dim selected As String: selected = Application.FileDialog(msoFileDialogSaveAs).Show()
Dim filePath As String
If selected <> 0 Then
filePath = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
Application.ActiveDocument.SaveAs2 fileName:=Split(filePath, ".")(0), FileFormat:=wdFormatPDF
End If
So then the real question(I guess) is how do you get "Application.FileDialog" to display the proper type you wish to save in under the "Save as type" drop down, and this has already been answered by #PatricK. Thanks everyone for the help, I apologize for the initial confused nature of this question.
I am surprised for SaveAs2 will bring you a prompt to be honest - Only a new document and .Save will bring you that prompt.
If you want to get something similar to that prompt, you use Application.FileDialog with type msoFileDialogSaveAs.
Use this code below (perhaps as an AddIn suits more):
Option Explicit
Sub MySaveAs()
Dim oPrompt As FileDialog, i As Long, sFilename As String
Set oPrompt = Application.FileDialog(msoFileDialogSaveAs)
With oPrompt
' Find the PDF Filter from Default Filters
For i = 1 To .Filters.Count
'Debug.Print i & " | " & .Filters(i).Description & " | " & .Filters(i).Extensions
' Locate the PDF filter
If InStr(1, .Filters(i).Description, "PDF", vbTextCompare) = 1 Then
.FilterIndex = i
Exit For
End If
Next
' Change the title and button text
.Title = "Saving """ & ActiveDocument.Name & """ to PDF format"
.ButtonName = "Save to PDF"
' Default name
.InitialFileName = ActiveDocument.Name
' Show the Prompt and get Filename
If .Show = -1 Then
sFilename = .SelectedItems(1)
Debug.Print "Final filename: " & sFilename
' Save the file as PDF
ActiveDocument.SaveAs2 sFilename, wdFormatPDF
End If
End With
Set oPrompt = Nothing
End Sub
Screenshot sample:
I have this task to convert recenntly written VBA code to OpenOffice version. I tried to launch it from OpenOffice, but it doesn't work (mostly "unsatisfied query..." error. I am now stuck on Open File Dialog, I can either use VBA compatible open file dialog as mine looks now like that (giving error):
FileToOpen = Application.GetOpenFilename("Please choose a file to import", "Excel Files *.dbf (*.dbf)")
I can also use OpenOffice file dialog box, but couldn't find any information on this.
Thanks in advance
I'm confused on what you're asking, but if you're having trouble creating a file dialog box, this is VBA code that will do it for you. I think this is what you're asking, but I could be wrong.
Private Sub cmdFileDialog_Click()
' This requires a reference to the Microsoft Office 11.0 Object Library.
Dim fDialog As Office.FileDialog
Dim varFile As Variant
' Clear the list box contents.
Me.FileList.Value = ""
' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Change allowmultiselect to true if you want them to be able to select multiple files
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Select One or More Files"
' Clear out the current filters, and then add your own.
.Filters.Clear
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
' Loop through each file that is selected and then add it to the list box.
For Each varFile In .SelectedItems
Me.FileList.Value = varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub