【问题标题】:Word - Auto Create UserForm TextboxesWord - 自动创建用户窗体文本框
【发布时间】: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


    【解决方案1】:

    你的代码已经给它命名了test1test2等,这是.Add方法中的第二个参数:

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

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

    如果您不喜欢这样,我认为您可以随时从 .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 属性中指定的名称。

    注意必须在将 TextBox(es) 添加到用户窗体后进行此分配,否则会发生错误。

    祝你好运!

    【讨论】:

    • 我之前尝试将第一个“测试”更改为 TextBox1,但它仍然无法与上面的代码一起使用。我会再试一次。
    • 是的,又试了一次。我也尝试更改它 TextBox1 和 TextBox 但顶部的代码仍然无法识别名称。我也会继续尝试一些不同的东西。
    • 好吧,一开始这不是你的问题……下次你应该解释得更好。我会修改我的答案来解决这个问题。
    • 您好 JohnB,上面的修订版应该可以解释为什么您的代码无法正常工作(至少有两个原因)。我想我对他们都说。让我知道这对你有什么作用!
    • 你所说的一切都很有道理。我已经在 VB 中编码大约 2 周了。我很欣赏你的建议。我今晚没时间了,但我会在本周晚些时候进一步调查。
    猜你喜欢
    • 2011-08-04
    • 2022-11-14
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多