Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting text in MS Word 2000 Using VBA

Status
Not open for further replies.

nuandaEB

Technical User
Oct 22, 2002
8
CA
I've having a bit of a difficult time formatting
text in a Word document.

I've tried using Paragraph objects -- however, I can't apply didn't formatting (font, style, etc.) to different parts of a paragraph. I tried using ranges, but without the success I'd like.

For example, I would like to display
a string, and then another string that would be underlined,
followed by another string that isn't underlined.

Any help would be greatly appreciated.

Thanks,
EB
 
Hi nuandaEB,

My apologies in advance if you know all this but I state it to put the rest in context. Word works with Paragraphs, Sentences, Words or Characters as well as Selections and Ranges. Positions within a Document (or more correctly a ‘Story’ within a Document but I don’t want to complicate it) are measured by character position relative to the beginning and any position can be named by assigning a Bookmark to it.

If you want to programmatically do things to blocks of text you must have some way of identifying them. To demonstrate, copy this post into a new Word document and then insert a bookmark called Mark1 immediately after this sentence. Now run the code below to underline three words after the bookmark, and then the next sentence. An alternative way is to find some text and underline it. The code below also does this and finally underlines text at character positions 100 – 120. Each underline is in a different format just to try and give you some ideas.

Code:
    Selection.GoTo What:=wdGoToBookmark, Name:="Mark1"
    Selection.MoveRight Unit:=wdWord, Count:=4, Extend:=wdExtend
    Selection.Font.Underline = wdUnderlineWords
    Selection.MoveEndUntil "."
    Selection.Collapse wdCollapseEnd
    Selection.MoveRight 1
    Selection.MoveEndWhile " "
    Selection.MoveRight 1
    Selection.Sentences(1).Underline = wdUnderlineDouble
    Selection.HomeKey wdStory
    Selection.Find.Execute "complicate"
    Selection.Range.Underline = wdUnderlineDash
    Selection.Collapse wdCollapseEnd
    ActiveDocument.Range(100, 120).Underline = wdUnderlineWavy

Enjoy,
Tony
 
Thanks Tony.

I'm generating a document of variable length.
So it doesn't seem practical to use a selection and
do a search of user a bookmark.

Using a range to keep track of where I am, and turning on and off styles seems more practical.
I just have to be able to find out where I am in the document.
How can I find out my current character position?

Thanks,

EB
 
Hi nuandaEB,

Wherever you are in a document is the Selection. Selection.Start and Selection.End give you, respectively, the start and end of the current selection; if you have no text 'blocked up' then the selection has zero length and start and end will be the same. If you are working with Objects (Ranges, Words, etc.) in VBA which are not the current selection then they should all have their own Start and End properties.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top