【发布时间】:2014-07-09 05:36:06
【问题描述】:
我有一个用户表单,它会自动创建标签和文本框。问题是创建后我不知道文本框 1 的名称。我以为它会是 TextBox1,但事实并非如此。我将如何将它们命名为 TextBox1 然后 TextBox2 等等?有问题的代码在“AddLine”下。
Sub CommandButton1_Click()
Dim TBs(9) As Object
Set TBs(0) = TextBox1: Set TBs(1) = TextBox2: Set TBs(2) = TextBox3
Set TBs(3) = TextBox4: Set TBs(4) = TextBox5: Set TBs(5) = TextBox6
Set TBs(6) = TextBox7: Set TBs(7) = TextBox8: Set TBs(8) = TextBox9
Set TBs(9) = TextBox10:
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
Private Sub AddLine_Click()
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
.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
【问题讨论】:
标签: vba ms-word automation userform