I seem to be over my head and can use some help...
I have pieced together some code from several internet searched that is supposed to export a query from access to an existing excel file. I also wanted it to clear any existing data on the excel sheet before the query is exported. It seems to work sometimes, other times when the excel file opens it has a white screen and the toolbars are frozen, but if I close and reopen it all the data is there. The question I have is,
1. Which part of the code could be causing to problem with the screen
2. Does the code need more fine tuning and if so what is recommended
3. Is there a better way to do the export to an existing file and be sure all the contents of the sheet are overwritten
Thank You for any feed back you can offer
I have pieced together some code from several internet searched that is supposed to export a query from access to an existing excel file. I also wanted it to clear any existing data on the excel sheet before the query is exported. It seems to work sometimes, other times when the excel file opens it has a white screen and the toolbars are frozen, but if I close and reopen it all the data is there. The question I have is,
1. Which part of the code could be causing to problem with the screen
2. Does the code need more fine tuning and if so what is recommended
3. Is there a better way to do the export to an existing file and be sure all the contents of the sheet are overwritten
Thank You for any feed back you can offer
Code:
Private Sub Export_Submittal_to_Excel_Click() DoCmd.Hourglass True Dim xlApp As Excel.Application Dim wkb As Excel.Workbook Dim wks As Excel.Worksheet Dim strQryName As String, strXLFile As String strDB = CurrentDb.Name strCurrentDir = Left(strDB, Len(strDB) - Len(Dir(strDB))) strXLFile = "" & strCurrentDir & "Submit Road Rewards.xls" strQryName = "Submit Road Rewards" 'Query Name Set xlApp = CreateObject("Excel.Application") Set wkb = xlApp.Workbooks.Open(strXLFile) xlApp.Visible = False xlApp.DisplayAlerts = False xlApp.Application.ScreenUpdating = True wkb.Worksheets("Submit_Road_Rewards").Cells.ClearContents 'Delete wkb.Worksheets("Template-Run Macro").Select wkb.Save wkb.Close xlApp.Quit Set xlApp = Nothing Set wkb = Nothing Set wks = Nothing DoCmd.Hourglass False DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strQryName, strXLFile, True If MsgBox("Do you want to view the file?", vbYesNo) = vbYes Then FollowHyperlink strXLFile End If End Sub
Comment