manipulating word document one character at a time

O

Omatase

I'm moving some text around manually in a word doc using some c# (I
couldn't find a word.csharp newsgroup, please respond in vba if it
makes you feel more comfortable though).

Moving characters around a word document one character at a time is
extremely slow. Even if the document isn't visible. Can someone offer
a suggestion to speeding the below code up? I'm thinking about some
sort of way to tell Word to not process events (or something like
that) while I'm moving everything one character at a time.


Range cellRange = aDoc.Tables.Item(1).Cell(1, 1).Range;
object collapseDirection = WdCollapseDirection.wdCollapseStart;
for (int i = mergeFields.Characters.Count; i > 0; i--)
{
string character = mergeFields.Characters.Item(i).Text;

if (mergeFields.Characters.ShapeRange.Shapes.Count == 0)
{
// add this character directly to our range
object fieldText = character;

// add our field
object type = WdFieldType.wdFieldEmpty;
cellRange.Collapse(ref collapseDirection);
cellRange.Text += character;

// remove this character from its original position
object unit = WdUnits.wdCharacter;
object count = 1;

mergeFields.Characters.Item(i).Delete(ref unit, ref count);
}
}

Any help is appreciated

Thanks
 
O

Omatase

Sorry, the code I posted was inaccurate. Please refer to the below
code instead

Thanks

Range cellRange = aDoc.Tables.Item(1).Cell(1, 1).Range;
object collapseDirection = WdCollapseDirection.wdCollapseStart;
for (int i = mergeFields.Characters.Count; i > 0; i--)
{
string character = mergeFields.Characters.Item(i).Text;

// add this character directly to our range
object fieldText = character;

// add our character
object type = WdFieldType.wdFieldEmpty;
cellRange.Collapse(ref collapseDirection);
cellRange.Text += character;

if (mergeFields.Characters.Item(i).ShapeRange.Shapes.Count == 0)
{
// remove this character from its original position
// if there is no shape attached to it
object unit = WdUnits.wdCharacter;
object count = 1;

mergeFields.Characters.Item(i).Delete(ref unit, ref count);
}

}
 
O

Omatase

sorry, I'm going to have to ammend this yet again. It's late and I
have had a long day. Sorry for the mistakes in the post. I know it
makes it hard to read.

Ammendment,

The obvious solution to the above snippet is to build a string during
the loop and then add the string to the destination which would put
all content in the destination in one fell swoop. That won't work for
my situation however because there are some merge fields in the text I
am iterating through and I need to build those to the destination
dynamically. So what I do is I iterate through all characters in the
source, when I find a non-merge field character (such as a carriage
return or a comma) I add it to the destination immediately. Whenever I
come across a "{" I build a merge field will all of the characters
until I run into a "}". Once I encounter the end brace "}" I add the
merge field to the destination and move on to the next character.

Thanks
 
O

Omatase

I'm still stumped on this one, any takers?

To repeat the jist of the question, I am trying to find something that
will tell word to stop processing events or applying all kinds of
layout logic for a period of time so I can speed up the process of
manipulating one character at a time.

Thanks
 
D

Dave D-C

Maybe look at "ScreenUpdating Property"
I'm moving some text around manually in a word doc using some c# (I
couldn't find a word.csharp newsgroup, please respond in vba if it
makes you feel more comfortable though).

Moving characters around a word document one character at a time is
extremely slow. Even if the document isn't visible. Can someone offer
a suggestion to speeding the below code up? I'm thinking about some
sort of way to tell Word to not process events (or something like
that) while I'm moving everything one character at a time.


Range cellRange = aDoc.Tables.Item(1).Cell(1, 1).Range;
object collapseDirection = WdCollapseDirection.wdCollapseStart;
for (int i = mergeFields.Characters.Count; i > 0; i--)
{
string character = mergeFields.Characters.Item(i).Text;

if (mergeFields.Characters.ShapeRange.Shapes.Count == 0)
{
// add this character directly to our range
object fieldText = character;

// add our field
object type = WdFieldType.wdFieldEmpty;
cellRange.Collapse(ref collapseDirection);
cellRange.Text += character;

// remove this character from its original position
object unit = WdUnits.wdCharacter;
object count = 1;

mergeFields.Characters.Item(i).Delete(ref unit, ref count);
}
}

Any help is appreciated

Thanks
 

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