[RESOLVED] [1.0/1.1] Destroy Object
Automating Word 2003 I have found that the usual way of cleaning up objects when the app closes does not work the same as vb.net.
Why cant I set the application object equal to a null like I can in other Office apps?
Thanks
VB Code:
private Word.Application moApp;
///...
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
///...
moApp.Quit(ref a, ref b, ref a);
moApp = null;
}
Re: [1.0/1.1] Destroy Object
Not sure.
However, the best way to destroy an object is to call it's Dispose() member function, if it implements the IDisposable interface. If the Word.Application class is IDisposable, you should do this.
Re: [1.0/1.1] Destroy Object
No moApp.Dispose method. After I call the quit method I get this error.
Quote:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Print Word CS.exe
Additional information: Bad parameter
At the moApp = null; line
Re: [1.0/1.1] Destroy Object
I'm not quite sure why you can't set the variable to null but it serves no purpose anyway. Setting a variable to null does not destroy an object and you're in the Closing event handler so the variable is going to lose scope almost immediately.
Also, when does the exception get thrown? Is it when the Quit line is executed or when the null line is executed? Given that you're getting a message telling you that you have passed a bad parameter I'd guess that it is the Quit line. Could it be because you're passing the same object as the first and third parameters?
Re: [1.0/1.1] Destroy Object
It errors at the "moApp = null;" line and it leaves my Word process running. I also tried "System.Runtime.InteropServices.Marshal.ReleaseComObject(moApp);". :(
Re: [1.0/1.1] Destroy Object
Ok, I added a new object variable and added the ReleaseCOMObject and it errors at the releasecomobject line with the same error message.
VB Code:
moApp.Quit(ref a, ref b, ref c);
System.Runtime.InteropServices.Marshal.ReleaseComObject(moApp);
moApp = null;
Re: [1.0/1.1] Destroy Object
I switched things around and still error but different and word is left running.
An unhandled exception of type 'System.Runtime.InteropServices.InvalidComObjectException' occurred in Word CS.exe
Additional information: COM object that has been separated from its underlying RCW can not be used.
VB Code:
System.Runtime.InteropServices.Marshal.ReleaseComObject(moApp);
moApp.Quit(ref a, ref b, ref c);
moApp = null;
Re: [RESOLVED] [1.0/1.1] Destroy Object
WOOT! I figured it out. :D
It was the second and third parameters of the .Quit method. They needed to be "System.Reflection.Missing.Value".
There is no delay in closing Word or my program that is usually associated with automating Office from .NET and Word does close and there is no need for the ReleaseCOMObject. :D
VB Code:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
object a = false;
object r_missing = System.Reflection.Missing.Value;
if (moApp.Documents.Count > 0)
{
moApp.Documents.Close(ref a, ref r_missing, ref r_missing);
}
moApp.Quit(ref a, ref r_missing, ref r_missing);
moApp = null;
}