I have a macro written in Excel that runs against all the files in a particular folder. I have the folder name hard coded in the macro. Right now, I can run it from Excel without problems. However, as this is just one of many steps in getting to the end result, I have created an Access file that will automate all the steps. From my form (frmExport) I want to be able to run this Excel macro against all files in the folder referenced on frmExport (frmExport.Path) instead of running the macro against all files in the folder that is hard coded in the macro.
This is the code I have in Access. It calls the Excel macro O:\Program Files\iMIS_SQL\custrpts\MOMacro.xls is the Excel file that contains the macro that I want to run:
Private Sub cmdExcelMacro_Click()
Dim xlApp As Object
Dim wb As Object
Dim ws As Object
Set xlApp = CreateObject("Excel.Application")
Set wb = xlApp.Workbooks.Open("O:\Program Files\iMIS_SQL\custrpts\MOMacro.xls", False, False)
xlApp.Visible = True
xlApp.Run "'O:\Program Files\iMIS_SQL\custrpts\MOMacro.xls'!RunsMacroAgainstAll"
wb.Close (False)
xlApp.Quit
Set xlApp = Nothing
End Sub
Here's the code I have in the Excel macro:
Sub RunsMacroAgainstAll()
Dim FSO As FileSystemObject
Dim myFolder As Folder
Dim myFile As File
Dim xlWB As Workbook
Dim strPath As String
strPath = forms!frmExport.path
This brings up an error as the Access reference is not recognized in Excel
Set FSO = New FileSystemObject
Set myFolder = FSO.GetFolder(" & strPath & ")
For Each myFile In myFolder.Files
Set xlWB = Application.Workbooks.Open(myFile)
'*****************
blah, blah, blah, blah
'*****************
ActiveWorkbook.Save
ActiveWorkbook.Close
Next
Set myFolder = Nothing
Set FSO = Nothing
End Sub
In Access, I'm clicking cmdExport on frmExport to run the above code. Is there a way to pass the value of frmExport.PATH to the Excel macro so that it knows what folder to run against?
Anna Jaeger
iMIS Database Support
This is the code I have in Access. It calls the Excel macro O:\Program Files\iMIS_SQL\custrpts\MOMacro.xls is the Excel file that contains the macro that I want to run:
Private Sub cmdExcelMacro_Click()
Dim xlApp As Object
Dim wb As Object
Dim ws As Object
Set xlApp = CreateObject("Excel.Application")
Set wb = xlApp.Workbooks.Open("O:\Program Files\iMIS_SQL\custrpts\MOMacro.xls", False, False)
xlApp.Visible = True
xlApp.Run "'O:\Program Files\iMIS_SQL\custrpts\MOMacro.xls'!RunsMacroAgainstAll"
wb.Close (False)
xlApp.Quit
Set xlApp = Nothing
End Sub
Here's the code I have in the Excel macro:
Sub RunsMacroAgainstAll()
Dim FSO As FileSystemObject
Dim myFolder As Folder
Dim myFile As File
Dim xlWB As Workbook
Dim strPath As String
strPath = forms!frmExport.path
This brings up an error as the Access reference is not recognized in Excel
Set FSO = New FileSystemObject
Set myFolder = FSO.GetFolder(" & strPath & ")
For Each myFile In myFolder.Files
Set xlWB = Application.Workbooks.Open(myFile)
'*****************
blah, blah, blah, blah
'*****************
ActiveWorkbook.Save
ActiveWorkbook.Close
Next
Set myFolder = Nothing
Set FSO = Nothing
End Sub
In Access, I'm clicking cmdExport on frmExport to run the above code. Is there a way to pass the value of frmExport.PATH to the Excel macro so that it knows what folder to run against?
Anna Jaeger
iMIS Database Support