Hi lo all,
I'm using the Word 9.0 interop (out of necessity!) to generate a Word catalog from a normal document with bookmarks that I use for a template. I'm running this against Word 2000.
I can write out my data just fine, but it doesn't seem to copy the style information... so I'm trying to assign that manually. Here's the relevent code (limited to just the "PRE" section of the template, because it's all the same):
try
{
alTemplateRow = (ArrayList)htTemplate[sField];
Word.Style sPRE = (Word.Style)alTemplateRow[1];
Object osPre = sPRE;
ioWord.Selection.TypeText((string)alTemplateRow[3]);
// next line blows up
ioWord.Selection.set_Style(ref osPre);
}
catch (Exception e)
{
MessageBox.Show("Exception! e: " + e);
}
The style is coming from the template, from this code:
oRange = oDocTemplate.Bookmarks.Item(ref oIndex).Range;
Word.Style sFIELD = (Word.Style)ioWord.Selection.get_Style();
alBookmarkLine.Add(sFIELD);
...
htTemplate.Add(oBookmarkName, alBookmarkLine);
set_Style throws the exception:
Exception! e: System.Runtime.InteropServices.COMException (0x80010105): The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
at Word.Selection.set_Style(Object& prop)
any ideas

text with style (Word 9.0 interop)
Cady
Hi,
you could try to add
osPre = null;
at the end of the try block. I had some problems with pinvoke calls where objects passed as parameters were not referenced after the pinvoke call so that the GC could free those objects still needed in the function called through pinvoke.
Maybe there is something similar with COM interop
Bye,
SvenC
dhnriverside
mphafner
aaronexodus
Word.Style is an interface. I'm closing the document after I gather the style info, thinking that Word.Style is a Word._Application interface but maybe it's a Word._Document interface and that is what is destroying it.
But even postponing the close til later, this method isn't working...
Now I guess I'm going to try to assign the paragraph format, font format, and borders format manually/seperately.
Shanmuk
Move osPre = null; out of and behind the catch block. That might expand the lifetime of that style.
You might also want to store property results like Selction in an extra variable like so:
Select oSel = ioWord.Selection;
oSel.TypeText((string)alTemplateRow[3]);
oSel.set_Style(ref osPre);
Now you can control access to the selection instance also and even make the selection instance live longer than style instance:
osPre = null;
oSel = null;
oSel might have to be moved outside of the try-block, also.
Still only guesses - but maybe... I think here are some points why Microsoft offers an Office PIA
Bye,
SvenC