|
-
Apr 25th, 2006, 01:14 PM
#1
[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;
}
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 25th, 2006, 01:38 PM
#2
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.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Apr 25th, 2006, 01:41 PM
#3
Re: [1.0/1.1] Destroy Object
No moApp.Dispose method. After I call the quit method I get this error.
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Print Word CS.exe
Additional information: Bad parameter
At the moApp = null; line
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 25th, 2006, 06:59 PM
#4
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?
-
Apr 25th, 2006, 07:47 PM
#5
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);".
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 25th, 2006, 07:56 PM
#6
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;
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 25th, 2006, 07:59 PM
#7
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;
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 26th, 2006, 11:39 PM
#8
Re: [RESOLVED] [1.0/1.1] Destroy Object
WOOT! I figured it out. 
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. 
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;
}
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|