PDA

View Full Version : [SOLVED:] Bold text while in VBA



Solrac3030
02-23-2011, 04:22 PM
I am inserting text into a document using the following code: Selection.TypeText Text:="This is the sentence I am inserting."
My problem is that I need to Italicize the word "sentnce" of that string and I do not know how to do that. I have been searching the Word Help for anything that might be useful to me but have not been successfull. I know that it is probably a very simple thing, I just don't know what it is. All I need is the VB code and syntax for making the needed text italics and the rest regular.

Thank you in advance for this simple request.

Solrac3030
02-23-2011, 05:36 PM
Figured a quick way to do this, not sure it is the best way but it works.



Selection.TypeText Text:="This is the "
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="sentence"
Selection.Font.Bold = wdToggle
Selection.TypeText Text:=" I am inserting."


This inserts the sentence at the cursor point and bolds the word sentence. If thers is a better way I would like to know but if this is it I will accept it. and keep coding it this way.

Thanks.

Frosty
02-23-2011, 07:03 PM
It's tough to give advice in a vacuum.

The better way to do what you're doing above is not to use a macro at all...

Just type "This is the sentence I am inserting" and manually format the word "sentence" to be bold, and then create a new autotext/building block entry.

If you're not familiar with Autotext... look it up in Word's help. It's a very useful feature, sort of like having a file cabinet of things you would copy and paste.

But what you've figured out is as good a way as any to do what you're doing.

Other than sensitive client data, it's generally better to give the real world whole picture of what you want to do from A to Z, unless you're very familiar with the piece you're missing. For the above, you could also record and adjust macro which searches for the word "sentence" in a selection, and then changes that word to bold formatting.

fumei
02-24-2011, 09:00 AM
In terms of coding, nope, toggling separate instructions of text is the only way to do it. Individual strings can not be separately formatted as strings.

So if you want to code chunks like that, that is how.

However, I agree with Frosty. If this is something you want more than once - that same string that is - use an AutoText.