Use the Visual Studio. Net client to run the automation function without exiting the Office application.

Source: Internet
Author: User
Symptoms: use Microsoft Visual Basic. Net or Microsoft Visual C #. Net to make Microsoft Office applications Program After the automation function is executed, the Office application is not exited when the quit method is called. Cause when Visual Studio. NET is hosted from Code When a COM object is called, it automatically creates a Runtime Library and callable packaging (RCW ). RCW mails calls between. Net Applications and COM objects. RCW retains the reference count for COM objects. Therefore, if all references are not released on RCW, the COM object will not exit. To exit the Office application, the automation code must meet the following conditions: declare each object as a new variable. For example, the following code line obook = oexcel. workbooks. Add () Change to the following content: dim obooks as Excel. workbooksobooks = oexcel. workbooksobook = obooks. Add () When you stop using an object, you should use system. runtime. interopservices. Marshal. releasecomobject. This reduces the reference count of RCW. To release a reference to a variable, set the variable to be nothing or null. Use the quit method of the Office application object to notify the server to close. This is caused by design. Start Visual Studio. NET. On the File menu, click New, and then click Project. Under the Visual Basic Project, select a Windows application and click OK. By default, form1 is created. Add a reference to the Microsoft Excel Object Library. To do this, follow these steps: on the project menu, click Add reference. On the com tab, find the Excel Object Library and click Select. For Microsoft Excel 2002: Microsoft Excel 10.0 Object Library Note: if you have not performed this operation, Microsoft recommends that you download and install the Microsoft Office XP master InterOP assembly (PIA ). For additional information about Office XP Pia, click Article Number to view the article in the Microsoft Knowledge Base: 328912 Microsoft Office XP master InterOP assembly (PIA) for download for Microsoft Office Excel 2003: in the Add reference dialog box, click OK to accept your selection. On the View menu, click the toolbox and drag a button control to form1. Double-click button1. The code window for this form is displayed. Add the following code to the top of form1.vb: Imports Microsoft. Office. InterOP Run the following code in the code window: private sub button#click (byval sender as system. Object, _ byval e as system. eventargs) handles button1.click end sub Replace with: private sub button#click (byval sender as system. object, byval e as system. eventargs) handles button1.click dim oapp as new excel. application () dim obook as Excel. workbook = oapp. workbooks. add dim osheet as Excel. worksheet = oapp. activesheet osheet = nothing obook. close (false) obook = nothing oapp. quit () oapp = nothing debug. writeline ("Sleeping... ") system. threading. thread. sleep (5000) debug. writeline ("End excel") end sub Press F5 to run the application. Open Windows Task Manager. In Visual Studio, the "output" window is displayed to view debugging information. Click the command button and note that an instance of excel.exe is displayed in the process list. Even if the application stops hibernation, the Excel instance still runs in the task list. Close the dialog box and note that excel disappears from the process list. After the "solution" step is executed, the Office application will exit after the last variable is released. Replace the function in step 5th with the following code: private sub nar (byval o as object) Try system. runtime. interopservices. marshal. releasecomobject (o) catch finally O = nothing end try end subprivate sub button#click (byval sender as system. object, byval e as system. eventargs) handles button1.click dim oapp as new excel. application () dim obooks as Excel. workbooks = oapp. workbooks dim obook as Excel. workbook = obooks. add dim osheet as Excel. worksheet = oapp. activesheet nar (osheet) obook. close (false) nar (obook) nar (obooks) oapp. quit () nar (oapp) debug. writeline ("Sleeping... ") system. threading. thread. sleep (5000) debug. writeline ("End excel") end sub If you are using Visual C #. net, Please reference the following code of the nar () function: Private void nar (Object O) {try {system. runtime. interopservices. marshal. releasecomobject (o);} catch {} finally {o = NULL;} After performing the troubleshooting, if the server is not disabled, you can use GC after releasing the last object. collect () and GC. waitforpendingfinalizers () method. Because the Runtime Library collects garbage on RCW, the GC. Collect () method forces the Garbage Collector to run and releases any references that RCW still has. The GC. Collect () method tries to reclaim the maximum available memory. Note that this does not guarantee that all memory is recycled.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.