2016-06-23 81 views
1

我想使用模板將數據從EXCEL複製到使用EXCEL VBA的POWERPOINT演示文稿。它在我使用顯式路徑時起作用。不過我想使用相對路徑來運行它,但它扔我下面的錯誤Excel VBA Mypresentation.ApplyTemplate與通用路徑文件

Sub PowerPoint() 

Dim rng As Excel.Range 
Dim PowerPointApp As PowerPoint.Application 
Dim myPresentation As PowerPoint.Presentation 
Dim mySlide As PowerPoint.Slide 
Dim myShapeRange As PowerPoint.Shape 
Dim Template As String 




'Copy Range from Excel 
    Set rng = Worksheets("Contact Page").Range("C2:O38") 


'Create an Instance of PowerPoint 
    On Error Resume Next 

    'Is PowerPoint already opened? 
     Set PowerPointApp = GetObject(class:="PowerPoint.Application") 

    'Clear the error between errors 
     Err.Clear 

    'If PowerPoint is not already open then open PowerPoint 
     If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application") 

    'Handle if the PowerPoint Application is not found 
     If Err.Number = 429 Then 
     MsgBox "PowerPoint could not be found, aborting." 
     Exit Sub 
     End If 

    On Error GoTo 0 

'Make PowerPoint Visible and Active 
    PowerPointApp.Visible = True 
    PowerPointApp.Activate 

'Create a New Presentation 

    Template = CurDir() 
    Template = Template & "\TEMPLATE3.potm" 
    Set myPresentation = PowerPointApp.Presentations.Add 
    myPresentation.ApplyTemplate (Template) 
    'myPresentation.ApplyTemplate ("C:\Users\Oriol\Documents\3mundi\Reporting\BR\New Model\TEMPLATE3.potm") 
    myPresentation.PageSetup.SlideSize = ppSlideSizeOnScreen 

Error

我應該怎麼辦?

回答

1

假設模板文件在Excel文件相同的文件夾,更改語句:

Template = CurDir() 

到:

Template = ThisWorkbook.Path 
+0

感謝菲利普,它作品。 –

0

是將Excel中爲您POTM相同的位置文件? CURDIR()返回Excel文件,目前保存在路徑所以,如果你POTM保存在子文件夾,你將要使用的子文件夾的路徑,以及:

Template = CurDir() 
Template = Template & "\SubFolder\TEMPLATE3.potm"