单词 - 自动创建用户窗体文本框

时间:2014-05-20 19:44:48

标签: vba ms-word automation userform

我有一个Userform,它自动创建标签和文本框。问题是创建后我不知道文本框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

1 个答案:

答案 0 :(得分:2)

您的代码已经为其命名为test1test2等,这是.Add方法中的第二个参数:

Set theTextbox = UserForm1.Controls.Add("Forms.TextBox.1", "test" & textboxCounter, True)

Intellisense非常清楚地表明了这一点:

enter image description here

如果您不喜欢,我认为您可以随时从.Name属性进行更改:

只需在添加时为其指定名称即可。

For textboxCounter = 1 To Amount
    Set theTextbox = UserForm1.Controls.Add("Forms.TextBox.1", "test" & textboxCounter, True)
    With theTextbox
        .Name = "TextBox_" & i
        .Width = 200
        .Left = 70
        .Top = 30 * textboxCounter
    End With

Next

评论更新

所以你在这里有这个任务:

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:

在模块顶部使用Option Explicit会警告您这些。当您运行此操作时,如果您已经采取了正常的调试步骤,您应该看到TBs(0)等都已分配Nothing(然后您可以拥有问了一个更好的问题:“为什么我的变量都是Nothing?”等...我提到这些事情不是为了诋毁你,而是为了告诉你如何解决你自己的问题(或者至少得到相关的信息)你可能会在将来提出更好的问题!

<强>为什么吗

因为TextBox1等范围内没有此类对象

为什么不呢?

由于您不会在代码中稍后创建文本框,因此在分配时,该数组将保留所有Nothing

如何解决?

我认为你不能以这种方式引用在运行时创建的控件,因为代码通常不会编译(同样,Option Explicit会提醒你这个;你应该总是总是在每个模块的顶部使用它。)

您应该可以分配如下:

Set TBs(0) = UserForm1.Controls("TextBox_1")等。确保在文本框中添加文本框时,使用.Add方法中指定的正确名称(或.Name属性中指定的名称)。

注意 / p> 祝你好运!