0
我有兩個問題。字VB - 錯誤'5941'和for循環
首先,我有錯誤「運行時錯誤」5941'請求的集合成員不存在。「在錯誤發生後選擇「結束」時,代碼將實際執行所期望的操作。我以爲我在Error 5941找到了解決方案,但它沒有幫助。他們建議在代碼中添加「Application.Templates.LoadBuildingBlocks」。
其次,我試圖加入 「textboxCounter」 到 「設置TBS()= UserForm1.Controls(」 TextBox_ 「):」 現在我不需要申報文本框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」)',但這是** NOT **您在前面的事件過程中分配給它們的名稱:UserForm1.Controls.Add (「Forms.TextBox.1」,「Test」&textboxCounter,True)'...... –
FWIW我不認爲LoadBuildingBlocks與這個問題有任何關係。就錯誤消息而言,錯誤消息是相當明確的:YOu試圖按名稱引用一個對象,該名稱不存在... –