我正在使用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。 提前谢谢!
答案 0 :(得分: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