472,331 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

Problem with using Excel.Application object

hi there,

i've got a form that populates a datagrid, and a button that calls a
function to export to an excel file.
All is well with the export, no errors are returned, but the Excel instance
doesnt want to terminate properly even when i set excelApp.Quit, and
excelApp = nothing.

has anyone else experienced a problem like this???
code below:
--------------------------------------------------------
Sub SaveToExcel(ByVal table As String, ByVal sql As String)

Try
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlQryTable As Excel.QueryTable
xlBook = xlApp.Workbooks.Add()
xlApp.DisplayAlerts = False
Do Until xlBook.Sheets.Count = 1
xlBook.Sheets(1).Delete()
Loop

xlSheet = xlBook.ActiveSheet
xlSheet.Name = "QueryBuilder Data"
xlQryTable = xlSheet.QueryTables.Add("OLEDB;" & strConn,
xlSheet.Range("A1"))
With xlQryTable
.Sql = sql
.Refresh()
End With

xlApp.DisplayAlerts = True
Dim strBookName As String = xlApp.GetSaveAsFilename(table,
fileFilter:="Excel Files (*.xls), *.xls")
If strBookName <> "False" Then xlBook.SaveAs(strBookName)
xlSheet = Nothing
xlQryTable = Nothing
xlBook.Close(False)
xlBook = Nothing
xlApp.Quit()
xlApp = Nothing
Application.DoEvents()
Catch ex As Exception
MessageBox.Show("An error occured while exporting to an Excel file.")
End Try

End Sub

--------------------------------------------------------
thanks,
Paul
Nov 20 '05 #1
4 2390
Hello Paul
I use a similar function and it quits normally.
Try the following :
System.Runtime.InteropServices.Marshal.ReleaseComO bject
(objExcelApp)

Kind Regards
Jorge
-----Original Message-----
hi there,

i've got a form that populates a datagrid, and a button that calls afunction to export to an excel file.
All is well with the export, no errors are returned, but the Excel instancedoesnt want to terminate properly even when i set excelApp.Quit, andexcelApp = nothing.

has anyone else experienced a problem like this???
code below:
--------------------------------------------------------
Sub SaveToExcel(ByVal table As String, ByVal sql As String)
Try
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlQryTable As Excel.QueryTable
xlBook = xlApp.Workbooks.Add()
xlApp.DisplayAlerts = False
Do Until xlBook.Sheets.Count = 1
xlBook.Sheets(1).Delete()
Loop

xlSheet = xlBook.ActiveSheet
xlSheet.Name = "QueryBuilder Data"
xlQryTable = xlSheet.QueryTables.Add("OLEDB;" & strConn,
xlSheet.Range("A1"))
With xlQryTable
.Sql = sql
.Refresh()
End With

xlApp.DisplayAlerts = True
Dim strBookName As String = xlApp.GetSaveAsFilename (table,fileFilter:="Excel Files (*.xls), *.xls")
If strBookName <> "False" Then xlBook.SaveAs(strBookName)
xlSheet = Nothing
xlQryTable = Nothing
xlBook.Close(False)
xlBook = Nothing
xlApp.Quit()
xlApp = Nothing
Application.DoEvents()
Catch ex As Exception
MessageBox.Show("An error occured while exporting to an Excel file.")End Try

End Sub

--------------------------------------------------------
thanks,
Paul
.

Nov 20 '05 #2
Jorge,

i added the extra line you mentioned, but still doesnt unload it.
After line by line elimination, i have found the problem to be when i load
the following:

xlBook = xlApp.Workbooks.Add()

although i unload this object before i unload the Excel object, it still
doesnt solve my problem..
Also, i added the ReleaseComObject on this object , but still no luck.

Any ideas?
thanks,
Paul.

"Jorge" <an*******@discussions.microsoft.com> wrote in message
news:13*****************************@phx.gbl...
Hello Paul
I use a similar function and it quits normally.
Try the following :
System.Runtime.InteropServices.Marshal.ReleaseComO bject
(objExcelApp)

Kind Regards
Jorge
-----Original Message-----
hi there,

i've got a form that populates a datagrid, and a button

that calls a
function to export to an excel file.
All is well with the export, no errors are returned, but

the Excel instance
doesnt want to terminate properly even when i set

