在PowerPoint宏中引用Excel单元格作为文件名

时间:2016-11-30 09:18:30

标签: excel vba excel-vba powerpoint powerpoint-vba

我无法使用Excel VBA中的SaveAsFixedFormat将PowerPoint文件导出为PDF。我已经从Excel VBA中使用预设功能点启动了一个宏,它直接从PowerPoint中将表示导出为pdf。

有没有办法在PowerPoint中运行的这个宏中的Excel文件中引用单元格来获取文件名?

Sub pppdf()

ActivePresentation.ExportAsFixedFormat "M:\random\test.pdf", 32

End Sub

我可以将PowerPoint文件保存为Excel中的.pptx,并使用不同的文件名和路径,但现在我想在导出为pdf的PowerPoint宏中引用相同的路径和文件名。

最后,我希望代码看起来有点像这样,但这显然需要一些工作才能从PowerPoint中运行:

Dim FName           As String
Dim FPath           As String

FPath = Range("SavingPath").Value
FName = Sheets("randomworksheet").Range("A1").Text

ActivePresentation.ExportAsFixedFormat FPath & FName & " Development" & ".pdf", 32

此PowerPoint宏将从Excel启动,执行此操作时,PowerPoint文件和Excel工作簿和工作表都将打开。

2 个答案:

答案 0 :(得分:0)

您直接从Excel VBE使用ExportAsFixedFormat会遇到什么问题?根据{{​​3}}(似乎不正确)和PowerPoint VBE IntelliSense,第二个参数FixedFormatType只能是两个值中的一个:

ExportAsFixedFormat(Path, FixedFormatType, Intent, FrameSlides, _
                    HandoutOrder, OutputType, PrintHiddenSlides, PrintRange, _
                    RangeType, SlideShowName, IncludeDocProperties, KeepIRMSettings)

FixedFormatType:

ppFixedFormatTypePDF = 2
ppFixedFormatTypeXPS = 1

答案 1 :(得分:0)

如果大部分代码都在Excel中,为什么不打开演示文稿并将其另存为Excel格式?

Sub SavePPTXasPDF()

    Dim PPT As Object
    Dim PP As Object

    Set PPT = CreatePPT
    Set PP = PPT.Presentations.Open("<FullPathToPresentation>")

    PP.SaveAs ThisWorkbook.Path & Application.PathSeparator & "ABC", 32 'ppSaveAsPDF

End Sub


Public Function CreatePPT(Optional bVisible As Boolean = True) As Object

    Dim oTmpPPT As Object

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Defer error trapping in case Powerpoint is not running. '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    On Error Resume Next
    Set oTmpPPT = GetObject(, "Powerpoint.Application")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'If an error occurs then create an instance of Powerpoint. '
    'Reinstate error handling.                                 '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If Err.Number <> 0 Then
        Err.Clear
        On Error GoTo ERROR_HANDLER
        Set oTmpPPT = CreateObject("Powerpoint.Application")
    End If

    oTmpPPT.Visible = bVisible
    Set CreatePPT = oTmpPPT

    On Error GoTo 0
    Exit Function

ERROR_HANDLER:
    Select Case Err.Number

        Case Else
            MsgBox "Error " & Err.Number & vbCr & _
                " (" & Err.Description & ") in procedure CreatePPT."
            Err.Clear
    End Select

End Function

或者如果您想在Powerpoint中运行代码:

Public Sub Test()

    Dim oXL As Object
    Dim oWB As Object
    Dim FName           As String
    Dim FPath           As String

    Set oXL = CreateXL
    Set oWB = oXL.workbooks.Open("<Path&FileName>")

    'Or if Workbook is already open:
    'Set oWB = oXL.workbooks("<FileName>")

    FPath = oWB.worksheets("Sheet1").Range("A1")
    FName = oWB.worksheets("Sheet1").Range("A3")

    ActivePresentation.ExportAsFixedFormat FPath & FName & " Development" & ".pdf", 32

End Sub

Public Function CreateXL(Optional bVisible As Boolean = True) As Object

    Dim oTmpXL As Object

    '''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Defer error trapping in case Excel is not running. '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''
    On Error Resume Next
    Set oTmpXL = GetObject(, "Excel.Application")

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'If an error occurs then create an instance of Excel. '
    'Reinstate error handling.                            '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If Err.Number <> 0 Then
        Err.Clear
        On Error GoTo ERROR_HANDLER
        Set oTmpXL = CreateObject("Excel.Application")
    End If

    oTmpXL.Visible = bVisible
    Set CreateXL = oTmpXL

    On Error GoTo 0
    Exit Function

ERROR_HANDLER:
    Select Case Err.Number

        Case Else
            MsgBox "Error " & Err.Number & vbCr & _
                " (" & Err.Description & ") in procedure CreateXL."
            Err.Clear
    End Select

End Function

或者您可以按照要求从Excel中打开演示文稿并执行演示文稿中存储的代码:

Sub SavePPTXasPDF()

    Dim PPT As Object
    Dim PP As Object

    Set PPT = CreatePPT
    Set PP = PPT.Presentations.Open("<FullPath>")
    PPT.Run PP.Name & "!Test"

End Sub  

这将使用Test宏并使用我上面示例中当前注释掉的Set oWB = oXL.workbooks("<FileName>")代码行。