通过VBScript在Powerpoint中打开用户表单

时间:2014-04-07 13:50:17

标签: vbscript powerpoint

我正在尝试通过VBScript打开我在PPTM文件中创建的用户表单。 VB脚本的代码如下。这确实有效。它只是打开宏PPTM并关闭它。有什么建议吗?

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

1 个答案:

答案 0 :(得分:1)

几个代码修订

  1. Set pptPresentation = pptApp.Presentations.Open(CurrentDirectory + "\Revison Macro V1.pptm",True)>> VBScript使用"&"而不是" +"即使这样工作正常,坚持正确的字符串处理也会更好。
  2. 需要间接调用userform来暂停vbscript。因此,创建一个单独的Sub并将其称为" Call_Revision"。代码将简单明了,如下所示:

    Sub Call_Revision
        Revision.Show
    End Sub
    
  3. 执行.Run命令时,需要知道如何查找运行UserForm的代码。所以现在我们已经建立了sub,我们可以使用它来显示表单。

    来自:pptApp.Run "Revision"

    收件人:pptApp.Run "Revison Macro V1.pptm!Module1.Call_Revision"

  4. 如果您正在等待用户关闭userform以执行其余代码并退出PPTM文件,则可以在Userform中应用以下OnClose事件:

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

    完整代码:

    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