Applying word styles using VB .Net using up too much memory and is slow

  • Thread starter Thread starter kayrogage
  • Start date Start date
K

kayrogage

I have an XML document that I am looping through to generate a word
document. I loop through each element in the xml document and type it
out in the word document, as I type out each element of text I apply
the relevant style to it like so:

With m_objselection
.Style = strFormat
.TypeText(strText)
End With

m_objselection is my current selection and strFormat is the name of the
style I want to apply.

This is a very slow process, when I watch the Winword.exe process in
the task manager the memory usage just goes up and up till it runs out
of memory and can't complete the document.

If I remove the bit where I set the style the compiler whizzes through
and converts the document in no time at all, e.g.:

With m_objselection
' .Style = strFormat
.TypeText(strText)
End With

Is there some way of freeing up the memory word is using to apply each
style and speed up the process?

Thanks

Kay
 
How are you creating the m_objselection and do you clean up references with
Marshal.ReleaseComObject (xxx) afterwards? You might be inadvertently
creating a new selection object on each iteration, without releasing the
previous one.
 
I create the word object refernce, then I create the word document,
then I create m_objSelection:


Dim objWordDoc As New Word.Document
m_objWord = New Word.Application

objWordDoc = m_objWord.Documents.Add()
objWordDoc = m_objWord.ActiveDocument

m_objselection = m_objWord.Selection

I then loop through my existing XML document and build up the word
document. The m_objselection is not set nothing till the end of the
document, I use it to type the word document out as you would manually
setting styles and writing text, paragraphs etc. I think if I set the
m_objselection to nothing after every time I write something out then I
will lose my place in the document.

I was wondering if I could maybe set the style then free up the memory
that is used to set the style some how straight after.

Or if there is another way of doing this.

I have not come across Marshal.ReleaseComObject (xxx) before, I just
had a look at teh msdn for it and I don't really understand it, could
tell me a bit more about it or point me in teh direction of some help
on it please.

Thanks

Kay
 
Back
Top