These are my code fragements after many attempetd failures to call the ExecuteExcelMacro from an Access 2010 VBA module.
Access referencing Excel (in code window, References, Excel checked) is a preferred way to create reports from MS Access 2010.
Just can't seem to get the object reference right for passing the variable from Access back to the Excel object.
Searched the web for an hour, nobody else has figured it out either.
If anyone can help with this, I would be happy to verify it.
It will gain popularity since Access 2013 is rumored not to have Reports. Users are expected to use SQL Server Reporting Services or Excel Automation. Yes, I realize there are other ways besides the ExecuteExcel4Macro to accomplish the same result.
Access referencing Excel (in code window, References, Excel checked) is a preferred way to create reports from MS Access 2010.
Just can't seem to get the object reference right for passing the variable from Access back to the Excel object.
Searched the web for an hour, nobody else has figured it out either.
If anyone can help with this, I would be happy to verify it.
It will gain popularity since Access 2013 is rumored not to have Reports. Users are expected to use SQL Server Reporting Services or Excel Automation. Yes, I realize there are other ways besides the ExecuteExcel4Macro to accomplish the same result.
Code:
Public Function GetValue(MyPath As String, MyFile As String, MySheet As String, MyCellReference As String) As String
' Retrieves a value from a closed workbook
Dim arg As String
Dim myA1 As String
myA1 = vbQuote & "A1" & vbQuote
'myrange = vbQuote & "A3" & vbQuote
Dim objXL As Excel.Application
Set objXL = New Excel.Application
' Make sure the MyFile exist
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
If Dir(MyPath & MyFile) = "" Then
GetValue = "MyFile Not Found."
Exit Function
End If
' Create the argument
arg = "'" & MyPath & "[" & MyFile & "]" & MySheet & "'!" & Range(MyCellReference).Range("A1").Address(, , xlR1C1) & "'"
' Execute an XLM macro
GetValue = objXL.ExecuteExcel4Macro(arg)
Set objXL = Nothing
'In order for this function to work properly, a worksheet must be active in Excel.
'This will generate an error if all windows are hidden, or if the active sheet is a Chart sheet or set with XLveryhidden.
End Function
Function GetExcelValue(path, file, sheet, ref)
path = "C:\"
file = "Book1.xlsx"
sheet = "Sheet1"
ref = "A1:C3"
Dim objXL As Excel.Application
Set objXL = New Excel.Application
Dim objxlRange As Excel.Range
Set objxlRange = objXL.Range("A3:A5").Range("a1").Address(, , xlR1C1)
Dim arg As String
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetExcelValue = "file not found"
Exit Function
End If
arg = "'" & path & "[" & file & "]" & sheet & "'!" & objxlRange
GetExcelValue = objXL.ExecuteExcel4Macro(arg)
End Function