Word VB文本框的重复代码

时间:2014-05-14 21:38:05

标签: vbscript ms-word code-duplication

我目前有10个文本框,每个代码几乎相同。我不能输入一个数字并让它创建那么多盒子。我做了很多研究,但我找不到一个简单的方法来做到这一点。有什么想法吗?它必须复制我的Userform和代码中的文本框。我在考虑复制和粘贴或某种循环。

此代码将文件名放入word doc,其中包含带有书签的文本""

Private Sub CommandButton1_Click()

With ActiveDocument
If .Bookmarks("href1").Range = ".jpg" Then
    .Bookmarks("href1").Range _
    .InsertBefore TextBox1
    .Bookmarks("src1").Range _
    .InsertBefore TextBox1
    .Bookmarks("alt1").Range _
    .InsertBefore TextBox1
 End If
End With

With ActiveDocument
If .Bookmarks("href2").Range = ".jpg" Then
    .Bookmarks("href2").Range _
    .InsertBefore TextBox2
    .Bookmarks("src2").Range _
    .InsertBefore TextBox2
    .Bookmarks("alt2").Range _
    .InsertBefore TextBox2
 End If
End With

With ActiveDocument
If .Bookmarks("href3").Range = ".jpg" Then
    .Bookmarks("href3").Range _
    .InsertBefore TextBox3
    .Bookmarks("src3").Range _
    .InsertBefore TextBox3
    .Bookmarks("alt3").Range _
    .InsertBefore TextBox3
 End If
End With

With ActiveDocument
If .Bookmarks("href4").Range = ".jpg" Then
    .Bookmarks("href4").Range _
    .InsertBefore TextBox4
    .Bookmarks("src4").Range _
    .InsertBefore TextBox4
    .Bookmarks("alt4").Range _
    .InsertBefore TextBox4
 End If
End With

With ActiveDocument
If .Bookmarks("href5").Range = ".jpg" Then
    .Bookmarks("href5").Range _
    .InsertBefore TextBox5
    .Bookmarks("src5").Range _
    .InsertBefore TextBox5
    .Bookmarks("alt5").Range _
    .InsertBefore TextBox5
 End If
End With

With ActiveDocument
If .Bookmarks("href6").Range = ".jpg" Then
    .Bookmarks("href6").Range _
    .InsertBefore TextBox6
    .Bookmarks("src6").Range _
    .InsertBefore TextBox6
    .Bookmarks("alt6").Range _
    .InsertBefore TextBox6
 End If
End With

With ActiveDocument
If .Bookmarks("href7").Range = ".jpg" Then
    .Bookmarks("href7").Range _
    .InsertBefore TextBox7
    .Bookmarks("src7").Range _
    .InsertBefore TextBox7
    .Bookmarks("alt7").Range _
    .InsertBefore TextBox7
 End If
End With

With ActiveDocument
If .Bookmarks("href8").Range = ".jpg" Then
    .Bookmarks("href8").Range _
    .InsertBefore TextBox8
    .Bookmarks("src8").Range _
    .InsertBefore TextBox8
    .Bookmarks("alt8").Range _
    .InsertBefore TextBox8
 End If
End With

With ActiveDocument
If .Bookmarks("href9").Range = ".jpg" Then
    .Bookmarks("href9").Range _
    .InsertBefore TextBox9
    .Bookmarks("src9").Range _
    .InsertBefore TextBox9
    .Bookmarks("alt9").Range _
    .InsertBefore TextBox9
 End If
End With

With ActiveDocument
 If .Bookmarks("href10").Range = ".jpg" Then
    .Bookmarks("href10").Range _
    .InsertBefore TextBox10
    .Bookmarks("src10").Range _
    .InsertBefore TextBox10
    .Bookmarks("alt10").Range _
    .InsertBefore TextBox5
 End If
End With

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

问题的第2部分。

我能够通过Rich提供的网页以及其他网页[stackoverflow.com/questions/10544456/](Dynamically Adding Labels to User Form = Blank UserForm)找出我的问题的第二部分。它还添加了我想要的倍数。我现在只需要格式化它们应该很容易。感谢您的帮助。

Private Sub CommandButton3_Click()

Dim theTextbox As Object
Dim textboxCounter As Long

For textboxCounter = 1 To 4
    Set theTextbox = UserForm1.Controls.Add("Forms.Textbox.1", "Test" & textboxCounter, True)
    With theTextbox        

        .Top = 10 * textboxCounter
    End With
Next
End Sub

1 个答案:

答案 0 :(得分:1)

您可以通过存储对象数组中的文本框然后单独迭代对象来实现此目的。

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 9
    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