You are on page 1of 1

Public Function ExportExcel(rst As ADODB.

Recordset)
On Error GoTo NoSave:
Dim FileNamePath As String
Dim xlAPP As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim newrst As New ADODB.Recordset
Dim strWork As New ADODB.Stream

Set xlAPP = New Excel.Application


Set xlWB = xlAPP.Workbooks.Add
Set xlWS = xlAPP.Worksheets(1)

FileName = InputBox("What name do you wish to save the file as?" & Chr(10) &
"(Do not use '/', '\', '*', '?', or '|' in filename.)", "File Name")
If FileName <> "" Then
xlWS.SaveAs "c:\temp\" & FileName & ".xls"
FileNamePath = "c:\temp\" & FileName & ".xls"
Else
GoTo NoSave
End If

xlWS.Name = "Sheet1"
xlWB.Save
rst.Save strWork, adPersistADTG
newrst.Open strWork
Set strWork = Nothing

xlWS.Range("A1").Select
xlWS.QueryTables.Add newrst, xlWS.Range("A1")
xlWS.QueryTables(1).Refresh

xlWS.Activate

xlWB.Save
xlWB.Close
xlAPP.Quit

Set xlWS = Nothing


Set xlWB = Nothing
Set xlAPP = Nothing

lngRet = ShellExecute(frmMain.hwnd, "Open", FileNamePath, "", "",


vbNormalFocus)
Exit Function

NoSave:
xlAPP.Quit
Set xlWS = Nothing
Set xlWB = Nothing
Set xlAPP = Nothing

End Function

You might also like