Parsing styles in documeny

R

RNG

Hello:

I’m automating Microsoft Word to parse a document and do a style
conversion. What my program does is the user defines a set of mappings of
styles let’s said StyleA maps to StyleB, StyleC maps to StyleD and so. Then
my program parses the whole document and every time it finds StyleA it
converts it to StyleB, etc.

The application is performing fine for mapping paragraph styles; the
problem is that to do character styles conversion I have to loop thru all the
characters in the document. That’s taking a very long time.

Basically what I’m doing in pseudo code is the following:

Foreach (Paragraph parag in document.Paragraphs)
{
for (int i = 1; i <= parag.Range.Characters.Count; i++)
{
if (Word.Style)
parag.Range.Characters.get_Style().NameLocal is mapped then
parag.Range.Characters.set_Style(mappedStyle)
}
}

Is there any way to accomplish what I’m trying to do more quickly?

I don’t think it matters, but I’m using C# and VSTO 2005.

Thanks in advance,
Ram
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?Uk5H?=,

I can make two suggestions

1. Use Find/Replace instead of parsing the document. Find/Replace can find and
replace styles, without touching the text.

2. Since this is dealing with Word 2003, you might consider saving the document
to XML format, then dealing with the XML. This should allow you to change the
style definitions of the current style names within the document, without
touching the text at all.

If you actually need to see the changes style names, open the document again and
use Application.Organizer.Rename to change the names.
I’m automating Microsoft Word to parse a document and do a style
conversion. What my program does is the user defines a set of mappings of
styles let’s said StyleA maps to StyleB, StyleC maps to StyleD and so. Then
my program parses the whole document and every time it finds StyleA it
converts it to StyleB, etc.

The application is performing fine for mapping paragraph styles; the
problem is that to do character styles conversion I have to loop thru all the
characters in the document. That’s taking a very long time.

Basically what I’m doing in pseudo code is the following:

Foreach (Paragraph parag in document.Paragraphs)
{
for (int i = 1; i <= parag.Range.Characters.Count; i++)
{
if (Word.Style)
parag.Range.Characters.get_Style().NameLocal is mapped then
parag.Range.Characters.set_Style(mappedStyle)
}
}

Is there any way to accomplish what I’m trying to do more quickly?

I don’t think it matters, but I’m using C# and VSTO 2005.


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
P

Peter Huang [MSFT]

Hi

I think you may try to use the Move method to enumerate the character one
by one.

Move Method
See AlsoApplies ToExampleSpecificsMove method as it applies to the Range
and Selection objects.

Collapses the specified range or selection to its start or end position and
then moves the collapsed object by the specified number of units. This
method returns a Long value that indicates the number of units by which the
object was actually moved, or it returns 0 (zero) if the move was
unsuccessful.

expression.Move(Unit, Count)
expression Required. An expression that returns one of the above objects.

Unit Optional Variant. The unit by which the collapsed range or selection
is to be moved. Can be one of the following WdUnits constants: wdCharacter,
wdWord, wdSentence, wdParagraph, wdSection, wdStory, wdCell, wdColumn,
wdRow, or wdTable. If expression returns a Selection object, you can
also use wdLine. The default value is wdCharacter.

Count Optional Variant. The number of units by which the specified range
or selection is to be moved. If Count is a positive number, the object is
collapsed to its end position and moved forward in the document by the
specified number of units. If Count is a negative number, the object is
collapsed to its start position and moved backward by the specified number
of units. The default value is 1. You can also control the collapse
direction by using the Collapse method before using the Move method. If the
range or selection is in the middle of a unit or isn't collapsed, moving it
to the beginning or end of the unit counts as moving it one full unit.

BTW: I suggest do that in the VBA first and then convert VBA to C# would be
some what easier.,


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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