Solved

Using Excel in VB.NET

Posted on 2004-09-30
12
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Progress
Introduction to Processes

690 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