首页 > 解决方案 > VBA 运行时错误 5941 请求的集合成员不存在

问题描述

我有从 excel 文档获取信息的 word doc,但是在打开 word doc 时出现上述错误。我有很多 word 文档可以很好地使用,但有些我得到了上面的运行时错误。下面是我调试时突出显示的 VBA 代码:

Windows(ActiveDocument.Name).Activate

' add registry keys if necessary
    Dim wShell As Object

    Set wShell = CreateObject("WScript.Shell")
    On Error Resume Next
    gsKeyValue1 = wShell.RegRead(Key1)   ' initial value of key, save for restoring user's key
    gsKeyValue2 = wShell.RegRead(Key2)   ' initial value of key, save for restoring user's key
    On Error GoTo 0
    If gsKeyValue1 = "" Or gsKeyValue1 = "0" Then wShell.RegWrite Key1, 1, "REG_DWORD"
    If gsKeyValue2 = "" Or gsKeyValue2 = "0" Then wShell.RegWrite Key2, 1, "REG_DWORD"

    ' close Document1
    On Error Resume Next
    Documents("Document1").Close
    On Error GoTo 0

    Initialize

    Windows(ActiveDocument.Name).Activate
    Application.WindowState = wdWindowStateNormal
    Application.Left = 10
    Application.Top = 20
    Application.Width = 920
    Application.Height = 680

    If MsgBox("Would you like to update the links to the Excel spreadsheet? (This is only necessary if the data has recently been updated.", vbYesNo) = vbYes Then
        Macro1_UpdateLinks
    End If


End Sub

我该如何纠正这个错误。

标签: excelvbams-wordruntime-error

解决方案


推荐阅读