VBA "This Command is not Available because no document is open"

VBA "This Command is not Available because no document is open"

作为 VBA 语言的新手,我已经研究了这个简单的任务几个小时,但我没有任何工作。我收到了错误 "This Command is not Available because no document is open",并且有一个 word 文档正在写。这里发生了什么?感谢您的帮助。

Sub excel_to_word()
Dim wapp As Word.Application
Dim wdoc As Word.Document

Set wapp = CreateObject("word.application")
wapp.Visible = True


Set wdoc = wapp.Documents.Add

wdoc.Content.InsertAfter Range("B5")

Application.Wait (Now + TimeValue("0:00:01"))


'CreateObject("Word.Application").ChangeFileOpenDirectory "C:\USERS\JOSEPH\DESKTOP\"
 Dim NEWPATH As String
 NEWPATH = "C:\USERS\JOSEPH\DESKTOP"


   CreateObject("Word.Application").ActiveDocument.SaveAs2 Filename:="joseph.docx",       FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", addtorecentfiles:=False, savenativepictureformat:=False, SaveFormsData:=False, saveasaoceletter:=False, CompatibilityMode:=14



End Sub
CreateObject("Word.Application").ActiveDocument.SaveAs2 Filename:="joseph.docx" 'etc

应该是

wdoc.SaveAs2 Filename:="joseph.docx" 'etc

当您第二次调用 CreateObject 时,您正在创建一个全新的 Word 实例(第二个实例中没有打开任何文档)。 .