Sven Heitmann
Posts: 193
Joined: 2004-08-23
|
Hi,
I have a question about the Marshal.ReleaseComObject:
This is some code I'am using, can you give me some hints if the using of Marshal.ReleaseComObject is ok this way?
private void CreateStandardStyle() {
object oType = Word.WdStyleType.wdStyleTypeParagraph;
object nextParagraph = StandardStyle;
object baseStyle = "";
Word._Application oWord = WordHost();
Word.Document oDocument = oWord.ActiveDocument;
Word.Styles oStyles = oDocument.Styles;
Word.Style oStyle = oStyles.Add(StandardStyle, ref oType);
Word.Font oFont = oStyle.Font;
Word.ParagraphFormat oFormat = oStyle.ParagraphFormat;
Word.Borders oBorders = oFormat.Borders;
//----- Font
oFont.Name = "Times New Roman";
oFont.Size = 12.0f;
//----- Paragraph
oFormat.TabStops.ClearAll();
oFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;
oFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
oFormat.WidowControl = 0;
oFormat.Hyphenation = -1;
oFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;
//----- Borders
oBorders.DistanceFromTop = 1;
oBorders.DistanceFromLeft = 4;
oBorders.DistanceFromBottom = 1;
oBorders.DistanceFromRight = 4;
//----- Misc
oStyle.set_NextParagraphStyle(ref nextParagraph);
oStyle.set_BaseStyle(ref baseStyle);
oStyle.LanguageID = Word.WdLanguageID.wdGerman;
oStyle.Frame.Delete();
Marshal.ReleaseComObject(oBorders);
Marshal.ReleaseComObject(oFormat);
Marshal.ReleaseComObject(oFont);
Marshal.ReleaseComObject(oStyle);
Marshal.ReleaseComObject(oStyles);
Marshal.ReleaseComObject(oDocument);
Marshal.ReleaseComObject(oWord);
}
Best regards,
SvenBest regards,
Sven Heitmann |
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Hi,
you do everything correctly. You can also use the following code to make sure that all references to a COM object were released:
while (Marshal.ReleaseComObject(yourObject) > 0);
|
|
Sven Heitmann
Posts: 193
Joined: 2004-08-23
|
Ok, thank you for your tip =)
Now I have to recheck my code :oBest regards,
Sven Heitmann |
|
Sven Heitmann
Posts: 193
Joined: 2004-08-23
|
edit:
Ok... I got some trouble when releasing the Word._Application Object... when I release it and wan't to use HostApllication a second time an Exception is thrown because the Word._Application Object was disconnected from the RCW...
so I think releasing all ComObjects apart from the Word._Application Object is a good behavior...Best regards,
Sven Heitmann |
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Hi,
you shouldn't release the Word._Application Object. This will be done by ADX.
so I think releasing all ComObjects apart from the Word._Application Object is a good behavior...
Yes, it will be the right way. |
|