在 Excel VBA 中引用 PowerPoint 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43120959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Referencing PowerPoint Object in Excel VBA
提问by Chris
Long story short - I am trying to pass a string to PowerPoint from PowerShell to update a textbox on a master slide. I can do this from within PowerPoint vba, but it is completely static. As soon as I try to pass data through PowerShell to PowerPoint, I get an error that I have been unable to solve. (see here)
长话短说 - 我试图将一个字符串从 PowerShell 传递给 PowerPoint,以更新母版幻灯片上的文本框。我可以在 PowerPoint vba 中执行此操作,但它是完全静态的。一旦我尝试通过 PowerShell 将数据传递到 PowerPoint,我就会收到一个我无法解决的错误。(见这里)
So instead I am attempting to do it through an Excel proxy! I already use Excel VBA to update content on PowerPoint slides, so thought this would be suitable. I currently have shapes set up in Excel, data is updated, and then this is pasted in to a PowerPoint deck, saved and closed.
所以相反,我试图通过 Excel 代理来完成它!我已经使用 Excel VBA 来更新 PowerPoint 幻灯片上的内容,所以认为这很合适。我目前在 Excel 中设置了形状,更新了数据,然后将其粘贴到 PowerPoint 幻灯片中,保存并关闭。
I need to now extend this Excel macro with functionality that updates the textbox on the master slide. I need to translate "PowerPoint VBA" to "Excel VBA for PowerPoint", what a horrible sentence... I digress:
我现在需要使用更新母版幻灯片上的文本框的功能来扩展这个 Excel 宏。我需要将“PowerPoint VBA”翻译成“Excel VBA for PowerPoint”,多么可怕的一句话......我离题了:
PowerPoint VBA
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
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
I need to be able to complete the same steps as in the PowerPoint VBA but in the Excel macro. I'm having issues finding the right names for it though. So where it says
我需要能够完成与 PowerPoint VBA 中相同的步骤,但在 Excel 宏中。不过,我在为它找到正确的名称时遇到了问题。所以它说的地方
Application.Windows(1).ViewType = ppViewSlideMaster
Can this be replaced with oPPTApp or oPPTFile? I've read through MSDN Docsand it seems to match but doesn't work.
这可以用 oPPTApp 或 oPPTFile 代替吗?我已经阅读了MSDN Docs,它似乎匹配但不起作用。
Hopefully that's conveyed my ask! I expect most people to read and shudder at such a situation...
希望这传达了我的要求!我希望大多数人在这种情况下阅读并不寒而栗......
回答by Steve Rindsberg
The hierarchy's like this:
层次结构是这样的:
To access a presentation open in PPT, you'd use e.g.
要访问在 PPT 中打开的演示文稿,您可以使用例如
Set oPPTPres = oPPTApp.Presentations(1) or
Set oPPTPres = oPPTApp.ActivePresentation
Each presentation object has a Designs collection representing the different masters in the presentation, so to access SlideMaster of the first design,
每个演示文稿对象都有一个 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
Each Design has a collection of custom layouts (the thumbnails you see indented beneath the main master when in Slide Master view). They can be similarly accessed.
每个设计都有一组自定义布局(在幻灯片母版视图中,您在主母版下方看到缩进的缩略图)。它们可以类似地访问。
回答by Chris
So extending Steve's answer, I decided to just replace the existing box with a new one, to save the pain of trying to edit the existing shape.
所以扩展史蒂夫的答案,我决定用一个新的盒子替换现有的盒子,以节省尝试编辑现有形状的痛苦。
I'm fortunate that the automation I am doing is for the creation of new PowerPoint decks, so I'm able to just place these new shapes on them as I create multiple decks from one blank canvas.
我很幸运,我正在做的自动化是为了创建新的 PowerPoint 幻灯片,所以当我从一个空白画布创建多个幻灯片时,我可以将这些新形状放在它们上面。
The code I ended up with to get the same formatting is:
我最终获得相同格式的代码是:
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
Fantastic! Thank you to everyone who contributed.
极好的!感谢所有做出贡献的人。