Pregunta

I currently have 10 Text boxes and pretty much the same code for each. I wan't to be able to type a number and have it create that many boxes. I did a lot of research but I am unable to find an easy way to do this. Any Ideas to try? It will have to duplicate the text boxes in my Userform and the code. I was thinking of copy and paste or some kind of loop.

This code puts the filename into word doc containing the text with bookmarks ""

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

2nd part of question.

I was able to figure out the 2nd part to my question by the webpage provided by Rich and this other webpage [stackoverflow.com/questions/10544456/] (Dynamically Adding Labels to User Form = Blank UserForm) It also adds multiples as I wanted. I just need to format them now which should be easy. Thanks for all the help.

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
¿Fue útil?

Solución

You can achieve this by stashing the textbox's in the object array and then iterating through the objects individually.

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top