Word VBA - Run-Time error '5941'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnb76
    New Member
    • May 2014
    • 3

    Word VBA - Run-Time error '5941'

    I have this code I wrote in Word VBA working with Bookmarks. In the code I choose the number of textboxes to fill out. Right now I have to choose 10 until I figure out how to change the set textboxes to equal the number I enter but thats for a later issue.

    My question is about the error (Run-Time error '5941' "The requested member of the collection does not exist"). The debug stops at the line (If .Bookmarks("hre f" & i + 1).Range = ".jpg" Then) I have tried everything I can think of to remedy this but nothing is helping. When ending the program it actually completes with the desired outcome.

    If I remove the stated if statement completely then it gives a script out of range. I am at my wits end. I hope someone can help me with this.
    Code:
    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()
    Dim Textbox As Object
    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
    Last edited by NeoPa; May 30 '14, 06:23 PM. Reason: Added tags and tidied multiple blank lines.
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1288

    #2
    You say
    If I remove the stated if statement completely then it gives a script out of range.
    . I suppose you mean if you delete the entire block of code that begins with the IF.

    What is the variable AMOUNT? I see that being used a lot but never set. Your statement
    Code:
    For i = 0 To Amount
    looks suspicious to me. I wonder if it should instead be
    Code:
    For i = 0 To Amount-1
    It could be your references to TBs(i) in that block of code are failing on the last iteration attempt (i=AMOUNT).

    Jim

    Comment

    • johnb76
      New Member
      • May 2014
      • 3

      #3
      "Amount" is a textbox on my Userform where I type in the number of textboxes I want. Sorry I should have put in comments.

      Comment

      • johnb76
        New Member
        • May 2014
        • 3

        #4
        jimatqsi you are a genius. I should have noticed that myself. All errors are gone. Thank you very much.
        Last edited by NeoPa; May 30 '14, 06:24 PM. Reason: New question removed. Please post as new thread.

        Comment

        • jimatqsi
          Moderator Top Contributor
          • Oct 2006
          • 1288

          #5
          Not a genius, just experienced in some things. I know any time you start looping at zero you usually stop at something-1.

          Very glad you got that worked.

          Jim

          Comment

          Working...