OpenFileDialog VBA (Powerpoint) Initial Directory - vba

Please help me make my OpenFileDialog showing my PowerPoint project directory.
i tried .InitialDirectory = "C:\\" ,but it doesn't work. Can i adapt this to vba? Thanks.

This is nearly copied from the Powerpoint VBA help. It uses FileDialog instead of OpenFileDialog(which I had difficulty finding in relation to PowerPoint). It sets the InitialFileName to C:\ so that it will prompt in that location. Just place this in a module, it should be easy to modify to your specific needs:
Sub fileDialogExample()
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Declare a variable for the directory path.
Dim directory As String
'Set the directory path
directory = "C:\"
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is aString,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Change the initial directory\filename
.InitialFileName = directory
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is aString that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example displays the path in a message box.
MsgBox "The path is: " & vrtSelectedItem
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub

Related

VBA - Application.FileDialog() - Object doesn't support this property or method (Error 438)

My code is simple.
I have copied it off of the VBA example site: https://learn.microsoft.com/en-us/office/vba/api/office.filedialog
Additionally, every other variation I can find online has the same problem.
All I am attempting is to open a File Dialogbox (Similar to a file explorer) where a user can select a folder or files.
However, I carry on getting this error:
It then highlights this line of code:
Additionally, I have added references to Microsoft Office 16.0 Object Library, and everything I can think of:
How do I fix this or get this to Run?
Thank You
Here is the Code:
Sub Main()
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is aString,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is aString that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example displays the path in a message box.
MsgBox "The path is: " & vrtSelectedItem
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub
Disclaimer: I'm sure there is a better/more correct way to do this but it works for me in VBA for SolidWorks. I am not a real programmer. Just trying to help because I know how few resources there are on VBA programming in SW.
All I really did was add the Excel Object library and change Application to Excel.Application.
Despite what GSerg said, there can be an Excel.Application if you add the Excel object library as a reference.
Despite macropods condescension, you are not stuck with what SW provides or the generic file browser and you don't need to re-install Office or SW. The generic file browser doesn't show your Quick Access items. SolidWorks doesn't run on Mac OS. His "answer" does not work in SW VBA.
Here is my working function (started with code from here):
Function GetFolder(Title As String)
Dim folder As FileDialog
Dim selected_folder As String
Set folder = Excel.Application.FileDialog(msoFileDialogFolderPicker)
With folder
.AllowMultiSelect = False
.ButtonName = "Ok"
.Filters.Clear
.InitialFileName = Excel.Application.DefaultFilePath
.Title = Title
If .Show <> -1 Then GoTo NextCode
selected_folder = .SelectedItems(1)
End With
NextCode:
Debug.Print selected_folder
GetFolder = selected_folder
Set folder = Nothing
End Function
I added the Excel & Office Object Libraries to the default VBA SW:
If you have a way to do this that shows the Quick Access items without having to use the Excel Object Library I'm very interested!
I assume you're using a Windows system, since Application.FileDialog is not available/fully supported on Macs.
I suggest you start over, without adding a plethora of irrelevant references. The following works just fine without adding any to the default references:
Sub Demo()
Dim fd As FileDialog, vrtSelectedItem As Variant
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
MsgBox "The path is: " & vrtSelectedItem
Next
End If
End With
Set fd = Nothing
End Sub
If the code still doesn't work for you, a repair/reinstall of Office or your SolidWorks installation is most likely called for.
For code that works on Macs and PCs, see: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-msoffice_custom-mso_365hp/showing-dialogs-word-for-mac-vba/513ea974-378d-4ebe-95c3-a0221a9287ff

Is there a way to get the FileDialog to open up a remote computer folder using the.InitialFileName property

I'm testing out a user app that they will be accessing remotely. There is file dialog functionality that I've included using the FileDialog object. I'd like for them to initially open up their own local files using the property .InitialFileName, but I'm having trouble finding the correct path for the initial open. Here's what I have so far:
Dim fileDialog As Object 'Office.FileDialog
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName = "C on " & Environ("CLIENTNAME")
....
When I do open up the dialog without setting the initial file path, there is a location that the user can access that is ""C on PCName", so I thought that I could just set the string to be the same (in above code), but it doesn't work if I set that as the path. Environ("CLIENTNAME") does get the PCName of the user, so I think I am close to a solution.
Thanks for any suggestions.
There is a list of potentially useful environment variables here
This might be a starting point
Option Explicit
Sub x()
Dim fileDialog As Object 'Office.FileDialog
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName = Environ("HOMEPATH")
.Show
End With
End Sub
Never mind. I do believe I've come across a solution:
FileDialog.InitialFileName = "\tsclient\c"

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

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.

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