打开然后激活文档

huangapple go评论65阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月13日 23:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定