Solved

Using Excel in VB.NET

Posted on 2004-09-30
12
263 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 49
nestparen challenge 4 57
How does Google get such fast response times? 5 126
Birthdays 3 21
This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now