This one's tough: Finding Character Styles from VBA

F

Fletcher James

I need to determine whether character styles are in use for any sub-portion
of a Range.

If you've worked with Range properties, you know that MOST properties (e.g.
..Bold) return a special undefined value (such as 9999999) when the property
value is not uniform over the range. However, Word uses the Range.Style
property for BOTH Paragraph Styles and Character Styles. If you query the
..Style property for a Range which is ENTIRELY in one Character Style, the
value will be the name of the Character Style. The moment that the Range
overlaps by a single character, into a run with either another Character
Style or no Character Style, then the return value will be the name of the
Paragraph Style for the Range. (If the Range overlaps two paragraphs with
different Styles, then Range.Style will be Nothing)

One solution is to check every single character in the range. That's
effective, but extremely slow. Word-by-word checks would be faster, but not
100% reliable.

A second solution is to examine all of the character styles in defined for
the document (I'm really only interested in the ones which Word has created
on its own from Paragraph styles, such as "Heading 1 Char"), map out
everywhere they are in use in the document (including Footnotes, etc.), and
then see whether any of those ranges overlap the areas of the document that
are of interest to me. That is faster than solution 1, but requires 1-200
lines of VBA code, and is still a kludge.

Does anyone have a direct and elegant way of accomplishing this?

A direct cc: to me, in addition to a reply-to-group, would be appreciated.

--
Fletcher James
President
Levit & James, Inc.

(703)771-1549
MailTo:[email protected]
http://www.levitjames.com
 
W

Word Heretic

G'day "Fletcher James" <[email protected]>,

Copy the chunk to temp invis doc

take the style of char(1)

if its a para style, set a find to all the font charcteristics and
replace with nothing. Any chars left over are thus styled or have a
diff para style. Test teh style type, rinse and repeat as required.

repeat

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Fletcher James reckoned:
 
F

Fletcher James

I guess I wasn't really explicit enough about what I need to do...

My problem is that I'm compiling extracts from a document (in the same
document, like a TOC). They are roughly sentence-length. They need to have
a new paragraph style applied, but any <intentional> direct formatting
retained (e.g. Bold, Italic, EmphasisCharStyle). So far so good
(TargetRange.FormattedText = SourceRange.FormattedText; apply Par Style,
etc.)

The problem comes in when there is <unintentional> direct formatting
applied -- viz, when a paragraph style had been applied to a text run
<without> a paragraph mark, and Word has helped us out by creating a
"Heading 1 Char" type of style (linked style). In that case, the text
retains all of the properties of "Heading 1", including Font.Name,
Font.Size, etc.. We are looking for a clean way to locate these runs in our
copied text, and fix them up (e.g. by resetting the direct formatting to
clear the Character Style, and re-applying the few properties which we
actually need to retain, such as Italic.)

--
Fletcher James
President
Levit & James, Inc.

(703)771-1549
MailTo:[email protected]
http://www.levitjames.com
 
K

Klaus Linke

Hi Fletcher,

I think the macro from
http://groups.google.com/group/micr..._frm/thread/d480035f4b14be0e/d9f49b2f7b5119a9
will do that.
Look at the comments ("To only remove Char styles:").

As soon as you delete the Char style, the font formatting will be reset. I'm
afraid you might loose manual font formatting applied on top of the Char
style.
If that is a problem, you'd need to "tag" the font formatting you want to
keep (say replace bold text with <b>^&</b>), clean out the Char styles, and
then reapply the bold if necessary (= search \<b\>(*)\</b\> and "not bold"
with a wildcard replacement, replace with \1 and "bold", then do the same
without formatting to get rid of the rest of the tags).

Regards,
Klaus
 

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