PDA

View Full Version : [SOLVED:] Use VBA to open the dialog box to All Files instead of All Word Documents



spencer
05-12-2014, 12:16 PM
I have Word 2010 and I have code that is automatically importing a csv file into microsoft word and saving it as a pdf file. You press the button and the macro automatically opens the dialog box where the user selects the csv file to import but every time the dialog box opens the default opens with "All Word Documents" selected (only viewing word docs) but I want the default to be "All Files" so that you can actually see the CSV files and not have to manually change it every time.

How can I change the dialog box so that it automatically opens with all "All Files" selected instead of "All Word Documents"?

Here is the part of the code that prompts the dialog box:

Options.DefaultFilePath(wdDocumentsPath) = "W:\Daily to Fortis\Today"
With Dialogs(wdDialogInsertFile)
.Name = "*.txt"
.Show
End With


Thanks

snb
05-12-2014, 12:59 PM
Sub M_snb()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Show

For j = 1 To .SelectedItems.Count
Documents.Add .SelectedItems(j)
Next
End With
End Sub

spencer
05-12-2014, 01:29 PM
Thanks, but when I use that different type of object it changes the file path and doesn't open up to the specified file path. In my original code (shown below) I change the default file path to "W:\Daily to Fortis\Today" but when I put your code in to replace the with part (second set of code below) it opens all file types but not to the same specified file path, even though I am still specifying the path right above that part.

Original:
'Get the present default filepath for documents
defpath = Options.DefaultFilePath(wdDocumentsPath)
'Change it to W:\
Options.DefaultFilePath(wdDocumentsPath) = "W:\Daily to Fortis\Today"
With Dialogs(wdDialogInsertFile)
.Name = "*.txt"
.Show
End With

With Your New Helpful Code:
'Get the present default filepath for documents
defpath = Options.DefaultFilePath(wdDocumentsPath)
'Change it to W:\
Options.DefaultFilePath(wdDocumentsPath) = "W:\Daily to Fortis\Today"
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Show
End With

So how could I use that code you sent me to open up to the Default file path of "W:\Daily to Fortis\Today" ?

gmaxey
05-12-2014, 06:17 PM
If you use an object, then you can use the locals window to help you see what properties and methods are associated with it:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDl As Object
Set oDl = Application.FileDialog(msoFileDialogFilePicker)
With oDl
.InitialFileName = "W:\Daily to Fortis\Today"
.AllowMultiSelect = True
.Show
For j = 1 To .SelectedItems.Count
Documents.Add .SelectedItems(j)
Next
End With

macropod
05-13-2014, 02:09 AM
Cross-posted (and responded to in a different way) at: http://www.msofficeforums.com/word-vba/20985-use-vba-open-dialog-box-all-files.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

spencer
05-13-2014, 07:19 AM
Thanks so much, thats perfect. And thanks for the link about cross-posting, i am pretty new to this