2015-10-15 90 views
0

我试图通过excel将模板应用于powerpoint。 PowerPoint模板通过插入 - >对象嵌入到我的Excel文件中。我已成功使用.applytemplate方法从文件应用模板,但我无法调整代码以引用嵌入的PowerPoint模板。我尝试使用OLEObject,但我担心这是不正确的。请查看下面的代码。将模板应用于Excel VBA中的powerpoint从嵌入式模板文件

Sub ppCreate() 

Dim myPP As PowerPoint.Application 
Dim myPres As PowerPoint.Presentation 
Dim activeSlide As PowerPoint.Slide 
Dim ppObj As OLEObject 

' Create instance of PowerPoint 
Set myPP = CreateObject("Powerpoint.Application") 

' For automation to work, PowerPoint must be visible 
myPP.Visible = True 


' Create a presentation 
Set myPres = myPP.Presentations.Add 

' Set slide view to Slide Only 
myPP.ActiveWindow.ViewType = ppViewSlide 

'Resize to 4:3 
myPres.PageSetup.SlideSize = 2 

'Add a slide 
Set activeSlide = myPres.Slides.Add(1, ppLayoutBlank) 

'Import Template 

Worksheets("CBRDATA").Select 
Set ppObj = ActiveSheet.OLEObjects("ppObj")  'NOT WORKING 
myPres.ApplyTemplate (ppObj)      'NOT WORKING 
myPres.ApplyTemplate "C:\CBR_TEMPLATE_COVER.potx" 'WORKING 
Worksheets("CBR").Select 

End Sub 

更新:

'Test if template exists in C:\ 
If Dir("C:\CBR_TEMPLATE_COVER.potx") = "" Then 
    'Open/Save the embedded template to C:\ 
    Set tempPP = CreateObject("Powerpoint.Application") 
    Worksheets("CBRDATA").OLEObjects("ppObj").Verb 0 
    tempPP.Visible = True 
    Set tempPres = tempPP.ActivePresentation 
    tempPres.SaveCopyAs ("C:\CBR_TEMPLATE_COVER.potx") 
    tempPres.Close 
Else: 
End If 

' Create instance of PowerPoint 
Set myPP = CreateObject("Powerpoint.Application") 

回答

1

这不起作用,因为ActiveSheet.OLEObjects("ppObj")是类型OLEObject,不PowerPoint.Presentation

Set ppObj = ActiveSheet.OLEObjects("ppObj")  'NOT WORKING 

当对象开放的POTX文件上手动双击(实际上它打开使用POTX为模板,一个新的空白PPTX),你的赋值语句上面没有做任何的是,它试图而是要将OLEObject放置在预期的演示文稿中,并且始终会失败。

那么,如何“打开”OLEObject呢? OLEObject有一个.Verb方法,下面将执行对象的默认动作,在嵌入包对象的情况下,通常是“打开”它们。

解决方案

'Import Template 
'## This should Open the template 
Worksheets("CBRDATA").OLEObjects("ppObj").Verb 0   

'## Assign the ActivePresentation to your ppObj variable 
Set ppObj = myPP.ActivePresentation 

编者按:嵌入式OLEObjects是众所周知的问题,并且可能不是故事的东西的理想场所像文档模板 :)

+0

大卫你好,谢谢你的回答。根据你的最终评论,我在想也许我应该先将嵌入式文档保存到文件中,然后从文件中应用模板。 – Citanaf

+0

我的最终评论只是一个建议,如果**您使用OLEObject作为“共享”和维护文档模板的方式,那可能不是最佳做法。我不会建议尝试使用VBA来完成你所提到的。如果您想从磁盘上打开POTX,那么您应该分发POTX,并从*已知或高度期待的位置*(如C:\ Users \%USERNAME%\ AppData \ Roaming \ Microsoft \ Templates \文档主题“,或允许用户使用”FileDialog“选择它。 –

+0

该文档是更大的仪表板的一部分,可根据用户选择创建标准化的Powerpoint。我更改了我的代码以测试C:\中是否存在模板,如果找不到它,它会创建一个新的PowerPoint并将其保存到该文件夹​​。在这种情况下,我的代码的其余部分不需要联系。上面提供的细节。再次感谢你的帮助 – Citanaf

相关问题