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.
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
Comment