Word VB - error '5941' and for-loop

回眸只為那壹抹淺笑 提交于 2019-12-10 12:23:49

问题


I have two questions.

First, I have the error "Run-time error '5941' The requested member of the collection does not exist." When selecting "End" after the error the code will actually do what's intended. I thought I found a solution at Error 5941 but it doesn't help. They suggested adding "Application.Templates.LoadBuildingBlocks" to the code.

Second, I am attempting to add "textboxCounter" to "Set TBs(0) = UserForm1.Controls("TextBox_1"):" So I don't need to declare textboxes 1 - 10 so I can have as many test boxes I need. I've tried just placing it in but that is not working. Not sure what I am doing wrong.

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:


I have discovered what I was doing wrong. I forgot when setting the textboxes they started at Zero. I put an Amount - 1 in my For i statement and it is now working without error. It was searching for another field on my document that didn't exist.



来源:https://stackoverflow.com/questions/23846550/word-vb-error-5941-and-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!