Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

徵兆

當您執行使用 Automation 來控制 Microsoft Word 的 Microsoft Visual Basic 程式碼時,您可能會收到下列其中一個錯誤訊息:

錯誤訊息 1

執行時間錯誤 (800706ba 2147023174)
自動化錯誤

錯誤訊息 2

執行時間錯誤 '462':遠端伺服器電腦不存在或無法使用

原因

Visual Basic 已建立Word的參照,因為程式程式碼呼叫Word物件、方法或屬性,但不符合Word物件變數的資格。 Visual Basic 不會在您結束程式之前釋出此參照。 當程式碼執行多次時,此錯誤參照會干擾自動化程式碼。

解決方案

修改程式碼,讓每個呼叫Word物件、方法或屬性符合適用物件變數的資格。

狀態

產生此錯誤是系統刻意為之。

其他相關資訊

若要自動化Word,您必須建立通常參照 Word Application 或 Document 物件的物件變數。 其他物件變數則可設定為參照選取範圍、範圍或Word物件模型中的其他物件。 當您撰寫程式碼以使用Word物件、方法或屬性時,您應一律在呼叫前面加上適當的物件變數。 如果沒有,Visual Basic 會使用隱藏的全域變數參照,它會設定為目前執行中的實例。 如果Word關機,或是已宣告的物件變數已發行,隱藏的全域變數現在會參照不正確 (損毀) 物件。 再次執行自動化程式碼時,呼叫此隱藏的物件變數將會失敗,並出現上述錯誤。


下列步驟說明如何重現此問題,以及如何修正問題。
 

重現行為的步驟

  1. 在 Visual Basic 中開始新的標準 EXE 專案。 表單1 預設為建立。

  2. 按一下 [專案] 功能表中的 [參考資料],然後按一下下列其中一個選項:

    • 若是 Office Word 2007,請按一下[Microsoft Word 12.0 物件程式庫]

    • 若是 Word 2003,請按一下[Microsoft Word 11.0 物件程式庫]

    • 若是 Word 2003,請按一下[Microsoft Word 10.0 物件程式庫]

    • 若是 Word 2000,請按一下[Microsoft Word 9.0 物件程式庫]

    • 若是 Word 97,請按一下[Microsoft Word 8.0 物件程式庫]

  3. 將 CommandButton 放在 Form1 上。

  4. 將下列程式碼複製到表單1 的程式碼視窗:

          Option Explicit
    
          Private Sub Command1_Click()
             Dim oWord As Word.Application
             Dim oDoc As Word.Document
             Dim oRange as Word.Range
    
             Set oWord = CreateObject("Word.Application")
             With oWord
                 .Visible = True
                 .Activate
                 .WindowState = wdWindowStateNormal
             End With
    
             Set oDoc = oWord.Documents.Add
             MsgBox "Document open", vbMsgBoxSetForeground
             With oDoc
                 .PageSetup.LeftMargin = InchesToPoints(1.25)
             End With
    
             ' This example inserts text at the end of section one.
             Set oRange = ActiveDocument.Sections(1).Range
             With oRange
                 .MoveEnd Unit:=wdCharacter, Count:= -1
                 .Collapse Direction:=wdCollapseEnd
                 .InsertParagraphAfter
                 .InsertAfter "End of section."
             End With
    
             With oDoc
                 .Saved = True
             End With
    
             Set oRange = Nothing
             Set oDoc = Nothing
             oWord.Quit
             Set oWord = Nothing
          End Sub
    
  5. 在 [ 執行] 功能表上,按一下 [ 開始 ] 或按 F5 鍵來啟動程式。

  6. 按一下 CommandButton。 不會發生錯誤。 不過,已建立Word參照,但尚未釋出。

  7. 再次按一下 CommandButton,並注意您收到先前描述的錯誤。

    注意:發生錯誤的原因是程式碼參照的英吋ToPoints 方法,在呼叫之前沒有 oWord 物件變數。

  8. 停止專案並變更下列行:

    .PageSetup.LeftMargin = InchesToPoints(1.25)
    

    -自-

     

    .PageSetup.LeftMargin = oWord.InchesToPoints(1.25)
    
  9. 再次執行程式。 然後,按一下 CommandButton。 不會發生錯誤。

  10. 再按一下 CommandButton,並注意您收到錯誤訊息。

    注意:發生錯誤的原因是程式碼參照 ActiveDocument Section one 的 Range 物件,而不會在呼叫前面加上 oWord 物件變數。

  11. 停止專案並變更下列行:

    Set oRange = ActiveDocument.Sections(1).Range
    

    -自-
     

    Set oRange = oWord.ActiveDocument.Sections(1).Range
    
  12. 再次執行程式。 請注意,您可以多次執行程式碼,而不會發生錯誤。

建立 Visual Basic 專案時,若您的專案有 Microsoft Word Word物件程式庫的參照,Word物件模型的物件、方法和屬性的範例程式碼可從Word說明檔案取得。 當游標停留在程式碼中的按鍵單字上方時,您會按 F1 鍵,看到任何適用的解說文字。


說明主題中的範例程式碼會是 Microsoft Word Visual Basic for Applications 程式碼。 它不會顯示 Visual Basic 程式碼需要的物件參照。 您必須視需要新增辨識符號。

參考

如需詳細資訊,請參閱 Microsoft 知識庫中的下列文章:

178510 PRB:Excel Automation Method of Object '_Global'Failed

如需 Office 應用程式自動化的其他資訊,請按一下下方的文章編號以檢視 Microsoft 知識庫中的文章:

Word物件模型概觀

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×