gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:26:30 24 4
gpt4 key购买 nike

作为 VBA 语言的新手,我已经研究了这个简单的任务几个小时,但我没有任何工作。我收到错误消息“此命令不可用,因为没有打开任何文档”,并且有一个 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 实例(第二个实例中没有打开任何文档)。 .

关于VBA "This Command is not Available because no document is open",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com