【问题标题】:Open a PowerPoint presentation from Excel with VBA and then set that presentation to a variable使用 VBA 从 Excel 中打开 PowerPoint 演示文稿,然后将该演示文稿设置为变量
【发布时间】:2015-10-14 04:17:15
【问题描述】:

我必须将大量 Excel 图表发布到特定的 PowerPoint 文档中,并且我正在 Excel VBA 中构建一个宏来为我做这件事。

我能够正确打开要更新的 PowerPoint 演示文稿,但是我不知道如何将刚刚打开的演示文稿设置为名为 MyPresentation 的变量。

Dim myPresentation As PowerPoint.Presentation
Dim PowerPointApp As PowerPoint.Application

PowerPointApp.Presentations.Open Filename:="obscured filepath and name"`

显然还有一些额外的代码,但我正在尝试将我刚刚在第 3 行打开的演示文稿设置为 MyPresentation 变量,以便我可以引用我刚刚打开的文档。

【问题讨论】:

    标签: excel vba powerpoint


    【解决方案1】:

    首先你要为使用ppt文件做铺垫, 我做了什么:

    Dim DestinationPPT As String
    Dim PowerPointApp As PowerPoint.Application
    Dim myPresentation As PowerPoint.Presentation
    
    Set PowerPointApp = CreateObject("PowerPoint.Application")
    
    DestinationPPT = "path"
    PowerPointApp.Presentations.Open (DestinationPPT)
    

    【讨论】:

      【解决方案2】:

      我最终找到了 MVP Andy Pope 的解决方案。

      一些相关代码sn-ps供未来用户使用。 (仅供参考,当我遇到问题时,我的 PPT 已经可见)

      Dim DestinationPPT As String
      Dim PowerPointApp As PowerPoint.Application
      Dim myPresentation As PowerPoint.Presentation
      
      'Easier to define manually set links up front so it's easier to change/modify
      DestinationPPT = "C:\yourfilepath\yourfilename.pptx"`
      

      查找电子表格大师从 Excel VBA 打开 PPT 的指南

      然后:

      Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
      

      【讨论】:

      • 这会导致我出现与 PowerPointApp 变量相关的“对象变量或未设置块变量”区域。使用 CreateObject 首先设置 PowerPointApp,如 ec.v1352 的回答,解决了问题。
      【解决方案3】:

      我试图创建一个 excel 文件,该文件可以基于特定工作表生成预定义的 powerpoint 演示文稿,在 excel 文件中具有不同的值。

      我的问题是我部门的每个人都应该能够下载并运行 excel 文件。

      在 vba 中定义演示文稿并不适用于所有人,因为此操作所需的参考并未在每台计算机/excel 程序上激活。

      Createobject 为我解决了这个问题。 这是我正在谈论的代码:

      Dim pptApp as object
      Dim strpath as string
      
      strpath = ThisWorkbook.Path & "\Test_Dummy.pptx"
      Set pptApp =  createobject("Powerpoint.application")
      
      pptApp.Presentations.Open Filename:=strpath, readonly:=false, untitled:=true, withwindow:=true
      

      没有工作的是:

      Dim pptApp as Powerpoint.Application
      

      Dim pptApp as object
      Set pptApp = New Powerpoint.Application
      

      没有在 vba 中设置所需的引用

      【讨论】:

        【解决方案4】:

        这个对我有用:

        'devlare variables
        Dim MyPPT As Object
        
        'create PowerPoint
        Set MyPPT = CreateObject("Powerpoint.application")
        'make it visible
        MyPPT.Visible = True
        'path to powerpoint
        MyPPT.presentations.Open "path\powerpoint.pptx"
        

        在打开之前将可见设置为真。否则它对我不起作用

        【讨论】:

        • 如果您以无窗口方式打开演示文稿,则无需将其设置为可见(并且您的代码可能会运行得更快)。 Presentations.Open("filename",bReadOnly,bUntitled,bWithWindow) 或 Presentations.Open("filename",,,False)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-28
        • 1970-01-01
        • 2020-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-04
        相关资源
        最近更新 更多