1

I have a VBA code running under Excel 2016 (Windows) that exports charts to a PowerPoint file (.pptx).
I am trying to make that run on MacOS.

I first rewrote the FileDialogOpen function for MacOS to browse and pick up one .pptx file to open for editing.
While opening the picked file on MacOS (Office 2016 as well), I get the following error:
Error message while opening a .pptx file from VBA on MacOS

It seems a PowerPoint application is started but the specified .pptx file is not loaded. I checked the file name variable is assigned to the full path filename that was picked up (including extension .pptx).

The code failing on Open.

Sub Export_Charts_To_PPT_Presentation()
    
    Dim PptApp As PowerPoint.Application
    Dim PptDoc As PowerPoint.Presentation
    
    Set PptApp = New PowerPoint.Application
    
    PptPresPath = FileDialogOpen
    If PptPresPath = "" Then Exit Sub
    Set PptDoc = PptApp.Presentations.Open(PptPresPath, WithWindow:=msoTrue)

End Sub

Function FileDialogOpen() As String

    mypath = MacScript("return (path to desktop folder) as String")

    sMacScript = "set applescript's text item delimiters to "","" " & vbNewLine & _
      "try " & vbNewLine & _
      "set theFiles to (choose file " & _
      "with prompt ""Please select a file or files"" default location alias """ & _
      mypath & """ multiple selections allowed false) as string" & vbNewLine & _
      "set applescript's text item delimiters to """" " & vbNewLine & _
      "on error errStr number errorNumber" & vbNewLine & _
      "return errorNumber " & vbNewLine & _
      "end try " & vbNewLine & _
      "return theFiles"

    FileDialogOpen = MacScript(sMacScript)
End Function
Community
  • 1
  • 1

2 Answers2

2

Try this:

Sub Export_Charts_To_PPT_Presentation()
    
    Dim PptApp As PowerPoint.Application
    Dim PptDoc As PowerPoint.Presentation
    Dim PptPresPath As String
    
    PptPresPath = FileDialogOpen
    
    If PptPresPath = "" Or Right(PptPresPath, 5) <> ".pptx" Then Exit Sub
    
    Set PptApp = New PowerPoint.Application
    Set PptDoc = PptApp.Presentations.Open(PptPresPath, WithWindow:=msoTrue)

End Sub

Function FileDialogOpen() As String

    Dim iPathStartPosition As Integer
    
    mypath = MacScript("return (path to desktop folder) as String")
    sMacScript = "set applescript's text item delimiters to "","" " & vbNewLine & _
        "try " & vbNewLine & _
        "set theFiles to (choose file " & _
        "with prompt ""Please select a file or files"" default location alias """ & _
        mypath & """ multiple selections allowed false) as string" & vbNewLine & _
        "set applescript's text item delimiters to """" " & vbNewLine & _
        "on error errStr number errorNumber" & vbNewLine & _
        "return errorNumber " & vbNewLine & _
        "end try " & vbNewLine & _
        "return theFiles"
  
    FileDialogOpen = Replace(MacScript(sMacScript), ":", "/")
    
    If Val(FileDialogOpen) = -128 Then
        FileDialogOpen = ""
    Else
        iPathStartPosition = InStr(1, FileDialogOpen, "/Users")
        If iPathStartPosition > 0 Then
            FileDialogOpen = Right(FileDialogOpen, Len(FileDialogOpen) - iPathStartPosition + 1)
        End If
    End If
End Function
Super Symmetry
  • 2,837
  • 1
  • 6
  • 17
  • Thanks a lot super Symmetry. I took your code and made some progress. Now I can see the file open in PowerPoint compared to before where file was not opened but then macros is still stuck on the Open statement with same error message I posted before. – Maurice Cinque Jul 14 '20 at 19:15
  • Remove the parameter `WithWindow:=msoTrue` and try again – Super Symmetry Jul 14 '20 at 19:23
  • One question: the pptx file was created on Windows. Should it be first opened and saved manually using PowerPoint on Mac to ensure it is fully compatible with PowerPoint Mac thus with PPT VBA? – Maurice Cinque Jul 14 '20 at 19:37
  • After removing parameter WithWindow:=msoTrue macro is now running beyond the Open statement. I am now debugging other pieces of code. Thanks Super Symmery. – Maurice Cinque Jul 14 '20 at 20:13
  • You're welcome and I am glad it is working for you. If this answered your question, kindly don't forget to set it as answered. – Super Symmetry Jul 14 '20 at 20:46
  • Unfortunately despite file is loaded into PowerPoint, the PptDoc object returned by open sems to be not initialized. A Watch point set on PptDoc does not show any attributes when expanded and finally all macro crashes on statement after Open with same error. It seems there is a general corruption after file is opened. I get a cryptic stack trace. – Maurice Cinque Jul 15 '20 at 20:22
  • I managed to change your code by displaying the return values of your macscript for different scenarios (e.g. pressing cancel). I haven't read your macscript and have never written one in my life. The code is running well on my computer. I have microsoft 365 installed on an old macbook pro running High Sierra. What office version are you running? Can you re-install it? For me it is very difficult to help if I cannot reproduce your error. – Super Symmetry Jul 16 '20 at 08:39
  • Update your question with the "cryptic stack trace", please – Super Symmetry Jul 16 '20 at 08:40
  • 1
    Hello Super Symmetry, given the huge gap between VBA on Windows and MacOS, we are going to pursue a different strategy to avoid porting and maintaining scripts on two environments. A server based approach will avoid us suporting scripts for Mac/Win laptops. Anyway I want to thank you for your time in helping me on this post. – Maurice Cinque Jul 17 '20 at 12:40
0

MacScript has been deprecated.

This article: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/macscript-function

Points you to this article: The MacScript function is not working well in Office for Mac 2016! Any ideas?

The answer is that you need try again using AppleScriptTask. If you get stuck there then you should open a new question with your AppleScriptTask code.

HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • Here's more about AppleScriptTask: https://www.rondebruin.nl/mac/applescripttask.htm – John Korchok Jul 13 '20 at 23:43
  • Thanks for the pointers. I understand MacScript is deprecated however the problem seemed to be a different issue. For the very short term, I am keeping MacScript provided i am able to open the file. But for sure for the long run I will migrate to AppleScriptTask once I have figured out porting the whole functionality of my code. – Maurice Cinque Jul 14 '20 at 20:42
  • BTW I realize the pain for developers who need to port and maintain VBA code on Windows and MacOS. In an enterprise context, it is very common to have a mix of laptops... – Maurice Cinque Jul 14 '20 at 20:46