excelApp.Quit, and
excelApp = nothing.

has anyone else experienced a problem like this???
code below:
--------------------------------------------------------
Sub SaveToExcel(ByVal table As String, ByVal sql As

String)

Try
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlQryTable As Excel.QueryTable
xlBook = xlApp.Workbooks.Add()
xlApp.DisplayAlerts = False
Do Until xlBook.Sheets.Count = 1
xlBook.Sheets(1).Delete()
Loop

xlSheet = xlBook.ActiveSheet
xlSheet.Name = "QueryBuilder Data"
xlQryTable = xlSheet.QueryTables.Add("OLEDB;" & strConn,
xlSheet.Range("A1"))
With xlQryTable
.Sql = sql
.Refresh()
End With

xlApp.DisplayAlerts = True
Dim strBookName As String = xlApp.GetSaveAsFilename

(table,
fileFilter:="Excel Files (*.xls), *.xls")
If strBookName <> "False" Then xlBook.SaveAs(strBookName)
xlSheet = Nothing
xlQryTable = Nothing
xlBook.Close(False)
xlBook = Nothing
xlApp.Quit()
xlApp = Nothing
Application.DoEvents()
Catch ex As Exception
MessageBox.Show("An error occured while exporting to an

Excel file.")
End Try

End Sub

--------------------------------------------------------
thanks,
Paul
.

Nov 20 '05 #3
Paul,
This is how i declare them:
Dim objExcelApp As New Excel.Application
Dim objBook As Excel.Workbook = objExcelApp.Workbooks.Add
Dim objSheet As Excel.Worksheet = DirectCast
(objExcelApp.ActiveSheet, Excel.Worksheet)

This is how i terminate them:
objBook.Close()
objSheet = Nothing
objExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject
(objExcelApp)
objExcelApp = Nothing
GC.Collect()

Kind Regards
Jorge
-----Original Message-----
Jorge,

i added the extra line you mentioned, but still doesnt unload it.After line by line elimination, i have found the problem to be when i loadthe following:

xlBook = xlApp.Workbooks.Add()

although i unload this object before i unload the Excel object, it stilldoesnt solve my problem..
Also, i added the ReleaseComObject on this object , but still no luck.
Any ideas?
thanks,
Paul.

"Jorge" <an*******@discussions.microsoft.com> wrote in messagenews:13*****************************@phx.gbl...
Hello Paul
I use a similar function and it quits normally.
Try the following :
System.Runtime.InteropServices.Marshal.ReleaseComO bject (objExcelApp)

Kind Regards
Jorge
>-----Original Message-----
>hi there,
>
>i've got a form that populates a datagrid, and a button
that calls a
>function to export to an excel file.
>All is well with the export, no errors are returned,
but the Excel instance
>doesnt want to terminate properly even when i set

excelApp.Quit, and
>excelApp = nothing.
>
>has anyone else experienced a problem like this???
>code below:
>------------------------------------------------------
-- >Sub SaveToExcel(ByVal table As String, ByVal sql As

String)
>
>Try
>Dim xlApp As New Excel.Application
>Dim xlBook As Excel.Workbook
>Dim xlSheet As Excel.Worksheet
>Dim xlQryTable As Excel.QueryTable
>xlBook = xlApp.Workbooks.Add()
>xlApp.DisplayAlerts = False
>Do Until xlBook.Sheets.Count = 1
> xlBook.Sheets(1).Delete()
>Loop
>
>xlSheet = xlBook.ActiveSheet
>xlSheet.Name = "QueryBuilder Data"
>xlQryTable = xlSheet.QueryTables.Add("OLEDB;" & strConn, >xlSheet.Range("A1"))
>With xlQryTable
> .Sql = sql
> .Refresh()
>End With
>
>xlApp.DisplayAlerts = True
>Dim strBookName As String = xlApp.GetSaveAsFilename

(table,
>fileFilter:="Excel Files (*.xls), *.xls")
>If strBookName <> "False" Then xlBook.SaveAs (strBookName) >xlSheet = Nothing
>xlQryTable = Nothing
>xlBook.Close(False)
>xlBook = Nothing
>xlApp.Quit()
>xlApp = Nothing
>Application.DoEvents()
>Catch ex As Exception
> MessageBox.Show("An error occured while exporting to an Excel file.")
>End Try
>
>End Sub
>
>------------------------------------------------------

-- >thanks,
>Paul
>
>
>.
>

.

Nov 20 '05 #4
thanks jorge,

i used your example and it works fine - although i have no idea what was the
problem with my example:))

