Harshit Pandey
Harshit Pandey

Reputation: 21

Opening a user form in Powerpoint via VBScript

I am trying to open a user form which I have created in a PPTM file via VBScript. Code for VB script is as below. This does seem to be working. It is simply opening that macro PPTM and closing it. Any suggestions?

Option Explicit

Dim pptApp, pptPresentation, CurrentDirectory

dim fso: set fso = CreateObject("Scripting.FileSystemObject")

CurrentDirectory = fso.GetAbsolutePathName(".")

Set pptApp = CreateObject("PowerPoint.Application")
Set pptPresentation = pptApp.Presentations.Open(CurrentDirectory + "\Revison Macro V1.pptm",True)

On Error Resume Next
pptApp.Run "Revision"
If Err Then

End If

pptPresentation.Close
pptApp.Quit

Set pptPresentation = Nothing
Set pptApp = Nothing

WScript.Quit

Upvotes: 1

Views: 1454

Answers (1)

Rich
Rich

Reputation: 4170

A Few code revisions

  1. Set pptPresentation = pptApp.Presentations.Open(CurrentDirectory + "\Revison Macro V1.pptm",True) >> VBScript uses "&" rather than "+" even though this worked fine, it's better to stick to the correct string handling.
  2. The userform needs to be indirectly called to pause the vbscript. So create a separate Sub and call it "Call_Revision". The code will be simple and straightforward as follows:

    Sub Call_Revision
        Revision.Show
    End Sub
    
  3. When you execute the .Run command, it needs to know how to find the code to run the UserForm. So now that we have established our sub, we can use that to show the form.

    From: pptApp.Run "Revision"

    To: pptApp.Run "Revison Macro V1.pptm!Module1.Call_Revision"

  4. If you are waiting for the user to close out the userform to execute the rest of the code and exit the PPTM file, you can apply the following OnClose event within the Userform:

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
         Application.Quit
    End Sub
    

    And the Full Code:

    Option Explicit
    Dim currppt : currppt = "Revison Macro V1.pptm"
    Dim ModuleName: ModuleName = "Module1"
    Dim OpenUF : OpenUF = "Call_Revision"
    Dim pptApp, pptPresentation, CurrentDirectory
    dim fso: set fso = CreateObject("Scripting.FileSystemObject")
    CurrentDirectory = fso.GetAbsolutePathName(".")
    Set pptApp = CreateObject("PowerPoint.Application")
    Set pptPresentation = pptApp.Presentations.Open(CurrentDirectory & "\" & currppt,True)
    On Error Resume Next
    pptApp.Run currppt & "!" & ModuleName & "." & OpenUF
    msgbox "Done"
    
    pptPresentation.Close
    pptApp.Quit
    
    Set pptPresentation = Nothing
    Set pptApp = Nothing
    

Upvotes: 1

Related Questions