Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,806
- Office Version
- 2016
- Platform
- Windows
Any ideas/Workarounds ?
The only ways I know of are to assign the object to a variable that the OnAction routine can access, or pass the name of the object in a way that the OnAction expects (i.e. it needs to know the type of object, or you could pass that too)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
pDest As Any, pSrc As Any, ByVal ByteLen As Long)
Sub test()
Dim lPtr As Long
lPtr = ObjPtr(Application)
ActiveSheet.Shapes(1).OnAction = "'Macro " & lPtr & "'"
End Sub
Sub Macro(ByVal Ptr As Long)
Dim oTempObj As Object
CopyMemory oTempObj, Ptr, 4
MsgBox oTempObj.Name
CopyMemory oTempObj, 0&, 4
End Sub
I actually meant passing the type name, not an object type (the routine would then need to react accordingly), but as usual you have a more elegant solution.
Sub test()
Dim sAppName As String
sAppName = Application.Name
ActiveSheet.Shapes(1).OnAction = "'Macro " & Chr(34) & sAppName & Chr(34) & "'"
End Sub
Sub Macro(ByVal Arg As String)
MsgBox (Arg)
End Sub
ActiveSheet.Shapes(1).OnAction = "'Macro " & Chr(34) & sAppName & Chr(34) & "," & Chr(34) & "Application" & Chr(34) & "'"
More like:
and then the routine has to react based on the 'Application' type passed to it.Code:ActiveSheet.Shapes(1).OnAction = "'Macro " & Chr(34) & sAppName & Chr(34) & "," & Chr(34) & "Application" & Chr(34) & "'"
Sub test()
Dim sObjectType As String
sObjectType = "Application"
ActiveSheet.Shapes(1).OnAction = "'Macro " & Chr(34) & sObjectType & Chr(34) & "'"
End Sub
Sub Macro(ByVal Arg As String)
Dim Obj1 As Application
Dim Obj2 As Workbook
Dim Obj3 As Worksheet
Set Obj1 = Application
Set Obj2 = ThisWorkbook
Set Obj3 = ActiveSheet
Select Case Arg
Case Is = TypeName(Obj1)
MsgBox Application.Name
Case Is = TypeName(Obj2)
MsgBox ThisWorkbook.Name
Case Is = TypeName(Obj3)
MsgBox ActiveSheet.Name
End Select
End Sub
Yep!
As a matter of interest, what are you using it for? I'd never really looked into this as I've never really needed to pass an object from a button before.