Consulting

Results 1 to 3 of 3

Thread: Open filedialog box in windows & mac using vba

  1. #1

    Open filedialog box in windows & mac using vba

    Sir, I have a excel file & want to select that file using vba code from powerpoint(windows). So I want to create a general vba code which will be applicable for both windows as well as mac so first verify is it windows or mac and then run the code.I have windows so i create below code which open filedialog "C:" drive. How to configure for mac (just open dialog box from where i can browse the file )? Any other solution will be equally appreciated.

    Sub SelectFile()


    MsgBox "Please select/browse data excel file from your computer! "
    Dim fDialog As FileDialog
    Dim filepath As String


    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

    'Optional: FileDialog properties
    fDialog.AllowMultiSelect = False
    fDialog.Title = "Select a file"
    fDialog.InitialFileName = "C:"
    'Optional: Add filters
    fDialog.Filters.Clear
    fDialog.Filters.Add "Excel files", "*.xlsx"
    fDialog.Filters.Add "All files", "*.*"


    'Show the dialog. -1 means success!
    If fDialog.Show = -1 Then
    filepath = fDialog.SelectedItems(1)
    Else
    End If
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Don't know the syntax to get the file name on a Mac, but I'm guessing that you'll need some complier directives


    Option Explicit
    
    
    Dim filepath As String
    
    
    
    
    #If Mac Then
    
    
    
    
    
    
    #Else
    Sub SelectFile()
        Dim fDialog As FileDialog
        Dim filepath As String
        
        MsgBox "Please select/browse data excel file from your computer! "
        
        Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
        With fDialog
            'Optional: FileDialog properties
            .AllowMultiSelect = False
            .Title = "Select a file"
            .InitialFileName = "C:"
            'Optional: Add filters
            .Filters.Clear
            .Filters.Add "Excel files", "*.xlsx"
            .Filters.Add "All files", "*.*"
    
    
            'Show the dialog. -1 means success!
            If .Show = -1 Then filepath = fDialog.SelectedItems(1)
        End With
    End Sub
    
    
    #End If
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Paul thanks Sir
    I want to open filedialog only in mac.Not to open any specific drive like windows(C: drive).If filedialog open then I can browse file from there.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •