下面的此Excel宏应将所有.Docx转换为所选文件夹中的.Pdf
这是向我提供错误代码429的代码行,但是几个小时前,同一行代码仍在工作。
Documents.Open (filePath & currFile) 'Error Code 429
这里是完整的宏代码
Sub ConvertDocxInDirToPDF()
Dim filePath As String
Dim currFile As String
filePath = ActiveWorkbook.Path & "\"
MsgBox filePath
currFile = Dir(filePath & "*.docx")
Do While currFile <> ""
Documents.Open (filePath & currFile) 'Error Code 429
Documents(currFile).ExportAsFixedFormat _
OutputFileName:=filePath & Left(currFile, Len(currFile) - Len(".docx")) & ".pdf", _
ExportFormat:=17
Documents(currFile).Close
currFile = Dir()
Loop
Application.ScreenUpdating = True
End Sub
有没有一种简单的方法可以使此宏正常工作并修复此错误。
最诚挚的问候。
答案 0 :(得分:3)
Documents.Open
是Documents对象的一种方法,需要“ MS Word对象库”才能起作用,而无需明确地引用到单词对象:
这是什么意思?如果选中了Microsoft Word 1X.0
引用(VBE> Extras> Libraries),那么下面的代码就可以正常工作:
Sub TestMe()
Dim filePath As String
filePath = ThisWorkbook.Path & "\"
Dim currFile As String
currFile = Dir(filePath & "*.docx")
Dim wrdDoc As Object
Documents.Open filePath & currFile
End Sub
如果未引用“ MS Word对象库”,则在后期绑定时仍可以引用该对象。 (后期绑定为CreateObject("Word.Application")
):
Sub TestMe()
Dim filePath As String
filePath = ThisWorkbook.Path & "\"
Dim currFile As String
currFile = Dir(filePath & "*.docx")
Dim wrdApps As Object
Set wrdApps = CreateObject("Word.Application")
wrdApps.Documents.Open (filePath & currFile)
End Sub
如果需要,Documents.Open
可以返回一个文档对象:
Sub TestMe()
Dim filePath As String
filePath = ThisWorkbook.Path & "\"
Dim currFile As String
currFile = Dir(filePath & "*.docx")
Dim wrdApps As Object
Set wrdApps = CreateObject("Word.Application")
Dim wrdDoc As Object
Set wrdDoc = wrdApps.Documents.Open(filePath & currFile)
End Sub