473,659 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exporting to Excel Using 2005 Express

Hi
I've found a code which should work, but it doesn't.
Please Help

Imports Microsoft.Offic e.Interop ' Refence in the Class

Dim oExcel As New Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel.
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.add ' The Error Occurs here
'Create an array with 3 columns and 100 rows.
Dim DataArray(99, 2) As Object
Dim r As Integer
For r = 0 To 99
DataArray(r, 0) = "ORD" & Format(r + 1, "0000")
DataArray(r, 1) = Rnd() * 1000
DataArray(r, 2) = DataArray(r, 1) * 0.07
Next
'Add headers to the worksheet on row 1.
oSheet = oBook.ActiveShe et
oSheet.Range("A 1").Value = "Order ID"
oSheet.Range("B 1").Value = "Amount"
oSheet.Range("C 1").Value = "Tax"
'Transfer the array to the worksheet starting at cell A2.
oSheet.Range("A 2").Resize(1 00, 3).Value = DataArray
'Save the Workbook and quit Excel.
oBook.SaveAs("B ook2.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

Thank you
Gal
Nov 21 '05 #1
7 2013
Hi,

Stop using late binding and you will get a better error message.

Dim oExcel As New Excel.Applicati on
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet

oExcel = new excel.applicati on

Ken
-----------------------
"Gal Zilberman" <ga***********@ tns-teleseker.com> wrote in message
news:eo******** ******@TK2MSFTN GP12.phx.gbl...
Hi
I've found a code which should work, but it doesn't.
Please Help

Imports Microsoft.Offic e.Interop ' Refence in the Class

Dim oExcel As New Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel.
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.add ' The Error Occurs here
'Create an array with 3 columns and 100 rows.
Dim DataArray(99, 2) As Object
Dim r As Integer
For r = 0 To 99
DataArray(r, 0) = "ORD" & Format(r + 1, "0000")
DataArray(r, 1) = Rnd() * 1000
DataArray(r, 2) = DataArray(r, 1) * 0.07
Next
'Add headers to the worksheet on row 1.
oSheet = oBook.ActiveShe et
oSheet.Range("A 1").Value = "Order ID"
oSheet.Range("B 1").Value = "Amount"
oSheet.Range("C 1").Value = "Tax"
'Transfer the array to the worksheet starting at cell A2.
oSheet.Range("A 2").Resize(1 00, 3).Value = DataArray
'Save the Workbook and quit Excel.
oBook.SaveAs("B ook2.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

Thank you
Gal

Nov 21 '05 #2
Hi Ken

You're absolutely right, but it still doesn't work.
I get: "Old format or invalid type library." Error
at the 'oBook = oExcel.Workbook s.add' line

Gal

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eH******** ******@TK2MSFTN GP15.phx.gbl...
Hi,

Stop using late binding and you will get a better error message.
Dim oExcel As New Excel.Applicati on
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet

oExcel = new excel.applicati on

Ken
-----------------------
"Gal Zilberman" <ga***********@ tns-teleseker.com> wrote in message
news:eo******** ******@TK2MSFTN GP12.phx.gbl...
Hi
I've found a code which should work, but it doesn't.
Please Help

Imports Microsoft.Offic e.Interop ' Refence in the Class

Dim oExcel As New Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel.
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.add ' The Error Occurs here
'Create an array with 3 columns and 100 rows.
Dim DataArray(99, 2) As Object
Dim r As Integer
For r = 0 To 99
DataArray(r, 0) = "ORD" & Format(r + 1, "0000")
DataArray(r, 1) = Rnd() * 1000
DataArray(r, 2) = DataArray(r, 1) * 0.07
Next
'Add headers to the worksheet on row 1.
oSheet = oBook.ActiveShe et
oSheet.Range("A 1").Value = "Order ID"
oSheet.Range("B 1").Value = "Amount"
oSheet.Range("C 1").Value = "Tax"
'Transfer the array to the worksheet starting at cell A2.
oSheet.Range("A 2").Resize(1 00, 3).Value = DataArray
'Save the Workbook and quit Excel.
oBook.SaveAs("B ook2.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

Thank you
Gal

Nov 21 '05 #3
* "Gal Zilberman" <ga***********@ tns-teleseker.com> scripsit:
You're absolutely right, but it still doesn't work.
I get: "Old format or invalid type library." Error
at the 'oBook = oExcel.Workbook s.add' line


BUG: "Old Format or Invalid Type Library" Error When Automating Excel 2002
<URL:http://support.microso ft.com/?scid=kb;en-us;320369>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4
Hi Gal,

You're still going to have trouble closing out, as in .net you must call
marshal.release comobject(<obje ct>).

Here's some code that works fine for me, using a datatable to create an
Excel .csv or .xls:
Public Function sqltabletocsvor xls(ByVal dt As DataTable, ByRef strpath As
String, ByVal dtype As String, ByVal includeheader As Boolean) As Integer

' signature:

' dim funcs as new imcfunctionlib. functions

' dim xint as integer

' xint = funcs.sqltablet ocsvorxls(dsman ifest.tables(0) ,mstrpath,
"csv",false )

' where mstrpath = , say, "f:\imcapps\xls files\test.xls"

sqltabletocsvor xls = 0

Dim objxl As Excel.Applicati on

Dim objwbs As Excel.Workbooks

Dim objwb As Excel.Workbook

Dim objws As Excel.Worksheet

Dim mrow As DataRow

Dim colindex As Integer

Dim rowindex As Integer

Dim col As DataColumn

Dim fi As FileInfo = New FileInfo(strpat h)

If fi.Exists = True Then

Kill(strpath)

End If

objxl = New Excel.Applicati on

'objxl.Visible = False ' i may not need to do this

objwbs = objxl.Workbooks

objwb = objwbs.Add

objws = CType(objwb.Wor ksheets(1), Excel.Worksheet )

' i many want to change this to pass in a variable to determine

' if i want to have a column name row or not

If includeheader Then

For Each col In dt.Columns

colindex += 1

objws.Cells(1, colindex) = col.ColumnName

Next

rowindex = 1

Else

rowindex = 0

End If

Dim fixedstring As String

For Each mrow In dt.Rows

rowindex += 1

colindex = 0

For Each col In dt.Columns

colindex += 1

fixedstring = mrow(col.Column Name).ToString( ).Replace(vbCrL f, "")

'objws.Cells(ro windex, colindex) = mrow(col.Column Name).ToString( )

If col.DataType.To String = "System.Str ing" Then

objws.Cells(row index, colindex) = "'" & fixedstring

Else

objws.Cells(row index, colindex) = fixedstring

End If

Next

Next

If dtype = "csv" Then

objwb.SaveAs(st rpath, xlCSV)

Else

objwb.SaveAs(st rpath)

End If

objxl.DisplayAl erts = False

objwb.Close()

objxl.DisplayAl erts = True

Marshal.Release ComObject(objws )

objxl.Quit()

Marshal.Release ComObject(objxl )

objws = Nothing

objwb = Nothing

objwbs = Nothing

objxl = Nothing

End Function

HTH,

Bernie Yaeger

"Gal Zilberman" <ga***********@ tns-teleseker.com> wrote in message
news:eo******** ******@TK2MSFTN GP12.phx.gbl...
Hi
I've found a code which should work, but it doesn't.
Please Help

Imports Microsoft.Offic e.Interop ' Refence in the Class

Dim oExcel As New Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel.
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.add ' The Error Occurs here
'Create an array with 3 columns and 100 rows.
Dim DataArray(99, 2) As Object
Dim r As Integer
For r = 0 To 99
DataArray(r, 0) = "ORD" & Format(r + 1, "0000")
DataArray(r, 1) = Rnd() * 1000
DataArray(r, 2) = DataArray(r, 1) * 0.07
Next
'Add headers to the worksheet on row 1.
oSheet = oBook.ActiveShe et
oSheet.Range("A 1").Value = "Order ID"
oSheet.Range("B 1").Value = "Amount"
oSheet.Range("C 1").Value = "Tax"
'Transfer the array to the worksheet starting at cell A2.
oSheet.Range("A 2").Resize(1 00, 3).Value = DataArray
'Save the Workbook and quit Excel.
oBook.SaveAs("B ook2.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

Thank you
Gal

Nov 21 '05 #5
Thank you
Now I just need to find the best workaround for this bug

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:uI******** *****@tk2msftng p13.phx.gbl...
* "Gal Zilberman" <ga***********@ tns-teleseker.com> scripsit:
You're absolutely right, but it still doesn't work.
I get: "Old format or invalid type library." Error
at the 'oBook = oExcel.Workbook s.add' line


BUG: "Old Format or Invalid Type Library" Error When Automating Excel 2002
<URL:http://support.microso ft.com/?scid=kb;en-us;320369>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6
Hi,

Try using the office xp pia.
http://msdn.microsoft.com/library/de...dc_oxppias.asp

Ken
------------------
"Gal Zilberman" <ga***********@ tns-teleseker.com> wrote in message
news:u4******** ******@TK2MSFTN GP10.phx.gbl...
Thank you
Now I just need to find the best workaround for this bug

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:uI******** *****@tk2msftng p13.phx.gbl...
* "Gal Zilberman" <ga***********@ tns-teleseker.com> scripsit:
You're absolutely right, but it still doesn't work.
I get: "Old format or invalid type library." Error
at the 'oBook = oExcel.Workbook s.add' line


BUG: "Old Format or Invalid Type Library" Error When Automating Excel 2002
<URL:http://support.microso ft.com/?scid=kb;en-us;320369>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #7
Thanks, I already have it.
It's a Regional thing, My PC has a different region the US and that's what
causing the problem, as soon as I change my region to US it works fine.
I need to get the workaround to work.

thanks anyway

Gal

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:ek******** *******@TK2MSFT NGP11.phx.gbl.. .
Hi,

Try using the office xp pia.
http://msdn.microsoft.com/library/de...dc_oxppias.asp
Ken
------------------
"Gal Zilberman" <ga***********@ tns-teleseker.com> wrote in message
news:u4******** ******@TK2MSFTN GP10.phx.gbl...
Thank you
Now I just need to find the best workaround for this bug

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:uI******** *****@tk2msftng p13.phx.gbl...
* "Gal Zilberman" <ga***********@ tns-teleseker.com> scripsit:
You're absolutely right, but it still doesn't work.
I get: "Old format or invalid type library." Error
at the 'oBook = oExcel.Workbook s.add' line


BUG: "Old Format or Invalid Type Library" Error When Automating Excel 2002 <URL:http://support.microso ft.com/?scid=kb;en-us;320369>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #8

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

Similar topics

3
9238
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works only exporting to single worksheet. but i need to export data to multiple worksheets. it is very urgent to us. so please help me in code.
2
7703
by: G | last post by:
When I export data from access to excel by with "export" or "Analyze with" I seem to loose parts of some fields (long text strings). Is there a way to export it all to excel? Thanks G
2
3057
by: deko | last post by:
I use a complied query to export to Excel like this: SELECT * INTO . FROM tblExcelData; But I have a situation where I need to export several tables into the same worksheet. The idea is to have each contiguous block of data on the worksheet separated by 50 or so rows so a graph can be inserted between each set of data.
1
3158
by: Mustufa Baig | last post by:
I have an ASP.NET website where I am showing off crystal reports to users by exporting them to pdf format. Following is the code: ---------------- 1 Private Sub ExportReport() 2 Dim oStream As System.IO.MemoryStream = 3 myReport.ExportToStream( ExportFormatType.PortableDocFormat) 4 Response.Clear() 5 Response.Buffer() = True
2
2402
by: bienwell | last post by:
Hi, I have a question about exporting data from datagrid control into Excel file in ASP.NET. On my Web page, I have a linkbutton "Export data". This link will call a Sub Function to perform exporting ALL data from the datagrid control. Exporting data works fine when I show all data on the datagrid control. I'd like to shows only 30 records on the datagrid control instead of ALL data using page navigation, and perform exporting...
0
1472
by: Cassidy | last post by:
Hi, Using the Excel object in Visual Studio 2005 I'm trying to export an Excel ObjectList to a string as XML. I'm trying to read out the three columns referenced my an Excel Range, but when I run the code I hit the line where it tries to set map the first column and get an error message saying "The XPath is not valid because either the XPath syntax is incorrect or not supported by Excel." As far as I can see the XPath syntax is...
2
3170
by: Snozz | last post by:
The short of it: If you needed to import a CSV file of a certain structure on a regular basis(say 32 csv files, each to one a table in 32 databases), what would be your first instinct on how to set this up so as to do it reliably and minimize overhead? There are currently no constraints on the destination table. Assume the user or some configuration specifies the database name, server name, and filename+fullpath. The server is SQL...
4
11583
by: Looch | last post by:
Thanks in advance for any help. I'm having trouble with this code: Dim xlsApp As Excel.Application Dim xlsWB As Excel.Workbook xlsApp = New Excel.Application xlsApp.Visible = True ...... The problem is Excel.Application is not recognized. I have the Excell
7
12292
by: leninv | last post by:
Hi, I have the following code where 'recs' is a record set. For i=0 to recs.Fields.Count - 1 if i = 0 then pindnt = string(itmlvl*2," ") response.write pindnt & recs(i) else Response.write recs(i)
0
8339
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8535
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.