473,396 Members | 1,996 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,396 software developers and data experts.

generating an excel file in vb 6.0

198 100+
hello expert i want to generate excel file in vb through coding, i use the following code to generate excel sheet
Dim AppXls As Excel.Application
Dim ObjWb As Excel.Workbook
Dim ObjWs As Excel.Worksheet

Set AppXls = CreateObject("Excel.Application")
Set ObjWb = AppXls.Workbooks.Add

Set ObjWs = ObjWb.Worksheets.Add
ObjWs.Range("A1").Value = "1"

ObjWb.SaveAs ("C:\TestCreate.xls")

ObjWb.Close (SaveChanges = False)
but it producing the error " Add method of object workbooks failed"
please help me by editing the code
Nov 23 '07 #1
3 1947
freedom
16
I assume here that your are getting data from a database say a Access or SQL? If that is the case, you must first create a connection string for that, afterwards, you can use the below code

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim RSFile as ADODB.Recordset
Dim strQuery as string
Dim StrConnection as string

strConnection(your connection string here)
strQuery(Your query here)
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)

Dim n As Int32
For n = 1 To RSFile.Fields.Count
oSheet.Cells(1, n).Value = RSFile.Fields(n - 1).Name
Next

'Transfer the data to Excel.
oSheet.Range("A2").CopyFromRecordset(RSFile)

'Save the workbook and quit Excel.
oBook.SaveAs("C:\Path\Filename.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing

.open (strconnection)
.execute(strquery)
.close

Hope this will help. Cheers!
Nov 23 '07 #2
veer
198 100+
hello expert
you are write, i am trying to insert the data in excel file from sql server
but i am not properly understanding your code like int32
i am sending my code for your clarification please modify it because when i execute it produces the error on line
int32 and can i change it into integer or not
other error is in for loop
" class does't support automation or interface"
please provide some help
thanks

Private Sub Command1_Click()
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim con As ADODB.Connection
Dim RSFile As ADODB.Recordset
Dim strQuery As String
Dim StrConnection As String
Set con = New ADODB.Connection
Set RSFile = New ADODB.Recordset
StrConnection = "Driver={SQL Server};Server=Irish-vul; Database=shrmgmtDb;Uid=sa"
strQuery = "select * from output"

Set oExcel = CreateObject("Excel.Application")


Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
oExcel.Visible = True
Dim n As int32

For n = 1 To RSFile.Fields.Count
oSheet.Cells(1, n).Value = RSFile.Fields(n - 1).Name
Next
'Transfer the data to Excel.
oSheet.Range("A2").CopyFromRecordset (RSFile)
'Save the workbook and quit Excel.
oBook.SaveAs ("C:\var\Book2.xls")
oSheet = Nothing
oBook = Nothing
'oExcel = Quit()
oExcel = Nothing
con.Open (StrConnection)
con.Execute (strQuery)
con.Close
End Sub





I assume here that your are getting data from a database say a Access or SQL? If that is the case, you must first create a connection string for that, afterwards, you can use the below code

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim RSFile as ADODB.Recordset
Dim strQuery as string
Dim StrConnection as string

strConnection(your connection string here)
strQuery(Your query here)
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)

Dim n As Int32
For n = 1 To RSFile.Fields.Count
oSheet.Cells(1, n).Value = RSFile.Fields(n - 1).Name
Next

'Transfer the data to Excel.
oSheet.Range("A2").CopyFromRecordset(RSFile)

'Save the workbook and quit Excel.
oBook.SaveAs("C:\Path\Filename.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing

.open (strconnection)
.execute(strquery)
.close

Hope this will help. Cheers!
Nov 23 '07 #3
freedom
16
Hi, I noticed in your code that you have'nt set the correct connection string, and specifyinf a recordset that would call for the records. It may be better if you first try to chnge your connection string as in the format below, then open a recordset but first dclare it as nee ADODB. Take note, add a compnent for ADO I think use the ADO 2.8 dll.

THe int32 here is for double


Dim conn As New ADODB.Connection
Dim RSFile As New ADODB.Recordset
Dim rs As ADODB.Recordset
Dim strCnn As String
Dim strQuery As String
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

strCnn = "Provider=SQLOLEDB.1;Password=password;Persist Security Info=False;User ID=sa;Initial Catalog=DATABASE;Data Source=Your Server"
strQuery = "SELECT * FROM TBL_AA"
RSFile.Open(strQuery, strCnn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly)
conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient

Thanks. Hope this will help Cheers!!
Nov 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: daniele.balducci | last post by:
Hi All, I'm generating XLS files from ASP(.Net) code using the usual code chunks ... Response.ContentType = "application/vnd.ms-excel" Response.AppendHeader("Content-Disposition", "attachment;...
0
by: Raghavendra | last post by:
hi, we r using forms authetication. problem :- i am using the below code to generate excel report but since we r using forms authetication.. after generating excel report the browser directs...
9
by: hari krishna | last post by:
hi, I want to send the data from dataset information to Excel through ASP.Net. there will be no XL installed on web server. web server is win 2000 server machine. I am using visual basic code in...
3
by: daniele.balducci | last post by:
Hi All, I'm generating XLS files from ASP(.Net) code using the usual code chunks ... Response.ContentType = "application/vnd.ms-excel" Response.AppendHeader("Content-Disposition", "attachment;...
0
by: pedro007 | last post by:
I have 2 listviews and want to save the two lists into an excel file, not individually but save the 2 lists in 2 tables in a file. is that possible, what should I do? Thanks for your help. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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,...

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.