PDA

View Full Version : Font Style Macro Word 2010



nicbono
11-21-2011, 05:46 PM
Hi,
I am writing a User Form macro. The macro simply inputs information in spaces specifyed into a table in Word 2010.

I am having trouble understanding how or if I can have two different styles in one line of code.

.Rows(5).Range.Text = txtName.Text & " " & txtPosition.Text

Above is the string of code which I need to edit.
I would like txtName.Text to be in bold and txtPosition.Text to be in regular.

If anyone has a clue if this can even be done, please let me know!

Kind regards,
nicbono

Tinbendr
11-22-2011, 07:29 AM
You'll have to insert the text first, then go back and change the attributes.

One way.
Sub test()
Dim Rng As Range
Set Rng = ActiveDocument.Tables(1).Cell(5, 1).Range
With Rng
.Collapse wdCollapseStart
.MoveEndUntil " "
.Font.Bold = True
End With
End Sub

gmaxey
11-22-2011, 12:51 PM
Or:

Private Sub CommandButton1_Click()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range
'Insert the first bit.
oRng.Text = Me.TextBox1.Text & " "
'Redefine the range.
oRng.Collapse wdCollapseEnd
'Get back in front of the end of cell mark.
oRng.Move wdCharacter, -2
'Insert the second bit.
oRng.Text = Me.TextBox2.Text
oRng.Font.Bold = True
End Sub

Tinbendr
11-22-2011, 05:22 PM
Or::doh:I've struggled with this for sometime, but just couldn't get the proper sequence. Thanks for sharing.

gmaxey
11-22-2011, 05:32 PM
You're welcome!