Paul.

"Jorge" <an*******@discussions.microsoft.com> wrote in message
news:13*****************************@phx.gbl...
Paul,
This is how i declare them:
Dim objExcelApp As New Excel.Application
Dim objBook As Excel.Workbook = objExcelApp.Workbooks.Add
Dim objSheet As Excel.Worksheet = DirectCast
(objExcelApp.ActiveSheet, Excel.Worksheet)

This is how i terminate them:
objBook.Close()
objSheet = Nothing
objExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject
(objExcelApp)
objExcelApp = Nothing
GC.Collect()

Kind Regards
Jorge
-----Original Message-----
Jorge,

i added the extra line you mentioned, but still doesnt

unload it.
After line by line elimination, i have found the problem

to be when i load
the following:

xlBook = xlApp.Workbooks.Add()

although i unload this object before i unload the Excel

object, it still
doesnt solve my problem..
Also, i added the ReleaseComObject on this object , but

still no luck.

Any ideas?
thanks,
Paul.

"Jorge" <an*******@discussions.microsoft.com> wrote in

message
news:13*****************************@phx.gbl...
Hello Paul
I use a similar function and it quits normally.
Try the following :
System.Runtime.InteropServices.Marshal.ReleaseComO bject (objExcelApp)

Kind Regards
Jorge
>-----Original Message-----
>hi there,
>
>i've got a form that populates a datagrid, and a button that calls a
>function to export to an excel file.
>All is well with the export, no errors are returned, but the Excel instance
>doesnt want to terminate properly even when i set
excelApp.Quit, and
>excelApp = nothing.
>
>has anyone else experienced a problem like this???
>code below:
>------------------------------------------------------ -- >Sub SaveToExcel(ByVal table As String, ByVal sql As
String)
>
>Try
>Dim xlApp As New Excel.Application
>Dim xlBook As Excel.Workbook
>Dim xlSheet As Excel.Worksheet
>Dim xlQryTable As Excel.QueryTable
>xlBook = xlApp.Workbooks.Add()
>xlApp.DisplayAlerts = False
>Do Until xlBook.Sheets.Count = 1
> xlBook.Sheets(1).Delete()
>Loop
>
>xlSheet = xlBook.ActiveSheet
>xlSheet.Name = "QueryBuilder Data"
>xlQryTable = xlSheet.QueryTables.Add("OLEDB;" & strConn, >xlSheet.Range("A1"))
>With xlQryTable
> .Sql = sql
> .Refresh()
>End With
>
>xlApp.DisplayAlerts = True
>Dim strBookName As String = xlApp.GetSaveAsFilename
(table,
>fileFilter:="Excel Files (*.xls), *.xls")
>If strBookName <> "False" Then xlBook.SaveAs (strBookName) >xlSheet = Nothing
>xlQryTable = Nothing
>xlBook.Close(False)
>xlBook = Nothing
>xlApp.Quit()
>xlApp = Nothing
>Application.DoEvents()
>Catch ex As Exception
> MessageBox.Show("An error occured while exporting to an Excel file.")
>End Try
>
>End Sub
>
>------------------------------------------------------ -- >thanks,
>Paul
>
>
>.
>

.

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet....
0
by: Adrian Belen via .NET 247 | last post by:
hi, I have a problem when I try to open an excel document in a AxWebBrowser Component in VB.NET. The problem appear in this situation: Before...
4
by: Ivan | last post by:
Hi All, I have tried to automate excel in vb.net and i found a problem that i can't find the solution in anywhere... i hope someone can help me...
4
by: goonsquad | last post by:
I'm writing an Excel Automation app and I'm running to a problem early on with what should be a basic step. Dim oExcel As Object Dim oBook As...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a...
6
by: Darrell Wesley | last post by:
A VB2003 application upgraded to VB2005 that builds an Excel spreadsheet. Everything appears to work correctly except that the Excel object does not...
0
by: Bill Fallon | last post by:
I am developing a VB.Net application with VS 2005 that opens an Excel workbook and populates the worksheet with data. I started developing the...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.