简体   繁体   English

VBA 使用替代文本填充 Combobox

[英]VBA Populating Combobox With Alternative Text

New to VBA so please forgive what may seem a simple question. VBA 的新手,所以请原谅这个看似简单的问题。

Using MS Word I have produced a simple form.使用 MS Word,我制作了一个简单的表格。 I have a ComboBox that is populated via an array.我有一个通过数组填充的 ComboBox。 This works fine.这工作正常。 What I am trying to achieve is when an option is selected in this ComboBox, an alternative text entry is actually placed into the document at a Bookmark named Advice .我想要实现的是,当在此 ComboBox 中选择一个选项时,一个替代文本条目实际上被放置在名为Advice的 Bookmark 的文档中。

I am using Bookmarks as placemarkers in the Word document.我在 Word 文档中使用书签作为地标。 At the moment the value already defined in the ComboBox is being place in the document, instead of the alternative.目前 ComboBox 中已经定义的值被放置在文档中,而不是替代。

Here is my code.这是我的代码。

    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

I'm sure I'm doing something really silly that is stopping this from working.我确定我正在做一些非常愚蠢的事情来阻止它工作。

Thanks!谢谢!

Sorry, I've just realised that I had missed the key part.抱歉,我刚刚意识到我错过了关键部分。 Unsurprisingly this still doesn't work.不出所料,这仍然行不通。

I've provided the rest of it and included your suggestion.我已经提供了它的 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

You haven't shown the context in which the code is run.您尚未显示运行代码的上下文。 What's the sub, and how is it getting called?什么是子,它是如何被调用的? And you haven't what type of object the Advice variable is or how it got set.而且您不知道 Advice 变量是什么类型的 object 或它是如何设置的。 Your code seems to be setting the combo items and trying to act on a combo selection in the same sub.您的代码似乎正在设置组合项并尝试对同一个子项中的组合选择进行操作。 That won't work.那是行不通的。

You should create an event procedure within the form to populate the combo when the dialog is initialized.您应该在表单中创建一个事件过程,以便在初始化对话框时填充组合。

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

The have another event procedure within the form for a combo change event:在组合更改事件的表单中有另一个事件过程:

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

This will replace the bookmark placeholder text with the indicated text.这将用指定的文本替换书签占位符文本。 Note: the replacement will get rid of the bookmark as well, so it will only work once unless you reset the bookmark.注意:替换也将摆脱书签,因此除非您重置书签,否则它只会工作一次。 If you want the bookmark to remain, you need to recreate it.如果您希望保留书签,则需要重新创建它。 The range object hasn't changed, so you can use that to create the new bookmark:范围 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

So now, after you select an option in the combo box, the text in the doc will be updated, and the bookmark will be reset to the new text.所以现在,在您选择组合框中的 select 选项后,文档中的文本将更新,并且书签将重置为新文本。 So, you can make a different choice, and the text will be updated again.因此,您可以做出不同的选择,文本将再次更新。

You are over writing the text at the bookmark with the following code which is probably run when the form is closed.您正在使用以下代码在书签处编写文本,该代码可能在表单关闭时运行。

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

End Sub结束子

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

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