繁体   English   中英

VBA 使用替代文本填充 Combobox

[英]VBA Populating Combobox With Alternative Text

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

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

我在 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

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

谢谢!

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

我已经提供了它的 rest 并包含了您的建议。

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 变量是什么类型的 object 或它是如何设置的。 您的代码似乎正在设置组合项并尝试对同一个子项中的组合选择进行操作。 那是行不通的。

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

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

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

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

所以现在,在您选择组合框中的 select 选项后,文档中的文本将更新,并且书签将重置为新文本。 因此,您可以做出不同的选择,文本将再次更新。

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

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

结束子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM