PowerPoint Creative
Log In or Register to participate in PPC!
PowerPoint Creative
Log In or Register to participate in PPC!

Go down
GamesByTim
GamesByTim
Featured Creator
Featured Creator
Posts : 239
Join date : 2017-10-09
https://www.gamesbytim.com/

File Selection on PowerPoint for Mac Empty File Selection on PowerPoint for Mac

Fri Nov 20, 2020 5:36 pm
Message reputation : 100% (1 vote)
On PowerPoint for Windows, you can select a file to read with the VBA function Application.FileDialog(). This function does not work on PowerPoint for Mac.

The good news is, it's still possible to select files on PowerPoint for Mac, using separate VBA! This tutorial will walk you through how file selection works on Mac and how to implement it in a cross-compatible way.

Download the demo!

The demo includes two buttons: one to read the first line in a text file, and one to read the value of A1 in an Excel file. Both buttons work on PowerPoint for Windows and Mac.

The Mac-specific VBA


Code:
Dim FileFormat As String
Dim RootFolder As String
Dim FileScript As String
Dim FilePath As String

On Error Resume Next
        
FileFormat = "{""public.text""}"
RootFolder = MacScript("return (path to desktop folder) as String")
        
FileScript = "return posix path of (choose file of type" & " " & FileFormat & " " & "with prompt ""Select Text File""" & _
            " default location alias """ & RootFolder & """) as string"
    
FilePath = MacScript(FileScript)
    
If FilePath <> "" Then
   ' Run functions for the FilePath
End If

To select a file on PowerPoint for Mac, we use AppleScript , a scripting language that can perform various operations on macOS. It's loosely similar to Batch scripting on Windows. We can run AppleScript on PowerPoint with the VBA function MacScript().

The above example contains two AppleScript commands - RootFolder to get the exact filepath of the user's Desktop folder (Ex: "/Users/Tim/Desktop") and FileScript to show the file select dialog defaulting at the Desktop.

The file path of the selected file goes into the variable FilePath, which you then use to open and perform whatever you need to with that file.

We use the variable FileFormat to restrict the type of files the user can select. In the above example, you can only select text files:

  • "{""public.text""}"

To restrict to Excel files, use:

  • "{""org.openxmlformats.spreadsheetml.sheet""}"

To restrict to text AND excel files, use:

  • "{""public.text"", ""org.openxmlformats.spreadsheetml.sheet""}"

Find the syntax for other files here . (Scroll down to Other file formats are)

You can also change the header text for the file select dialog to something other than "Select Text File".

Cross-compatible VBA


Combining Windows and Mac-specific VBA is straightforward. Simply use a preprocessor if statement, like so:

Code:
#If Mac Then ' PowerPoint for Mac
    ' Run Mac-specific VBA
     
#Else ' PowerPoint for Windows
    ' Run Windows-specific VBA

#End If

Caveats


  • Due to Apple's app sandbox, Mac users have to additionally grant PowerPoint permission to access the selected file. Thankfully PowerPoint thoroughly guides users through this step, so I don't foresee many issues with technical support. Also, users only have to grant file access once per file.
  • The VBA function MacScript() is deprecated by Microsoft . That said, I wouldn't worry too much about this.
    - Microsoft is known for their top-notch backwards compatibility, so I doubt they'll remove MacScript() anytime soon. Besides, I couldn't find any deprecated VBA function that Microsoft removed in Office. (If you know any, please let me know.)
    - The main reason MacScript() is deprecated is because Apple's app sandbox won't let MacScript() run AppleScript commands that open applications. Luckily for us, we don't need MacScript() to open applications.
  • On Mac, reading Excel files requires that Excel is installed. Opening an Excel file will cause Excel to take over as the active window, so users will have to re-select PowerPoint in the dock to get back to the slide show.
  • If you don't have macOS, I recommend NOT adding file selection support for Macs. The last thing you want is a Mac user to report about the file selector not working, with no way to test the code for yourself.

rusnakcreative likes this post

Back to top
Permissions in this forum:
You cannot reply to topics in this forum