VBA:在Excel中如何将Word文档保存为PDF?

lztngnrs  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(352)

我有一些代码可以将Excel文件中的数据复制并粘贴到Word文档中,如何将该Word文档保存为PDF?
代码如下:

Sub RetailerGraphs()

Dim Location As String
Dim Detail As String

Worksheets("-Summary").Activate
Range("AE9").Select
While ActiveCell.Value <> ""
    ActiveCell.Offset(1, 0).Select
    Location = ActiveCell.Value
    Worksheets("Detail Summary").Activate
    Range("B7").Value = Location

    Dim objWord, objDoc As Object
    ActiveWindow.View = xlNormalView
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add

    Range("AH40").Select
    While ActiveCell <> ""
        ActiveCell.Offset(1, 0).Select
        Detail = ActiveCell.Value
        Range("B11").Value = Detail 
        Application.Wait (Now + TimeValue("0:00:05"))
        Range("A1:Z111").CopyPicture Appearance:=xlScreen, Format:=xlPicture
        objWord.Visible = True
        objWord.Selection.Paste
        objWord.Selection.TypeParagraph
        Set objSelection = objWord.Selection
        objSelection.InsertBreak (wdPageBreak)
        If ActiveCell.Value = "END" Then
             objWord.SaveAs2 "C:\Docs\MyDoc.pdf"
        End If
    Wend
    Worksheets("-Summary").Activate
Wend

End Sub

线内:

If ActiveCell.Value = "END" Then

End If

我尝试了以下代码,试图让它保存为PDF:

objWord.ExportAsFixedFormat OutputFileName:="C:\wordtest.pdf", _
  ExportFormat:=wdExportFormatPDF

 objWord.ExportAsFixedFormat OutputFileName:="C:\Documents and Settings\All Users\Desktop\YourFile.pdf", ExportFormat:=wdExportFormatPDF

 objDoc.ExportAsFixedFormat OutputFileName:="C:\Documents and Settings\All Users\Desktop\YourFile.pdf", ExportFormat:=wdExportFormatPDF

但是我在尝试将其导出为PDF的行上出现错误,例如。objWord.SaveAs2 "C:\Docs\MyDoc.pdf"

Run-time error '438':
Object doesn't support this property or method

有人能帮帮我吗?
注意:我已经确保在引用中勾选了“Microsoft Word 14.0 Object Library”,并且我将位置更改为必要的目录,而不是使用上面的目录。

eivnm1vs

eivnm1vs1#

ExportAsFixedFormat是Word.Document(objDoc)的方法,而不是Word.Application(objWord)的方法。所以呢

objDoc.ExportAsFixedFormat OutputFileName:="C:\Docs\File.pdf", ExportFormat:=wdExportFormatPDF

应该工作。
这也给予一个错误吗?哪个?

相关问题