簡體   English   中英

將多個Word文檔轉換為pdf

[英]Multiple Word Doc to pdf convert

我正在使用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。 先感謝您!

您正在嘗試使用沒有參考的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM