PDA

View Full Version : WORD VBA - Please help me Bold specific text selected from ListBox



pk247
01-07-2016, 12:24 PM
Hi Everyone,

I've tried a few things to try get this code to work without any manual intervention but I can't quite make it work. I found and adapted it from Greg Maxey's extremely helpful website. Would someone be kind enough to show me how to do this:

1/ The selection made from Listbox1 returns as text into my Word doc (this part works great - thanks Greg Maxey!!)
2/ Any text from the selection that contains text within between two ¦ signs would be bolded
3/ the ¦ would be removed from the selection

e.g. With selection range ¦This text here¦ would become ¦This text here¦ which would end up This text here

Therefore the bold identifiers ¦...¦ would be removed from the document altogether :)

I have thought about running code at the end of the "call UserForm" module but that would mean applying above requirements to the whole document and I really would prefer to steer away from doing that if possible.

Hopefully this is quite an easy thing for someone to help me with please? Or if you can point me in the right direction that would be a great help!


Private Sub CommandButton1_Click()
Dim i As Long
Dim STYLE As String
Dim oRng As Word.Range
STYLE = ""

For i = 1 To ListBox1.ColumnCount
Select Case True
'Build the combo display
Case i = ListBox1.ColumnCount - 1
STYLE = STYLE & ListBox1.Column(i - 1) & " "
Case i = ListBox1.ColumnCount

STYLE = vbTab & ListBox1.Column(1) & vbCr & vbTab & _
ListBox1.Column(2) & vbCr & vbTab & _
ListBox1.Column(3) & vbCr & vbTab & _
ListBox1.Column(4) & vbCr & vbTab & _
ListBox1.Column(5) & vbCr & vbTab & _
ListBox1.Column(6) & vbCr & vbTab & _
ListBox1.Column(7) & vbCr & vbTab & _
ListBox1.Column(8) & vbCr & vbTab & _
ListBox1.Column(9) & vbCr & vbTab & _
ListBox1.Column(10) & vbCr & vbTab & _
ListBox1.Column(11) & vbCr & vbTab & _
ListBox1.Column(12) & vbCr & vbTab
Case Else
STYLE = STYLE & ListBox1.Column(i - 1) & vbCr '& vbTab
End Select
Next i
Set oRng = Selection.Range


oRng.Text = STYLE


'I believe it is around here that my range would be coded to identify words within ¦ identifiers

Me.Hide
lbl_Exit:
Exit Sub
End Sub

Many thanks!

Paul, Ireland

:beerchug:

gmaxey
01-07-2016, 06:11 PM
Set oRng = Selection.Range
With oRng.Find
.MatchWildcards = True
.Text = "^0166*^0166"
While .Execute
oRng.Characters.First.Select 'Added for test
oRng.Characters.First.Delete
oRng.Characters.Last.Delete
oRng.Font.Bold = True
oRng.Collapse wdCollapseEnd
Wend
End With

pk247
01-08-2016, 03:27 AM
Thank you very much Greg!

The bold is working but for the life of me I can't figure why the ¦ delete isn't working. This is how the outcome looks. Maybe my sourcedoc set up does not conform to what you had coded?

P:
¦<!Custom Question!> [XXX-C-X]¦
RB ENT ¦<entered value>¦ RB YN ¦[XXX-C-X]¦
W:
¦<!Custom Question!> [XXX-C-X]¦
RAD BT ¦[XXX-C-X]¦
¦<!Option1!> [XXX-C-X]¦
¦<!Option2!>¦
¦<!OptionX!>¦
[TEXTBOX] ¦[XXX-C-X]¦

The sourcedoc is a word table and the different lines of text are in a separate column each. Is there maybe a small tweak required?

Thanks for all your help so far though!

gmaxey
01-08-2016, 04:30 AM
Step through the revised code and see what is being selected at the first character. That is what is being deleted.

pk247
01-08-2016, 05:47 AM
Thanks for getting back to me Greg. The step into is bunching the individual lines together, then splits them out, then bolds the ¦...¦ text, then ends.

It's almost as if it needs to do the bold then remove ¦ at each juncture. Would you maybe know how to just delete all instances of ¦ within oRng?

You're help is very much appreciated here :beerchug:

Paul,

Ireland

gmaxey
01-08-2016, 06:07 AM
Paul,

If I put your example text in a document, select it and run the macro the result is:


<!Custom Question!> [XXX-C-X]
RB ENT <entered value> RBYN [XXX-C-X]
W:
<!Custom Question!> [XXX-C-X]
RAD BT [XXX-C-X]
<!Option1!> [XXX-C-X]
<!Option2!>
<!OptionX!>
[TEXTBOX] [XXX-C-X]


The process is pretty simply 1)Find your symbol, anything between it and the next instance the symbol
2) Delete the first characters (the opening symbol) and the last characters (the closing symbol)
3) Bold the rest

Seems your issue is that you have not defined the Selection so that it includes all of the text you want processed.

pk247
01-08-2016, 07:47 AM
Thanks for the guidance Greg. I'll hit the drawing board again and see what I come up with. If I get to the bottom of it I'll post back.

Take care,

Paul