英文:
How do I open then activate a document
问题
My VBA is no longer supporting the code that was working before.
When I tried Objword.Documents.Open and then docword.Activate, the document is opened but I get
4248 Run time error, "the command is not available because no document is open".
VBA macro references has the MS Office 16 Object Library selected.
ObjWord.Visible = True
Dim docword As Object
Dim OCC as ContentControl
ObjWord.Activate
Set docWord = ObjWord.Documents.Open(Filename)
docWord.Activate-------Error
For each oCC In ActiveDocument.ContentControls
Select Case Occ.Title
Case abc
Occ.Range.Text
英文:
My VBA is no longer supporting the code that was working before.
When I tried Objword.Documents.Open and then docword.Activate, the document is opened but I get
>4248 Run time error, "the command is not available because no document is open".
VBA macro references has the MS Office 16 Object Library selected.
ObjWord.Visible= True
Dim docword As object
Dim OCC as ContentControl
ObjWord.Activate
Set docWord = objWord.Documents.Open( Filename)
docWord.Activate-------Error
For each oCC In ActiveDocument.ContentControls
Select Case Occ.Title
Case abc
Occ.Range.Text
答案1
得分: 0
以下是翻译好的部分:
"由于您已为打开的文档设置了一个变量,因此不应使用 ActiveDocument。无论新打开的文档是否被激活,您的代码都应该运行。
因此,假设您的代码不是从 Word 中运行的,它应该是:
ObjWord.Visible= True
Dim docWord As Document
Dim OCC as ContentControl
ObjWord.Activate
Set docWord = objWord.Documents.Open(Filename)
For each oCC In docWord.ContentControls
Select Case Occ.Title
Case abc
Occ.Range.Text
请注意,这是翻译的代码部分。
英文:
As you have set a variable for the document you have opened you should not be using ActiveDocument. Your code should run regardless of whether the newly opened document is activated.
So, assuming your code is not being run from Word it should be:
ObjWord.Visible= True
Dim docWord As Document
Dim OCC as ContentControl
ObjWord.Activate
Set docWord = objWord.Documents.Open(Filename)
For each oCC In docWord.ContentControls
Select Case Occ.Title
Case abc
Occ.Range.Text
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论