Iterate through formatted runs in a document

C

Chad Knudson

Is there a convenient way to iterate through the formatted runs of a
document? If I have a sentence and the second word of the sentence is bold,
is there some simple way to retrieve the first word, then the second, then
the rest of the sentence?
 
P

Peter Hewett

Hi Chad Knudson

I'm not sure what it is you really want to do. But a Range object has Paragraphs,
Sentences and Words collection objects that you can iterate.

But to do what you asked, you'd do it something like this:

Public Sub TestX()
Dim rngSentence As Word.Range
Dim rngWord1 As Word.Range
Dim rngWord2 As Word.Range

' First sentence of the document
Set rngSentence = ActiveDocument.Sentences(1)

' If second Word of the first sentence is bold
If rngSentence.Words(2).Bold Then

' Setup First word, second word and then the rest of the sentence
Set rngWord1 = rngSentence.Words(1)
Set rngWord2 = rngSentence.Words(2)
rngSentence.Start = rngSentence.Words(3).Start

Debug.Print rngWord1.Text
Debug.Print rngWord2.Text
Debug.Print rngSentence.Text
End If
End Sub

However, you are of course obliged to agree to Words interpretation of what a word is.

HTH + Cheers - Peter
 
C

Chad Knudson

What I'm after is I want to scan through the document and pick up
information about the document's format. I want to delineate where
formatting is different from "Normal" and then I'll record information about
that. I'm trying to do some interpolation on document formatting to be able
to apply themes to my documents. If I see that a word in a paragraph is
bold, I want to preserve that word's bold state no matter what theme
settings I apply to the document. If a paragraph uses a Heading 1 style,
then I know to use the appropriate style from my theme.

Ultimately, I want to be able to receive documents from outside sources and
be able to apply my themes to the documents to have a uniform look.
 
P

Peter Hewett

Hi Chad Knudson

This is quite different from your original post and is very complex in practice. See the
following thread in this NG posted by "Julie" Message-ID:
<[email protected]>

It may be a good starting place.

Cheers - Peter


What I'm after is I want to scan through the document and pick up
information about the document's format. I want to delineate where
formatting is different from "Normal" and then I'll record information about
that. I'm trying to do some interpolation on document formatting to be able
to apply themes to my documents. If I see that a word in a paragraph is
bold, I want to preserve that word's bold state no matter what theme
settings I apply to the document. If a paragraph uses a Heading 1 style,
then I know to use the appropriate style from my theme.

Ultimately, I want to be able to receive documents from outside sources and
be able to apply my themes to the documents to have a uniform look.

HTH + Cheers - Peter
 

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