Excel VBA Mypresentation.ApplyTemplate 带有通用路径文件

Excel VBA Mypresentation.ApplyTemplate with generic path file

我想使用模板将数据从 EXCEL 复制到 EXCEL VBA 的 POWERPOINT 演示文稿中。当我使用显式路径时它会起作用。但是我想使用 运行 它的相对路径,但它抛出以下错误

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\Documentsmundi\Reporting\BR\New Model\TEMPLATE3.potm")
  myPresentation.PageSetup.SlideSize = ppSlideSizeOnScreen

Error

我该怎么办?

假设模板文件与Excel文件在同一个文件夹中,更改语句:

Template = CurDir()

至:

Template = ThisWorkbook.Path

Excel 文件是否与您的 potm 位于相同的位置? CurDir() returns 当前保存 excel 文件的路径。因此,如果您的 potm 保存在子文件夹中,您也需要在路径中使用该子文件夹:

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