在Excel VBA中引用PowerPoint对象

在Excel VBA中引用PowerPoint对象,vba,excel,powershell,ms-office,powerpoint,Vba,Excel,Powershell,Ms Office,Powerpoint,长话短说-我正在尝试从PowerShell向PowerPoint传递一个字符串,以更新主幻灯片上的文本框。我可以在PowerPoint vba中执行此操作,但它是完全静态的。当我试图通过PowerShell将数据传递到PowerPoint时,我收到了一个无法解决的错误。() 因此,我试图通过Excel代理来实现这一点!我已经使用Excel VBA来更新PowerPoint幻灯片上的内容,所以我认为这是合适的。我目前在Excel中设置了形状,更新了数据,然后将其粘贴到PowerPoint幻灯片中,

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

因此,我试图通过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
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 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
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
我需要能够在PowerPoint VBA中完成相同的步骤,但要在Excel宏中完成。不过,我很难找到合适的名字。那么它说什么呢

Application.Windows(1).ViewType = ppViewSlideMaster
是否可以用OPPTAP或oPPTFile替换?我通读了一遍,它似乎匹配,但不起作用


希望这就是我的要求!我希望大多数人在这种情况下都会阅读并发抖……

层次结构是这样的:

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

Set oPPTPres = oPPTApp.Presentations(1) or
Set oPPTPres = oPPTApp.ActivePresentation
每个演示对象都有一个设计集合,代表演示中的不同母版,以便访问第一个设计的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

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

因此扩展了Steve的答案,我决定用一个新的框替换现有的框,以避免编辑现有形状的痛苦

我很幸运,我所做的自动化是为了创建新的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

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

您需要切换视图吗?我知道如果您是手动操作,您会这样做,但是如果您正在引用
ActivePresentation.SlideMaster.CustomLayouts(1)…
,那么您可能不需要这样做?不过我不知道,我从来没有给老师写信过。从PowerPoint到你需要记录的一切。使用适当的PowerPoint对象创建和引用。因此,您需要的不是Application.Windows(1).ViewType=ppViewNormal,而是opptap.Windows(1).ViewType。我还将opptap声明为PowerPoint.Application类型。确保您引用了PowerPoint对象库。@CLR-手动部分的唯一原因是确保选择了正确的主幻灯片,因为它有3张。如果我能找到一行代码来直接更新幻灯片上的方框,我很乐意这样做。我会研究的。手动查看幻灯片是最快的方法,然后按主幻灯片,这会将您带到幻灯片的主幻灯片。我的意思是,我不确定您是否需要找到以下行的Excel等效项:
Application.Windows(1)。ViewType=ppViewSlideMaster
Application.Windows(1)。ViewType=ppViewNormal
。祝你好运@RichHolton PowerPoint。应用程序似乎不是有效类型,我理解正确吗?Dim Opptap作为PowerPoint.ApplicationSteve,这是一个非常好的帮助,非常感谢。我想我差一点就搞定了。我使用的是oPPTPres.Slides(3).CustomLayout With.Shapes(“sourcelabel”).TextFrame.TextRange.Text=消息它完成了,没有出现错误(这是第一次),但框没有更新。我查看了文档,CustomLayout对象似乎是只读的,这是为什么?我将试着按照你的建议浏览设计对象。宾果。。。很好地完成了我的最后一篇文章,并与它一起运行。如果需要检索以前添加的形状(要在添加新形状之前进行编辑或删除),请查找标记。