【问题标题】:VBA Populating Combobox With Alternative TextVBA 使用替代文本填充组合框
【发布时间】:2020-10-12 13:36:38
【问题描述】:

VBA 新手,所以请原谅这个看似简单的问题。

我使用 MS Word 制作了一个简单的表格。我有一个通过数组填充的 ComboBox。这工作正常。我想要实现的是,当在此 ComboBox 中选择一个选项时,一个替代文本条目实际上被放置在名为 Advice 的书签处的文档中。

我在 Word 文档中使用书签作为地标。目前,已经在 ComboBox 中定义的值被放置在文档中,而不是替代选项。

这是我的代码。

    Dim myArray1() As String
  'Use Split function to return a zero based one dimensional array
  myArray1 = Split("advice for option one|advice for option two|" _
             & "advice for option three", "|")
  'Use List method to populate listbox
  ComboBox2.List = myArray1 
  Exit Sub
  
  If ComboBox2.List = "advice for option one" Then
        Advice.Text = "This piece of text for option one. It's much longer than that in the DropBox, but it is what is needed."
        
    ElseIf ComboBox2.List = "advice for option two" Then
        Advice.Text = "This piece of text for option two. It's much longer than that in the DropBox, but it is what is needed."
        
    ElseIf ComboBox2.List = "advice for option three" Then
        Advice.Text = "This piece of text for option three. It's much longer than that in the DropBox, but it is what is needed."
        
    Else
        Advice.Text = ""
        
    End If
  
lbl_Exit:
  Exit Sub
End Sub

我确定我正在做一些非常愚蠢的事情来阻止它工作。

谢谢!

抱歉,我刚刚意识到我错过了关键部分。不出所料,这仍然不起作用。

我已经提供了其余部分并包含了您的建议。

Private Sub CancelBut_Click()
UserForm.Hide
End Sub

Private Sub EnterBut_Click()
Dim number As Range
Set number = ActiveDocument.Bookmarks("number").Range
number.Text = Me.TextBox1.Value
Dim Name As Range
Set Name = ActiveDocument.Bookmarks("Name").Range
Name.Text = Me.TextBox2.Value
Dim Name1 As Range
Set Name1 = ActiveDocument.Bookmarks("Name1").Range
Name1.Text = Me.TextBox2.Value
Dim Address As Range
Set Address = ActiveDocument.Bookmarks("Address").Range
Address.Text = Me.TextBox3.Value
Dim ReportDate As Range
Set ReportDate = ActiveDocument.Bookmarks("ReportDate").Range
ReportDate.Text = Me.TextBox4.Value
Dim Location As Range
Set Location = ActiveDocument.Bookmarks("Location").Range
Location.Text = Me.TextBox5.Value
Dim Reason As Range
Set Reason = ActiveDocument.Bookmarks("Reason").Range
Reason.Text = Me.ComboBox1.Value
Dim Advice As Range
Set Advice = ActiveDocument.Bookmarks("Advice").Range
Advice.Text = Me.ComboBox2.Value
Dim Office As Range
Set Office = ActiveDocument.Bookmarks("Office").Range
Office.Text = Me.TextBox6.Value
Me.Repaint
UserForm.Hide
End Sub

Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
ComboBox2.Visible = True
Else
ComboBox2.Visible = False
End If
End Sub

Private Sub UserForm_Initialize()
Dim myArray() As String
  'Use Split function to return a zero based one dimensional array
  myArray = Split("problem1|problem2|problem3|problem4|" _
             & "problem5|problem6|problem7|problem8|problem9|" _
             & "problem10|problem11|problem12|problem13|problem14", "|")
  'Use List method to populate listbox
  ComboBox1.List = myArray
 
Dim myArray1() As String
  'Use Split function to return a zero based one dimensional array
  myArray1 = Split("advice for option one|advice for option two|" _
             & "advice for option three", "|")
  'Use List method to populate listbox
  ComboBox2.List = myArray1
  
  End Sub
  
  Private Sub ComboBox2_Change()
    Dim Advice As Range
    
    If ActiveDocument.Bookmarks.Exists("Advice") = True Then
        Set Advice = ActiveDocument.Bookmarks("Advice").Range
        
        Select Case ComboBox2.Value
            Case "advice for option one":
                Advice.Text = "This piece of text for option one."
            Case "advice for option two":
                Advice.Text = "This piece of text for option two."
            Case "advice for option three":
                Advice.Text = "This piece of text for option three."
        End Select
        ActiveDocument.Bookmarks.Add "Advice", Advice
    End If
End Sub

【问题讨论】:

  • 您没有显示代码运行的上下文。什么是子,它是如何被调用的?而且您不知道 Advice 变量是什么类型的对象或它是如何设置的。这是相关信息。

标签: vba combobox ms-word


【解决方案1】:

