【发布时间】: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 变量是什么类型的对象或它是如何设置的。这是相关信息。