多个Word Doc到pdf转换 [英] Multiple Word Doc to pdf convert
问题描述
我正在使用vba代码,该代码会将文件夹中的多个word文档转换为单个pdf文件.
I was working with a vba code that will convert multiple word docs in a folder to individual pdf files.
问题是我无法将Word文件另存为pdf.
The problem is that I am not able to save the word file as 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
行显示错误消息,因为此命令不可用,因为未打开任何文档".
The line ActiveDocument.ExportAsFixedFormat OutputFileName:=NewFileName, ExportFormat:=wdExportFormatPDF
is giving error as "This command is not available because no document is open".
我能够打开文档,但无法将文档另存为pdf. 预先谢谢你!
I am able to open the doc but just not able to save the doc as pdf. Thank you in advance!
推荐答案
您正试图在没有参考的情况下使用Excel中的Microsoft Word代码.添加对 Microsoft Word 15.0对象库的引用,然后尝试使用此代码
You are trying to use Microsoft Word code from Excel without a reference. Add a reference to Microsoft Word 15.0 Object Library and try this code
'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
这篇关于多个Word Doc到pdf转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!