Solved

Using Excel in VB.NET

Posted on 2004-09-30
12
265 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
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…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

803 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