PDA

View Full Version : Open filedialog box in windows & mac using vba



dibyendu2280
09-12-2020, 07:48 AM
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

Paul_Hossler
09-12-2020, 08:05 AM
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

dibyendu2280
09-12-2020, 09:48 AM
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.