combobox use problems

  • Thread starter susiecc via OfficeKB.com
  • Start date
S

susiecc via OfficeKB.com

I have created a userform combox where a user enters several word selections
which will subsequently be inserted in various places in document text (i.e.
user makes a text selection and selects an entry in the combobox and inserts
the entry before and after the selection). I managed to set up a textbox
where the entries are typed in. On keyboard return, the entry populates the
combobox. This worked...kind of. Unfortunately, every time a word is typed
into the textbox and the return is hit, the first item in the combobox gets
inserted into text, even without selecting something in the combobox.

I have no idea how to stop it from doing this.

The second problems is that I want the text that is inserted to be red, but
the combobox entry itself needs to stay black. I have been unable to get only
the inserted text to change to red--it changes all of the selected text to
red.

Is there some way I can resolve these problems?

sue
 
D

Doug Robbins - Word MVP

Show us the code that you have

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

susiecc via OfficeKB.com

Doug said:
Show us the code that you have
here is the textbox code and the combobox code.


Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = vbKeyReturn Then
KeyCode = 0
ComboBox1.AddItem TextBox1.Text
ComboBox1.ListIndex = 0
TextBox1.Text = ""
TextBox1.SetFocus
End If
TextBox1.Cut
End Sub

Private Sub ComboBox1_Click()
ComboBox1.SetFocus
With Selection
..InsertBefore "}"
..InsertBefore (ComboBox1.Text)
..InsertBefore "{"
..InsertAfter "{/"
..InsertAfter (ComboBox1.Text)
..InsertAfter "}"
End With
End Sub

i then wrote a third macro to change this text to red (It was the only way i
could figure out how to do it). I tried to call it when I selected from the
combobox, but it made the whole thing red and not just the {word}. This is
probably a convoluted way to do it, but I am not very experienced. The change
color macro worked when I attached it to a command button (i.e. activate the
userform, select text in the document, click on the button).

Sub ChangeColor()
Dim ETMmyRange As Range
Set ETMmyRange = Selection.Range

With ETMmyRange.Find
.Text = "\{*\}"
With .Replacement
.Text = "\{*\}"
'.ClearFormatting
.Font.Bold = False
.Font.Italic = False
.Font.Color = wdColorRed

End With
End With

ETMmyRange2.Find.Execute Replace:=wdReplaceAll
End Sub

Any help is truly appreciated.

sue
 
D

Doug Robbins - Word MVP

Rather than using Private Sub TextBox1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer) to populate the combobox, I
would use a command button on the userform with the code

Combobox1.AddItem TextBox1.Text
TextBox1.Text = ""

I would also give the controls a name that means something.

In the template, I would have a docvariable field with the \* charformat
switch added to it, then when you format the d of docvariable with the
colour you want, that is the colour taht will be used for the variable that
is displayed in the field

To load the variable with the item selected in the Combobox, I would use
another command button on the userform with the code

With ActiveDocument
.Variables("varname").Value = Combobox1.Text & "\" & Combobox1.Text
.Fields.Update
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top