【问题标题】:Word VB - error '5941' and for-loopWord VB - 错误 '5941' 和 for 循环
【发布时间】:2014-07-13 20:03:33
【问题描述】:

我有两个问题。

首先,我有错误“运行时错误'5941'请求的集合成员不存在。”在错误后选择“结束”时,代码实际上会执行预期的操作。我以为我在Error 5941 找到了解决方案,但它没有帮助。他们建议在代码中添加“Application.Templates.LoadBuildingBlocks”。

其次,我正在尝试将“textboxCounter”添加到“Set TBs(0) = UserForm1.Controls("TextBox_1"):”所以我没有不需要声明文本框 1 - 10,这样我就可以拥有我需要的尽可能多的测试框。我试过把它放进去,但这不起作用。不知道我做错了什么。

Option Explicit    


Private Sub AddLine_Click()

Application.Templates.LoadBuildingBlocks

Dim theTextbox As Object
Dim textboxCounter As Long

For textboxCounter = 1 To Amount
    Set theTextbox = UserForm1.Controls.Add("Forms.TextBox.1", "Test" & textboxCounter, True)
    With theTextbox
        .Name = "TextBox_" & textboxCounter
        .Width = 200
        .Left = 70
        .Top = 30 * textboxCounter
    End With


Next

Dim theLabel As Object
Dim labelCounter As Long

For labelCounter = 1 To Amount
    Set theLabel = UserForm1.Controls.Add("Forms.Label.1", "Test" & labelCounter, True)
    With theLabel
        .Caption = "Image" & labelCounter
        .Left = 20
        .Width = 50
        .Top = 30 * labelCounter
    End With

    With UserForm1
        .Height = Amount * 30 + 100
    End With

    With CommandButton1
        .Top = Amount * 30 + 40
    End With

    With CommandButton2
        .Top = Amount * 30 + 40
    End With


Next


End Sub

Sub CommandButton1_Click()

Application.Templates.LoadBuildingBlocks


Dim Textbox1 As Object
Dim Textbox2 As Object
Dim Textbox3 As Object
Dim Textbox4 As Object
Dim Textbox5 As Object
Dim Textbox6 As Object
Dim Textbox7 As Object
Dim Textbox8 As Object
Dim Textbox9 As Object
Dim Textbox10 As Object

Dim i

Dim TBs(9) As Object
Set TBs(0) = UserForm1.Controls("TextBox_1"): Set TBs(1) = UserForm1.Controls("TextBox_2"): Set TBs(2) = UserForm1.Controls("TextBox_3")
Set TBs(3) = UserForm1.Controls("TextBox_4"): Set TBs(4) = UserForm1.Controls("TextBox_5"): Set TBs(5) = UserForm1.Controls("TextBox_6")
Set TBs(6) = UserForm1.Controls("TextBox_7"): Set TBs(7) = UserForm1.Controls("TextBox_8"): Set TBs(8) = UserForm1.Controls("TextBox_9")
Set TBs(9) = UserForm1.Controls("TextBox_10"):

For i = 0 To Amount
    With ActiveDocument
        If .Bookmarks("href" & i + 1).Range = ".jpg" Then
            .Bookmarks("href" & i + 1).Range _
            .InsertBefore TBs(i)
            .Bookmarks("src" & i + 1).Range _
            .InsertBefore TBs(i)
            .Bookmarks("alt" & i + 1).Range _
            .InsertBefore TBs(i)
        End If
    End With
Next

UserForm1.Hide

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    With Selection.Find
        .Text = ".jpg "
        .Replacement.Text = ".jpg"

        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.HomeKey Unit:=wdLine
    Selection.Find.Execute Replace:=wdReplaceAll



        Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    With Selection.Find
        .Text = "/ "
        .Replacement.Text = "/"

        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.HomeKey Unit:=wdLine
    Selection.Find.Execute Replace:=wdReplaceAll



    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    With Selection.Find
        .Text = ".jpg.jpg"
        .Replacement.Text = ".jpg"

        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.HomeKey Unit:=wdLine
    Selection.Find.Execute Replace:=wdReplaceAll

End Sub

【问题讨论】:

  • 在一个过程中,您正在调用对象UserForm1.Controls("TextBox_1"),但这不是您在前面的事件过程中分配给它们的名称:UserForm1.Controls.Add("Forms.TextBox.1", "Test" & textboxCounter, True).. .
  • FWIW 我不认为 LoadBuildingBlocks 与这个问题有任何关系。就错误消息而言,错误消息相当明确:您正在尝试按名称引用一个对象,该名称不存在...

标签: vba for-loop ms-word runtime-error


【解决方案1】:

我发现我做错了什么。我忘记了设置从零开始的文本框时。我在我的 For i 语句中输入了 Amount - 1,它现在可以正常工作。它正在搜索我的文档中不存在的另一个字段。

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 2012-01-03
    • 2017-01-03
    • 1970-01-01
    • 2016-08-15
    • 2013-06-15
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多