Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2005

Tip: Looking for answers? Try searching our database.

This one's tough:  Finding Character Styles from VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fletcher James - 27 Sep 2005 00:43 GMT
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.

Signature

Fletcher James
President
Levit & James, Inc.

(703)771-1549
MailTo:fjames@levitjames.com
http://www.levitjames.com

Word Heretic - 28 Sep 2005 13:08 GMT
G'day "Fletcher James" <fjames@levitjames.com>,

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:

>I need to determine whether character styles are in use for any sub-portion
>of a Range.
[quoted text clipped - 25 lines]
>
>A direct cc: to me, in addition to a reply-to-group, would be appreciated.
Fletcher James - 28 Sep 2005 15:05 GMT
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.)

Signature

Fletcher James
President
Levit & James, Inc.

(703)771-1549
MailTo:fjames@levitjames.com
http://www.levitjames.com

> G'day "Fletcher James" <fjames@levitjames.com>,
>
[quoted text clipped - 51 lines]
>>
>>A direct cc: to me, in addition to a reply-to-group, would be appreciated.
Klaus Linke - 29 Sep 2005 21:34 GMT
Hi Fletcher,

I think the macro from
http://groups.google.com/group/microsoft.public.word.docmanagement/browse_frm/th
read/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

>I guess I wasn't really explicit enough about what I need to do...
>
[quoted text clipped - 76 lines]
>>>A direct cc: to me, in addition to a reply-to-group, would be
>>>appreciated.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.