1

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:

enter image description here

It then highlights this line of code:

enter image description here

Additionally, I have added references to Microsoft Office 16.0 Object Library, and everything I can think of:

enter image description here

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
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • 1
    Why would you add references to Office object model? If the `Application` in question comes from solidworks, then that's what you have to use. There is no `Word.Application` or `Excel.Application`. – GSerg Apr 20 '21 at 08:55
  • Don't think it'll help, but when you don't have a reference to the Office library, in Excel you can use: `Dim fd As Object: Set fd = Application.FileDialog(3)`. – VBasic2008 Apr 20 '21 at 10:06

2 Answers2

0

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:

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!

c1one
  • 99
  • 7
-1

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

macropod
  • 12,757
  • 2
  • 9
  • 21
  • `repair of your Office installation` - this is [solidworks](https://stackoverflow.com/tags/solidworks/info), not Office. – GSerg Apr 20 '21 at 09:02
  • So why are you using code from: https://learn.microsoft.com/en-us/office/vba/api/office.filedialog - which *requires* you to use Office? The only code natively available to SolidWorks is that which SolidWorks provides. If you want to take advantage of Office code, you need to automate an Office application - which you haven't done. Simply adding Office references is insufficient. – macropod Apr 20 '21 at 09:13
  • Yes, that's [what I said](https://stackoverflow.com/questions/67175418/vba-application-filedialog-object-doesnt-support-this-property-or-method/67175688?noredirect=1#comment118738829_67175418). – GSerg Apr 20 '21 at 10:25
  • I think not. You've shown no understanding of what automation entails. In any event, you should look at what file selection tools SolidWorks provides. Failing that, for a generic file browser see: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/e4b20ead-dbf3-49f0-bb36-45bb6634fb72/problem-with-the-browseforfolder-function?forum=ITCG – macropod Apr 20 '21 at 11:33