Solved

Using Excel in VB.NET

Posted on 2004-09-30
12
266 Views
Last Modified: 2010-08-05
Hi everyone,

I am having a problem closing Excel from within my VB.NET app. I use a DataReader to return the results of an SQL and put the results into an Excel worksheet. I then print the worksheet and close excel - as follows:

'Declare Variables
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Open("c:\temp\data.xlt"), Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)

 xlSheet.Range("B3").Value = reader.item("total_qty")

'Print Sheet
xlSheet.Application.Visible = True
xlSheet.PrintOut()
xlApp.DisplayAlerts = False

'Close App
xlBook.Close()
xlApp.Quit()

Although Excel does appear to close correctly (i.e. its not visible!!!), I can see in Windows Task Manager that an instance of Excel is still running. Only when I completely exit my app does Task Manager show that Excel has closed.

Any help at all would be great
0
Comment
Question by:hendrix500
12 Comments
 
LVL 8

Expert Comment

by:bramsquad
ID: 12189521
try this after xlApp.Quit()

xlApp = Nothing

~b
0
 
LVL 1

Author Comment

by:hendrix500
ID: 12189904
Made no difference. Sorry
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12190386
not sure if the Excel.Application object supports .Dispose, but try this:

xlApp.Quit()
xlApp.Dispose()
xlApp = Nothing

you may be seeing 'latency' in the Garbage Collector - setting xlApp to Nothing simply tells the GC that this reference can be Garbage Collected whenever necessary, but that may not take place for a while - depends on the other system/storage needs of your app.

AW
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:hendrix500
ID: 12190513
Dosen't support .Dispose.
0
 
LVL 4

Expert Comment

by:computerg33k
ID: 12192220
instead of xlApp.quit use this:
xlApp.ActiveWindow.Close()

0
 
LVL 4

Expert Comment

by:computerg33k
ID: 12192231
be sure to include xlApp=nothing as well though
0
 
LVL 1

Author Comment

by:hendrix500
ID: 12198178
Doesnt solve the problem unfortunately. Excel is still running in the background
0
 
LVL 4

Expert Comment

by:computerg33k
ID: 12200183
hmm..not sure--will have to try more code--what verison of excel are you running and what version of windows.  for word this works:
'to open a wrd doc
wrdApp = CType(CreateObject("Word.Application"), Word.Application)
        wrdApp.Visible = True

        ' Add a new document.
        wrdDoc = wrdApp.Documents.Add()
        wrdDoc.Select()

'to close a wrd doc

            ' Go to the end of the document.
            wrdApp.Selection.GoTo(Word.WdGoToItem.wdGoToLine, _
                       Word.WdGoToDirection.wdGoToLast)

            ' Perform mail merge.
            wrdMailMerge.Destination = _
                       Word.WdMailMergeDestination.wdSendToNewDocument
            wrdMailMerge.Execute(False)

            ' Close the original form document.
            wrdDoc.Saved = True
            wrdDoc.ActiveWindow.Close()

            ' Release References.
            wrdSelection = Nothing
            wrdMailMerge = Nothing
            wrdMergeFields = Nothing
            wrdDoc = Nothing
            wrdApp = Nothing

            ' Clean up temp file.
            System.IO.File.Delete("C:\DataDoc.doc")

and i'm using windows xp and word version 2002--maybe this could shed some light?
0
 
LVL 1

Author Comment

by:hendrix500
ID: 12200626
Managed to find a solution myself. For anyone who's interested - this is the link

http://www.thescarms.com/dotNet/ExcelObject.asp

Thanks to everyone who made suggestions.  Now how do I close this without accepting a solution!!!!!
0
 
LVL 4

Expert Comment

by:computerg33k
ID: 12200711
to close question with refund go to the community support page and post a "question" there.  be sure to include account name (hendrix500).  just to let you know a .kill is bad code and you might want to try a different route if the program is always going that route to close excel.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12258852
Question answered by asker or dialog valuable.
Closed, 125 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question