I have an Excel sheet that originates from an Access database. Since there are numerous sheets and I have multiple users who need to access it, I want to format it once and not have worries about users needing to do so or have them enable macros. Toward that end, I first create the workbook from the DoCmd.TransferSpreadsheet method creating a dynamic number of sheets and dynamically naming them. I prep the file by first copying an Excel template file which contains macros to format the sheets and insert totals. The first sub that runs from the OpenWorkbook macro checks for the existence of Sheet1, if it exists, it deletes it and loops through the remaining sheets formatting each in turn and saves the workbook. If I build the workbook from Access and run it manually from Excel it works great. I end up with Sheet1 deleted and all of the remaining sheets correctly named and formatted. I can then open the sheet with macros disabled and have the desired result.
However, I want to fully automate this process running the app and saving a fully formatted Excel workbook on a network share. When I call the already outputted Excel sheet from Access it opens, the formatting macros run, but the workbook doesn’t save, even though I have ‘ThisWorkbook.Save’ line in one of the macros that runs.
One oddity is that when I step though the code module and Excel opens the workbook, my dynamically named sheet has a ‘1’ appended to it. i.e.: my sheet is named using a global variable that I set when I output the file. – gFileName - I name it “sched” & the date & “.xls” (sched122709.xls) but shows in the Excel title bar as sched1227091.xls
This is my Access function:
Public Function fnOpenSheet()
Debug.Print gFileName
Dim oXL As Object
Dim oExcel As Object
Dim sFullPath As String
Dim sPath As String
Set oXL = CreateObject("Excel.Application")
On Error Resume Next
oXL.UserControl = True
On Error GoTo 0
On Error GoTo ErrHandle
sFullPath = gFileName
With oXL
.Visible = True
.Workbooks.Open (sFullPath)
Debug.Print sFullPath
'.Save
'.Run "subSave"
.Workbooks.Close
'.Workbooks.Close SaveChanges:=True
.Quit
End With
ErrExit:
Set oXL = Nothing
Exit Function
ErrHandle:
oXL.Visible = False
MsgBox Err.Description
GoTo ErrExit
End Function
If I uncomment .save I get a Microsoft Excel error : A file named ‘RESUME.XLW’ already exists in this location. Do you wish to replace it?
So I thought I’d add a macro to the workbook and call it from Access. I added one call subSave consisting of a single line:
ThisWorkbook.Save
When I uncomment that the macro is called but still nothing saves in the workbook.
If I uncomment .Workbooks.Close SaveChanges:=True
I get an Access error Application Defined or Object Defined error.
When I drop ActiveWorkbook.Close SaveChanges:=True into my subSave macro, I get a an automation error.
What seems to be happening is that when calling the workbook from Access the open file is actually a temporary file of some sort that isn’t being written to via the macros.
Any suggestions as to how to work around this? I really need to finish this and move on to other projects.
However, I want to fully automate this process running the app and saving a fully formatted Excel workbook on a network share. When I call the already outputted Excel sheet from Access it opens, the formatting macros run, but the workbook doesn’t save, even though I have ‘ThisWorkbook.Save’ line in one of the macros that runs.
One oddity is that when I step though the code module and Excel opens the workbook, my dynamically named sheet has a ‘1’ appended to it. i.e.: my sheet is named using a global variable that I set when I output the file. – gFileName - I name it “sched” & the date & “.xls” (sched122709.xls) but shows in the Excel title bar as sched1227091.xls
This is my Access function:
Public Function fnOpenSheet()
Debug.Print gFileName
Dim oXL As Object
Dim oExcel As Object
Dim sFullPath As String
Dim sPath As String
Set oXL = CreateObject("Excel.Application")
On Error Resume Next
oXL.UserControl = True
On Error GoTo 0
On Error GoTo ErrHandle
sFullPath = gFileName
With oXL
.Visible = True
.Workbooks.Open (sFullPath)
Debug.Print sFullPath
'.Save
'.Run "subSave"
.Workbooks.Close
'.Workbooks.Close SaveChanges:=True
.Quit
End With
ErrExit:
Set oXL = Nothing
Exit Function
ErrHandle:
oXL.Visible = False
MsgBox Err.Description
GoTo ErrExit
End Function
If I uncomment .save I get a Microsoft Excel error : A file named ‘RESUME.XLW’ already exists in this location. Do you wish to replace it?
So I thought I’d add a macro to the workbook and call it from Access. I added one call subSave consisting of a single line:
ThisWorkbook.Save
When I uncomment that the macro is called but still nothing saves in the workbook.
If I uncomment .Workbooks.Close SaveChanges:=True
I get an Access error Application Defined or Object Defined error.
When I drop ActiveWorkbook.Close SaveChanges:=True into my subSave macro, I get a an automation error.
What seems to be happening is that when calling the workbook from Access the open file is actually a temporary file of some sort that isn’t being written to via the macros.
Any suggestions as to how to work around this? I really need to finish this and move on to other projects.