473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 2493
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 script that would be scheduled to run daily to...
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 Office-XP PIA dll’s as these dll’s were been...
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. Also, Whatever the USER ENETRS needs to go to the...
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 to all the Excel application is opened from the...
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 in this group.... the problem is i try using...
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 Object Dim oSheet As Object Dim oRng 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 standard Excel Spreadsheet (just with specific column...
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 go away it is still in the Process list in task...
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 application with Office 2007 installed on my Vista...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.