VBA打开powerpoint以新名称保存

时间:2018-05-09 10:03:28

标签: vba excel-vba excel

我试图简单地打开一个powerpoint,然后使用新名称SaveAs。

我得到"编译错误:未找到方法或数据成员"

Public Sub OpenPPTfinalOpp()
    Dim templatePath As String
    Set PPT = New PowerPoint.Application


    templatePath = ThisWorkbook.Sheets("Automation").Range("D20")
    'templatePath = "C:\Users\[userName]\Desktop\test\Weekly Pack Update - Template.pptx"

    PPT.Visible = False
    Set PPT_pres = PPT.Presentations.Open(Filename:=templatePath)

    Set PPT_pres = PPT.Presentations.SaveAs(Filename:="C:\Users\[userName]\Desktop\test\Weekly Pack Update - Final.pptx")


End Sub

代码在没有SaveAs行的情况下运行,理想情况下我可以在不打开powerpoint的情况下运行它,因为这只是将其附加到电子邮件之前的第一步。

由于

1 个答案:

答案 0 :(得分:1)

从对问题的评论升级:
要在不打开Powerpoint的情况下执行此操作,请为任何文件类型:FileCopy ThisWorkbook.Sheets("Automation").Range("D20"), "C:\Users\[userName]\Desktop\test\Weekly Pack Update - Final.pptx"

为了更安全地做到这一点:

Public Function SafeCopy(Source As String, Destination As String) As Boolean
    SafeCopy = False
    'Source does not exist
    If Len(Dir(Source)) < 2 Then Exit Function
    'Clear destination file if it already exists
    If Len(Dir(Destination)) > 1 Then
        Kill Destination
        'Cannot clear destination
        If Len(Dir(Destination)) > 1 Then Exit Function
    End If
    'Do the actual copy
    FileCopy Source, Destination
    'Report on success/failure
    SaveCopy = (Len(Dir(Destination)) > 1)
End Function