【问题标题】:Referencing PowerPoint Object in Excel VBA在 Excel VBA 中引用 PowerPoint 对象
【发布时间】:2017-08-24 13:28:19
【问题描述】:

长话短说 - 我正在尝试将字符串从 PowerShell 传递到 PowerPoint 以更新母版幻灯片上的文本框。我可以在 PowerPoint vba 中执行此操作,但它是完全静态的。当我尝试通过 PowerShell 将数据传递到 PowerPoint 时,我会收到一个我无法解决的错误。 (see here)

因此,我尝试通过 Excel 代理来实现!我已经使用 Excel VBA 来更新 PowerPoint 幻灯片上的内容,所以认为这很合适。我目前在 Excel 中设置了形状,更新了数据,然后将其粘贴到 PowerPoint 幻灯片中,保存并关闭。

我现在需要扩展这个 Excel 宏,使其具有更新母版幻灯片上的文本框的功能。我需要将“PowerPoint VBA”翻译成“Excel VBA for PowerPoint”,多么可怕的一句话……我跑题了:

PowerPoint VBA

Function UpdateMasterF(message As Variant)
    Dim sourceLabel As Shape
    Dim curSLD As Long
    curSLD = ActiveWindow.View.Slide.SlideIndex

    'switch to SlideMaster
    Application.Windows(1).ViewType = ppViewSlideMaster

    Set sourceLabel = ActivePresentation.SlideMaster.CustomLayouts(1).Shapes("sourcelabel")
    sourceLabel.TextFrame.TextRange.Text = message

    'return to default
    Application.Windows(1).ViewType = ppViewNormal

    'set slide
    ActiveWindow.Presentation.Slides(curSLD).Select

Excel VBA

Function TestPowerPoint(message As Variant, presPath As Variant)
    Dim oPPTApp As Object
    Dim oPPTFile As Object

    Set oPPTApp = CreateObject("PowerPoint.Application")
    oPPTApp.Visible = msoTrue
    Set oPPTFile = oPPTApp.Presentations.Open(presPath)

    ' translate e.g. ApplicationWindow, ActivePresentation etc

    oPPTFile.Save
    oPPTFile.Close
    Set oPPTFile = Nothing
    oPPTApp.Quit
    Set oPPTApp = Nothing

我需要能够在 Excel 宏中完成与 PowerPoint VBA 中相同的步骤。不过,我在为它找到正确的名称时遇到了问题。所以它说的地方

Application.Windows(1).ViewType = ppViewSlideMaster

可以用 oPPTApp 或 oPPTFile 代替吗?我已经阅读了MSDN Docs,它似乎匹配但不起作用。

希望这传达了我的要求!我希望大多数人在这种情况下阅读和颤抖......

【问题讨论】:

  • 需要切换视图吗?我知道如果您手动执行此操作,您会这样做,但如果您引用ActivePresentation.SlideMaster.CustomLayouts(1)...,那么也许您不需要?不过我不知道,我从来没有给大师写信。
  • PowerPoint 中您需要声明的所有内容。 e 并使用适当的 PowerPoint 对象进行引用。因此,您需要 oPPTApp.Windows(1).ViewType 而不是 Application.Windows(1).ViewType = ppViewNormal。我还将 oPPTApp 声明为 PowerPoint.Application 类型。确保您引用了 PowerPoint 对象库。
  • @CLR - 手动部分的唯一原因是确保选择了正确的母版幻灯片,因为它有 3。如果我能找到一行直接更新该幻灯片上的框我很乐意这样做。会研究。手动最快的方法是查看幻灯片并按下母版幻灯片,这会将您带到该幻灯片的母版。
  • 我的意思是,我不确定您是否需要找到与以下行等效的 Excel:Application.Windows(1).ViewType = ppViewSlideMasterApplication.Windows(1).ViewType = ppViewNormal。祝你好运!
  • @RichHolton PowerPoint.Application 似乎不是有效类型,我理解正确吗?将 oPPTApp 调暗为 PowerPoint.Application

标签: vba excel powershell ms-office powerpoint


【解决方案1】:

层次结构是这样的:

要访问在 PPT 中打开的演示文稿,您可以使用例如

Set oPPTPres = oPPTApp.Presentations(1) or
Set oPPTPres = oPPTApp.ActivePresentation

每个演示文稿对象都有一个 Designs 集合,代表演示文稿中的不同母版,因此要访问第一个设计的 SlideMaster,

With oPPTPres.Designs(1).SlideMaster
   ' for example:
   With .Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 500, 50)
      .TextFrame.TextRange.Text = "Well, look at that, will ya!"
   End With

End with

每个设计都有一组自定义布局(您在幻灯片母版视图中看到的缩进主母版下方的缩略图)。可以类似地访问它们。

【讨论】:

  • 史蒂夫,这是一个很棒的帮助,非常感谢。我想我已经几乎破解它了。我正在使用 With oPPTPres.Slides(3).CustomLayout With .Shapes("sourcelabel").TextFrame.TextRange.Text = message 它完成时没有错误(一切都是第一次!)但盒子没有更新。我查看了文档,CustomLayout 对象似乎是只读的,这是为什么呢?我将按照您的建议尝试通过 Designs 对象。
【解决方案2】:

所以扩展史蒂夫的答案,我决定用一个新的盒子替换现有的盒子,以节省尝试编辑现有形状的痛苦。

我很幸运,我所做的自动化是为了创建新的 PowerPoint 幻灯片,所以当我从一张空白画布创建多个幻灯片时,我可以将这些新形状放在它们上面。

我最终得到相同格式的代码是:

Function TestPowerPoint(message1 As String, message2 As String, presPath As Variant)

set up variables to be used
Dim oPPTApp As PowerPoint.Application
Dim oPPTFile As Object
Dim oPPTPres As PowerPoint.Presentation

Set oPPTApp = CreateObject("PowerPoint.Application")
oPPTApp.Visible = msoTrue
Set oPPTFile = oPPTApp.Presentations.Open(presPath)
Set oPPTPres = oPPTApp.ActivePresentation

With oPPTPres.Designs(1).SlideMaster.CustomLayouts(1)
    With .Shapes.AddTextbox(msoTextOrientationHorizontal, 28, 60, 500, 25)
        .ZOrder msoBringToFront
        With .TextFrame.TextRange
            .Text = message1
            With .Font
                .Size = 10
                .Name = "Calibri"
                .Color = RGB(255, 0, 0)
                .Bold = msoTrue
            End With
        End With
    End With
End With

With oPPTPres.Designs(1).SlideMaster.CustomLayouts(2)
    With .Shapes.AddTextbox(msoTextOrientationHorizontal, 28, 200, 736, 50)
        .ZOrder msoBringToFront
        With .TextFrame.TextRange
            .Text = message2
            .ParagraphFormat.Alignment = ppAlignCenter
            With .Font
                .Size = 32
                .Name = "Calibri"
                .Color = RGB(255, 255, 255)
                .Bold = msoFalse
            End With
        End With
    End With
End With

oPPTFile.Save
oPPTFile.Close
Set oPPTFile = Nothing
oPPTApp.Quit
Set oPPTApp = Nothing

End Function

太棒了!感谢所有做出贡献的人。

【讨论】:

  • Bingo ... 很好地完成了我的最后一篇文章并继续使用它。如果您需要检索之前添加的形状(在添加新形状之前进行编辑或删除),请查看标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
相关资源
最近更新 更多