VBA 运行时错误 424:Word 文档另存为需要对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25936076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Run-time error 424: Object required with Word document SaveAs
提问by Casz146
I have an issue with this piece of code I'm writing. When I run the macro it gives me an "Run-time error '424': Object required" on the
我正在编写的这段代码有问题。当我运行宏时,它给我一个“运行时错误'424':需要对象”
ActiveDocument.SaveAs filename:="C:\test\test.docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
line.
线。
I suppose this has something to do with the word Document and the word window situation, but I don't know how to solve it.
我想这和文档这个词和窗口情况这个词有关系,但我不知道如何解决。
Any help would be greatly appreciated! (After the mentioned line I need to close the word document as well, maybe anyone could help with that too?)
任何帮助将不胜感激!(在提到的行之后,我还需要关闭 word 文档,也许有人也可以帮忙?)
Dim objword As Object
Dim objDoc As Object
Dim fNameAndPath As Variant
Dim fNameAndPath2 As Variant
fNameAndPath = "C:\test"
fNameAndPath2 = "C:\test2"
i = 2
While Not IsEmpty(Cells(i, 3)) ' or whevever you want to start
If Cells(i, 9) = "End of Probation Per" Then
Set objword = CreateObject("Word.Application")
objword.Visible = True
objword.Documents.Open (fNameAndPath)
objword.Activate
With objword.ActiveDocument
.Bookmarks("EmpName").Range.Text = Cells(i, 2).Value
.Bookmarks("EndDate").Range.Text = Cells(i, 11).Value
ActiveDocument.SaveAs filename:="C:\test\test.docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
End With
Else: Cells(i, 9).Font.Italic = True
End If
i = i + 1
Wend
End Sub
回答by Rory
You need to fully qualify the object:
您需要完全限定对象:
objWord.ActiveDocument.SaveAs filename:="C:\test\test.docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
Also note that if you haven't set a reference to the Word object library, the constant wdFormatXMLDocument
has no value, so you should define it:
另请注意,如果您尚未设置对 Word 对象库的引用,则该常量wdFormatXMLDocument
没有值,因此您应该定义它:
Const wdFormatXMLDocument As Long = 12
Re your last comment, you can include a cell value for the file name:
关于您的最后一条评论,您可以为文件名包含一个单元格值:
objWord.ActiveDocument.SaveAs filename:="C:\test\" & cells(i, 2).value & ".docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False