dim xlapp as Excel.Application
I declare the above code in the general section of my vb project. but when i try to run my project vb send me a error message like "User-define not define"
I got that code from vbcode.com web site. I would like to export FlexGrid data to Excel Sheet.
Software/Hardware used:
ASKED:
April 3, 2006 3:05 AM
UPDATED:
April 4, 2006 2:44 PM
You must reference the ms excel object library in your project under the project/references menu to make the types available in your project. Microsoft office/excel must be installed in order for you to do this.
This is a copy of a function I use to export a recordset to Excel.
Tony
Public Function PopulateExcel() As Boolean
Dim fldCount As Integer
Dim iCol As Integer
Dim Msg As String
Dim MyFileName As String
Dim MySQL As String
Dim ExcelApp As Excel.Application
Dim ExcelWB As Excel.Workbook
Dim X As Boolean
On Error Resume Next
PopulateExcel = True
With frmMain
.MousePointer = vbHourglass
.stsMsg.SimpleText = “Populating the spreadsheet, please wait.”
End With
DoEvents
Set ExcelApp = GetObject(, “Excel.Application”)
If Err 0 Then
If Err = 429 Then ‘ Excel not running
Set ExcelApp = CreateObject(“Excel.Application”)
Err = 0
End If
End If
MyFileName = “c:MyTempprjSales Analysis.xls”
Set ExcelWB = ExcelApp.Workbooks.Open(MyFileName)
If Err 0 Then
MsgBox Error
Set ExcelApp = Nothing
GoTo ExitcmdExcel
End If
If objConn.State adStateOpen Then
If Not StartMe() Then
PopulateExcel = False
GoTo ExitcmdExcel
End If
End If
X = MyQueries(12, MySQL)
Set rsRecordset = objConn.Execute(MySQL)
If Err 0 Then
Msg = “Sorry, couldn’t get the data to export.”
MsgBox Msg, vbCritical + vbOKOnly, “Export Data”
PopulateExcel = False
GoTo ExitcmdExcel
End If
‘ Copy field names to the first row of the worksheet
fldCount = rsRecordset.Fields.Count
For iCol = 1 To fldCount
ExcelWB.ActiveSheet.Cells(1, iCol).Value = rsRecordset.Fields(iCol – 1).Name
Next
‘ Copy the data from Access DB starting in “A2″
ExcelWB.ActiveSheet.Cells(2, 1).CopyFromRecordset rsRecordset
If Err 0 Then
Msg = “Sorry, couldn’t populate the spreadsheet.”
MsgBox Msg, vbCritical + vbOKOnly, “Export Data”
PopulateExcel = False
GoTo ExitcmdExcel
End If
ExitcmdExcel:
With frmMain
.MousePointer = vbDefault
.stsMsg.SimpleText = “The spreadsheet can be found at – ” & MyFileName
End With
DoEvents
With ExcelWB
.Save
.Application.Workbooks.Close
End With
rsRecordset.Close
Set rsRecordset = Nothing
Set ExcelWB = Nothing
objConn.Close
Set objConn = Nothing
End Function