Word VB - 错误'5941'和for循环

时间:2014-05-24 15:08:41

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

我有两个问题。

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

其次,我试图将“textboxCounter”添加到“设置TB( 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

1 个答案:

答案 0 :(得分:0)

我发现了我做错了什么。我忘了设置他们从零开始的文本框。我在For I语句中加了一个Amount - 1,它现在正在运行而没有错误。它正在我的文档中搜索另一个不存在的字段。