WORD DOM performance in c#

  • Thread starter Thread starter philipl
  • Start date Start date
P

philipl

hi

I am using the word DOM to do some grammar checking. With big docs it
takes a long time. I have managed to improve performance by for
example limiting file I/O and opening only the word app just once for
all documents. Just wondering about the efficency of the below code:

Word.ApplicationClass WordApp = new Word.ApplicationClass();
Word.Range wr = WordApp.Selection.Range;
gramcount = wr.GrammaticalErrors.Count;

for(i=1;i<=gramcount;i++)
{
errorword = wr.GrammaticalErrors.Item(i).Text.ToString();
}

I am assuming that the DOM I am accessing is already in memory and all
info has been filled already. Is this correct? It seems to take less
than a milli second to access the count property but it takes over one
milli second to go throught the loop. Is there anymore I can do to
improve this?

Cheers
 
Philip,

You aren't going to squeeze much more out of that loop. The only thing
I can think of is that if you do this for multiple documents, you might want
to force a garbage collection. You are not cleaning up the references that
the runtime is holding for the COM objects correctly and that could leave a
lot of references lying around. If there are too many resources consumed,
it will eventually slow the whole machine. After your loop, you might want
to call GC.Collect.

Hope this helps.
 
Word.ApplicationClass WordApp = new Word.ApplicationClass();
Word.Range wr = WordApp.Selection.Range;
gramcount = wr.GrammaticalErrors.Count;

for(i=1;i<=gramcount;i++)
{
errorword = wr.GrammaticalErrors.Item(i).Text.ToString();
}

If you cache the GrammaticalErrors collection, you save one call per
iteration, which could make a difference (since marshaling and the
managed<->unmanaged transition makes interop calls slower then pure
managed calls). So try

Word.ProofreadingErrors grammaticalErrors = wr.GrammaticalErrors;
for(i=1;i<=gramcount;i++)
{
errorword = grammaticalErrors.Item(i).Text.ToString();
}



Mattias
 
Back
Top