Open Windows level fileopen dialog via vba - vba

When I open the standard fileopen dialog using VBA (Word)
e.g., Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
I get what I ask for, but it's not what I want. I am defaulted to opening the selected file in the parent program. (If in Word, and displaying Excel files, the selected Excel file will open in Word, not the program associated with the .xls extension; if displaying PDF, the selected file will open in Word, etc.).
How can I get a Window's level (as opposed to application level) dialog to open so that when I click a document with a non-Word extension, the proper program associated with the file extension will be called. (I know that I can always right click on the file and click 'Open With . . . ', but I don't want to have to teach this to my staff if I can avoid it.)

Private Sub Tester()
Dim fd As FileDialog, sh As Object
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "All files", "*.*"
If .Show = -1 Then
Set sh = CreateObject("Shell.Application")
sh.Open .SelectedItems(1)
End If
End With
End Sub

Related

Open a Word 2013 legacy Open dialog in a specific folder

I have a simple Word macro that shows the legacy Open dialog.
Sub LegacyOpen()
DoEvents
Dialogs(wdDialogFileOpen).Show
End Sub
I’m now trying to get it to open in a specific folder.
"C:\Users\Paul Schroeter\Documents\Microsoft Word Documents".
After about an hour I have not found an macro argument or example of how to get it to do what I want it to do.
If you wonder why I need to do this, it’s because every time I use “Search documents” in the legacy Open dialog, it resets the Open dialog path to "C:\Users\Paul Schroeter\Documents", which is driving me insane, because I then have to change it back to the folder where I actually keep my Word documents.
A number of the built-in Word dialog boxes have "dialog box arguments" corresponding to some of the controls/settings in the dialog box. A list can be found here. These are not part of the Intellisense and are late-bound into the object model. The developer needs to know they exist and how to look them up and use them.
One of these built-in arguments is to set/read the file full name from the File/Open dialog box. In VBA the arguments are usually used in a With block. Putting the argument before the Show or Display method executes the setting before the dialog box is shown to the user. If it's placed after the method, then it's used to read the user's choice.
Sub WordFileOpen()
Dim dlg As Word.Dialog
Dim sPath As String
Set dlg = Application.Dialogs(wdDialogFileOpen)
sPath = "C:\Users\Paul Schroeter\Documents\Microsoft Word Documents"
With dlg
.Name = sPath
.Show
End With
End Sub
If you consider something different than Dialogs collection you could use FileDialogs property. Here is working example:
Sub OtherWindowType()
Dim FD As FileDialog
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
.AllowMultiSelect = False
.InitialFileName = "c:\" '...your path here
.Show
End With
'if you want to open the file...
If FD.SelectedItems.Count > 0 Then
Documents.Open FD.SelectedItems(1)
End If
End Sub

Access VBA - Dialog Box. Looking for an specific file

Now I have this code in order to open a dialog box to search for a concrete type of file, a .txt called memory: memory.txt
So:
Dim S As String
S = OpenCommDlg("C:\memory.txt")
If IsNull(S) Or S = "" Then Exit Sub
Unfortunately, this opens a dialog box in C:\, of course, but looking for image type archives, which is absolutely not what I'm looking for. You can see this at the right side of the attached image:
Anyone knows how to modify this code in order to find the kind of archive we are looking for, and it's name...
You can use the FileDialog method. This will save the full file path to your s string, and you can add multiple filters to filter by file type.
Dim s As String
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "Text Files", "*.txt"
.Filters.Add "All Files", "*.*"
If .Show Then s = .SelectedItems(1)
End With
Debug.Print s
If you want to automatically fill in the inputbox with your filename, then you can just add this line to the with statement:
.InitialFileName = "C:\memory.txt"

Choosing a filepath to export .xlsx file VBA

I'm trying to write a bit of code that allows the user to choose the file path of a folder before exporting data in a separate .xlsx file to that folder. Its easy enough to look up a folder's path beforehand and hard code it in, but I want this program to allow the user to choose a folder each time. As it is, I have this function that utilizes the excel open file dialog box. From there, I am able to find the folder I need, and just copy the file path from the top bar and hit cancel. Here's the code:
Function GetFileDestination() As String
Dim DataObj As New MSForms.DataObject
'This MsgBox just tells the user what to do
MsgBox "To get the file Destination, the 'Open File' Dialog Box will open. Go to the folder_
you want to use, click on the bar at the top, and copy the destination. Then hit Cancel",_
vbOKOnly, "Finding the File Destination"
Application.Dialogs(xlDialogOpen).Show
DataObj.GetFromClipboard
GetFileDestination = DataObj.GetText
End Function
This does the job, but it seems pretty sloppy, since it forces the user to manually copy the file path needed and then cancel the open file dialog box.
Does anyone know a more creative and clean way about this while still keeping the same functionality?
Thanks in advance!
Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
Here's the description:
Application.FileDialog(msoFileDialogFolderPicker) - The folder dialog prompts the user to
select a directory path.
strPath - Default path which will be passed on to the function.
show - If the user chooses to cancel the dialog, the value '0' will be assigned, otherwise the value '-1' is assigned.
GetFolder = sItem - The path of the folder selected/opened is returned by this statement,
else Null is returned if Cancel button is clicked.
Hope this clears the overall logic used.

VBA to Star Basic (OpenOffice), struggling

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

Word VBA Save As Dialog with custom filter?

Is there a way (code) for "Save As" dialog in Word VBA with customer filters? For example: ".ttt"
I think you probably want to use the Application.FileDialog as this allows custom file filters. As KazJaw points out you can't save a Photoshop file in Word so I assume its to allow some other manipulation of a psd file.
The following shows you how to use it (see http://msdn.microsoft.com/en-us/library/aa219843%28office.11%29.aspx). Note this will allow the user to select multiple files though.
Sub CustomFilter()
'Declare a variable for the FileDialog object and one for the selectedItems
Dim fd As FileDialog, vSelectedItem As Variant
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'With the FileDialog
With fd
.Filters.Clear 'Clear current filters
.Filters.Add "Photoshop Files", "*.psd", 1 'Add a filter that has Photoshop Files.
If .Show = -1 Then
'Step through each String in the FileDialogSelectedItems collection.
For Each vSelectedItem In .SelectedItems
'Do whatever you want here
Next vSelectedItem
Else
'The user pressed Cancel.
End If
End With
Set fd = Nothing
End Sub