J
J. Trucking
Hello All,
I have a split database for which some of the users are running Office
2007 and some are running 2003. I have one part of the DB which
automatically exports data from Access into a couple of pre-
constructed Excel Templates. It works awesome on the computers with
2007 but there seems to be some issues when running in 2003. I have
included the code below which was constructed with the help of some
very knowledgable people whom I give all of the credit to. I have
taken out the error handling to save space.
Sub exportmonthlybreakdown()
Dim objXLApp As Object
Set objXLApp = CreateObject("Excel.Application")
Dim objXLBook As Object
Dim obj As Object
Dim strFilter As String
Dim strInputFileName As String
Dim conpath As String
Dim db As DAO.Database
MsgBox ("This process will export data to Microsoft Excel. Please be
patient as this may take a few minutes. You will be prompted to save
the Excel file. You will then be notified when the export is
complete.")
Set db = CurrentDb
conpath = CurrentProject.Path
If Len(Dir(conpath & "\HourBreakdown.xls")) > 0 Then
Kill conpath & "\HourBreakdown.xls"
End If
If Len(Dir(conpath & "\HourBreakdown1.xls")) > 0 Then
Kill conpath & "\HourBreakdown1.xls"
End If
Set objXLBook = objXLApp.Workbooks.Open(conpath &
"\HourBreakdown.xlt")
If Val(Application.Version) < 12 Then
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)",
"*.xls")
strInputFileName =
ahtCommonFileOpenSave_(OpenFile:=False,Filter:=strFilter,Flags:=ahtOFN_OVERWRITEPROMPT
Or ahtOFN_READONLY)
objXLBook.SaveAs (strInputFileName)
objXLBook.Close
DoCmd.TransferSpreadsheet_
acExport,acSpreadsheetTypeExcel9,
"qryMonthlyHourBreakdown",strInputFileName,True, "ExportedData"
Else
strFilter = ahtAddFilterItem(strFilter, "Excel 2000-2003 Workbook
(*.xls)", "*.xls")
strInputFileName = ahtCommonFileOpenSave(OpenFile:=False,
Filter:=strFilter, Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY)
objXLBook.SaveAs (strInputFileName), FileFormat:=56
objXLBook.Close
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12,
"qryMonthlyHourBreakdown", strInputFileName, True, "ExportedData"
End If
MsgBox ("Export Complete.")
FinishWork:
On Error Resume Next
Set db = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing
ExitHere:
Exit Sub
Resume FinishWork
End Sub
I am sorry if the code that is supposed to be on one line got copied
onto two lines. I have two problems with the above code. The first
problem is when the user selects the location to save their file using
the API file open/save box and hits 'Save', the system asks them if
they would like to save 'HourBreakdown1.xls' even though thats not
what they named their file. Do I need to add in another line to kill
that instant of Excel? The second problem is when the user exports
their Excel file and goes to open it, the computer claims that the
file is 'Not Recognizable'. I am guessing that this is because the
user doesn't have the compatibility pack installed but I am not too
sure. I say this because they can open the Excel templates before the
data gets copied over. Once the template is populated with the data
and saved as a different name, it seems to take the same effect as an
uncompatible Excel file. Any advice would be greatly appreciated.
John
I have a split database for which some of the users are running Office
2007 and some are running 2003. I have one part of the DB which
automatically exports data from Access into a couple of pre-
constructed Excel Templates. It works awesome on the computers with
2007 but there seems to be some issues when running in 2003. I have
included the code below which was constructed with the help of some
very knowledgable people whom I give all of the credit to. I have
taken out the error handling to save space.
Sub exportmonthlybreakdown()
Dim objXLApp As Object
Set objXLApp = CreateObject("Excel.Application")
Dim objXLBook As Object
Dim obj As Object
Dim strFilter As String
Dim strInputFileName As String
Dim conpath As String
Dim db As DAO.Database
MsgBox ("This process will export data to Microsoft Excel. Please be
patient as this may take a few minutes. You will be prompted to save
the Excel file. You will then be notified when the export is
complete.")
Set db = CurrentDb
conpath = CurrentProject.Path
If Len(Dir(conpath & "\HourBreakdown.xls")) > 0 Then
Kill conpath & "\HourBreakdown.xls"
End If
If Len(Dir(conpath & "\HourBreakdown1.xls")) > 0 Then
Kill conpath & "\HourBreakdown1.xls"
End If
Set objXLBook = objXLApp.Workbooks.Open(conpath &
"\HourBreakdown.xlt")
If Val(Application.Version) < 12 Then
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)",
"*.xls")
strInputFileName =
ahtCommonFileOpenSave_(OpenFile:=False,Filter:=strFilter,Flags:=ahtOFN_OVERWRITEPROMPT
Or ahtOFN_READONLY)
objXLBook.SaveAs (strInputFileName)
objXLBook.Close
DoCmd.TransferSpreadsheet_
acExport,acSpreadsheetTypeExcel9,
"qryMonthlyHourBreakdown",strInputFileName,True, "ExportedData"
Else
strFilter = ahtAddFilterItem(strFilter, "Excel 2000-2003 Workbook
(*.xls)", "*.xls")
strInputFileName = ahtCommonFileOpenSave(OpenFile:=False,
Filter:=strFilter, Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY)
objXLBook.SaveAs (strInputFileName), FileFormat:=56
objXLBook.Close
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12,
"qryMonthlyHourBreakdown", strInputFileName, True, "ExportedData"
End If
MsgBox ("Export Complete.")
FinishWork:
On Error Resume Next
Set db = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing
ExitHere:
Exit Sub
Resume FinishWork
End Sub
I am sorry if the code that is supposed to be on one line got copied
onto two lines. I have two problems with the above code. The first
problem is when the user selects the location to save their file using
the API file open/save box and hits 'Save', the system asks them if
they would like to save 'HourBreakdown1.xls' even though thats not
what they named their file. Do I need to add in another line to kill
that instant of Excel? The second problem is when the user exports
their Excel file and goes to open it, the computer claims that the
file is 'Not Recognizable'. I am guessing that this is because the
user doesn't have the compatibility pack installed but I am not too
sure. I say this because they can open the Excel templates before the
data gets copied over. Once the template is populated with the data
and saved as a different name, it seems to take the same effect as an
uncompatible Excel file. Any advice would be greatly appreciated.
John