您尚未显示代码运行的上下文。什么是子,它是如何被调用的?而且您不知道 Advice 变量是什么类型的对象或它是如何设置的。您的代码似乎正在设置组合项并尝试对同一子项中的组合选择进行操作。那是行不通的。

您应该在表单中创建一个事件过程,以便在对话框初始化时填充组合。

Private Sub UserForm_Initialize()
Dim myArray1() As String
  'Use Split function to return a zero based one dimensional array
  myArray1 = Split("advice for option one|advice for option two|" _
             & "advice for option three", "|")
  'Use List method to populate listbox
  ComboBox1.List = myArray1
End Sub

在组合更改事件的表单中有另一个事件过程:

Private Sub ComboBox1_Change()
    Dim Advice As Range
    
    If ActiveDocument.Bookmarks.Exists("advice") = True Then
        Set Advice = ActiveDocument.Bookmarks("advice").Range
        
        Select Case ComboBox1.Value
            Case "advice for option one":
                Advice.Text = "This piece of text for option one."
            Case "advice for option two":
                Advice.Text = "This piece of text for option one."
            Case "advice for option three":
                Advice.Text = "This piece of text for option one."
        End Select
    End If
End Sub

这将用指定的文本替换书签占位符文本。注意:替换也将摆脱书签,因此除非您重置书签,否则它只会工作一次。如果您希望保留书签,则需要重新创建它。范围对象没有改变,因此您可以使用它来创建新书签:

Private Sub ComboBox1_Change()
    Dim Advice As Range
    
    If ActiveDocument.Bookmarks.Exists("advice") = True Then
        Set Advice = ActiveDocument.Bookmarks("advice").Range
        
        Select Case ComboBox1.Value
            Case "advice for option one":
                Advice.Text = "This piece of text for option one."
            Case "advice for option two":
                Advice.Text = "This piece of text for option two."
            Case "advice for option three":
                Advice.Text = "This piece of text for option three."
        End Select
        ActiveDocument.Bookmarks.Add "advice", Advice
    End If
End Sub

所以现在,当您在组合框中选择一个选项后,文档中的文本将被更新,并且书签将被重置为新文本。所以,你可以做出不同的选择,文字会再次更新。

【讨论】:

  • 我意识到我没有提供足够的代码/信息。由于答案无效,原帖已更新。
  • 您添加了更多代码,但没有说明具体如何它不起作用。您是否尝试过设置任何断点并单步执行?如果你在 ComboBox2_Change() 中设置了一个断点,那么只要你在组合框中做出不同的选择,它就会中断。
  • VBA 新手,所以我仍在努力学习。目前问题仍然存在,组合框中的文本仍被添加到 word 文档中的书签处。它仍然没有替代“替代”文本。
【解决方案2】:
Private Sub ComboBox2_Change()
Dim Advice As Range

If ActiveDocument.Bookmarks.Exists("Advice") = True Then
            
    Select Case ComboBox2.Value
        Case "advice for option one":
            ***Advice.Text = "This piece of text for option one."***
        Case "advice for option two":
            Advice.Text = "This piece of text for option two."
        Case "advice for option three":
            Advice.Text = "This piece of text for option three."
    End Select
    
End If

结束子

【讨论】:

  • 对,这是有道理的。我实际上已经看到我已经两次声明了“Advice”变量。从设置其余部分的代码的顶部删除它可以解决问题。
  • 正如另一个后续 - 我可以明智地在代码中添加多少“文本”来修饰“建议”?还是以某种方式“导入”更好?如果是后者,那么有什么地方可以向我突出显示以便我可以做到这一点?
  • 1) 最大语句长度是多少? 2) 如何将值存储在 MS Word 文档中?我希望当通过组合框选择一个选项时,这将在我的文档中填充从几句话到一张 A4 纸的任何内容。
  • 如果您正确执行此操作,您将为您的文档创建一个模板 (dotm) 来保存代码、样式等。如果是这种情况,您可以将各种文本存储为快速部件。
  • 我确实已经创建并添加了 VBA 并将其保存在 (dotm) 文档中。我可以或者确实,我如何在我在这个论坛上展示的 VBA 编码中启动一个选定的快速部件?即通过组合框选择一个快速部件
【解决方案3】:

您使用以下代码在书签处重写了文本,该代码可能在表单关闭时运行。

Private Sub EnterBut_Click()
    ...
    Set Advice = ActiveDocument.Bookmarks("Advice").Range
    Advice.Text = Me.ComboBox2.Value
    ...
End Sub

【讨论】:

  • 代码仍然失败,如星号之间所示。 Private Sub ComboBox2_Change() Dim Advice As Range If ActiveDocument.Bookmarks.Exists("Advice") = True Then Select Case ComboBox2.Value Case "advice for option one": Advice.Text = "This piece of选项一的文本。" Case "选项二的建议": Advice.Text = "选项二的这段文本。"案例“选项三的建议”:Advice.Text =“选项三的这段文字。”结束选择结束如果结束子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
相关资源
最近更新 更多