打开然后激活文档

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

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.

  1. ObjWord.Visible = True
  2. Dim docword As Object
  3. Dim OCC as ContentControl
  4. ObjWord.Activate
  5. Set docWord = ObjWord.Documents.Open(Filename)
  6. docWord.Activate-------Error
  7. For each oCC In ActiveDocument.ContentControls
  8. Select Case Occ.Title
  9. Case abc
  10. 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.

  1. ObjWord.Visible= True
  2. Dim docword As object
  3. Dim OCC as ContentControl
  4. ObjWord.Activate
  5. Set docWord = objWord.Documents.Open( Filename)
  6. docWord.Activate-------Error
  7. For each oCC In ActiveDocument.ContentControls
  8. Select Case Occ.Title
  9. Case abc
  10. Occ.Range.Text

答案1

得分: 0

以下是翻译好的部分:

"由于您已为打开的文档设置了一个变量,因此不应使用 ActiveDocument。无论新打开的文档是否被激活,您的代码都应该运行。

因此,假设您的代码不是从 Word 中运行的,它应该是:

  1. ObjWord.Visible= True
  2. Dim docWord As Document
  3. Dim OCC as ContentControl
  4. ObjWord.Activate
  5. Set docWord = objWord.Documents.Open(Filename)
  6. For each oCC In docWord.ContentControls
  7. Select Case Occ.Title
  8. Case abc
  9. 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:

  1. ObjWord.Visible= True
  2. Dim docWord As Document
  3. Dim OCC as ContentControl
  4. ObjWord.Activate
  5. Set docWord = objWord.Documents.Open(Filename)
  6. For each oCC In docWord.ContentControls
  7. Select Case Occ.Title
  8. Case abc
  9. 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:

确定