【问题标题】:Multiple Word Doc to pdf convert多Word文档到pdf转换
【发布时间】:2018-02-08 05:25:53
【问题描述】:

我正在使用一个 vba 代码,它将文件夹中的多个 word 文档转换为单个 pdf 文件。

问题是我无法将word文件另存为pdf。

下面是代码:

        Sub convertword()


        Dim Filename As String
        Dim irow As Integer
        Dim jsObj As Object
        Dim NewFileName As String
        Dim objWord As Object
        Dim strDocName As String, strDocName1 As String, strDocName2 As String
        Dim strMyPath As String


        irow = 4
        Do While Cells(irow, 2) <> Empty
        Filename = Cells(irow, 2).Value
        NewFileName = Cells(irow, 3).Value

        Set objWord = CreateObject("word.Application")
        objWord.Visible = True

        objWord.Documents.Open Filename

        'Document.SaveAs Filename, wdFormatPDF

        'ActiveDocument.Visible = True

        ActiveDocument.ExportAsFixedFormat OutputFileName:=NewFileName, ExportFormat:=wdExportFormatPDF

        'ActiveDocument.Close

        irow = irow + 1
        Loop
        End Sub

ActiveDocument.ExportAsFixedFormat OutputFileName:=NewFileName, ExportFormat:=wdExportFormatPDF 行给出错误“此命令不可用,因为没有打开文档”。

我可以打开文档,但无法将文档另存为 pdf。 提前谢谢!

【问题讨论】:

    标签: vba excel pdf


    【解决方案1】:

    您正在尝试在没有参考的情况下使用 Excel 中的 Microsoft Word 代码。添加对 Microsoft Word 15.0 对象库的引用并尝试此代码

    'Set a Reference to Microsoft Word 15.0 Object Library
    Sub convertword()
        Dim irow As Integer
        Dim objWord As Word.Application
        Dim newdoc As Word.Document
        Set objWord = New Word.Application
        objWord.Visible = True
    
        irow = 4
        Do While Cells(irow, 2) <> Empty
            Set newdoc = objWord.Documents.Open(Cells(irow, 2).Value)
            newdoc.ExportAsFixedFormat OutputFileName:=Cells(irow, 3).Value, _
                ExportFormat:=wdExportFormatPDF
            newdoc.Close (False)
            irow = irow + 1
        Loop
        objWord.Quit
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 2011-11-10
      • 2012-08-07
      • 1970-01-01
      • 2011-03-02
      相关资源
      最近更新 更多