I was trying to help someone here in the support forum pass some variables between Excel/Word. In doing so I came to the conclusion AutoIt does not support ByRef variables through COM.
Create a Word Document
Press Alt-F11
Double Click "ThisDocument"
Paste the following macro
Saveas "ByRef.doc"
Sub MacroTestByRef(ByRef v_Arg)
v_Arg = "After"
End Sub
I would have expected the following to write "After" to the console, but instead it still has the value of "Before".
Limitations on COM Events in AutoIt Some Objects (like the 'WebBrowser') pass arguments to their Event Functions 'by reference'. This is intended to allow the user change these arguments and passing it back to the Object. However, AutoIt uses it's own variable scheme, which is not compatible to COM variables. This means that all values from Objects need to be converted into AutoIt variables, thus loosing the reference to the original memory space. Maybe in the near future we can solve this limitation for you !
Edited by /dev/null, 21 June 2007 - 04:33 PM.
__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
I thought I'd remembered reading something like that before. Although this has nothing to do with events, I'm guessing it is due to the same limitation.
Well shoot. I use byRef throughout IE.au3 thinking that I was saving resources by no cloning the Object variable over and over again... if the architecture requires cloning anyway, the the restrictions that byRef creates are unwarranted (primarily not being able to nest _IE function calls inside eachother).
I don't think it's going to happen. I've mentioned it before, but we can't even use ByRef on built-in functions. It's not a trivial issue to solve, particularly since nobody wants to work on it. Until we figure out how to modify parameters ByRef for built-in functions, COM isn't going to get it either.
I don't think it's going to happen. I've mentioned it before, but we can't even use ByRef on built-in functions. It's not a trivial issue to solve, particularly since nobody wants to work on it. Until we figure out how to modify parameters ByRef for built-in functions, COM isn't going to get it either.
why not considere events as a special type of function and define it with a special keyword (like "Event Func") so it can only be called by the COM object and have a different behaviour when pass arguments by reference?
because events are mostly called by COM object not by user
Sorry if I can't be more explicit, my english is limited
What do events have to do with by reference? An event can't take a parameter by reference.
Here is an example
in the next code I define an event called "EdEvent_EndEdit" it have 4 arguments:
1) if you add to any of this arguments the keyword ByRef then an error occurs (Expected a variable in user function call.)
2) if you don't then you can't control the behavior of the edited cell because the changes in the arguments value are ingnored by the object
if you dont have "OWC11.dll" you can download it Here
The help file of this object, after installed, is in:
"[Drive where you intalled it]:\Program Files\Common Files\Microsoft Shared\Web Components\11\1033\OWCDSS11.CHM"
And the expalnation of the event "EndEdit" can be found in the help file with this path:
"Programming Information/Events/E/EndEdit Event"
AutoIt
#include <GUIConstants.au3>#NoTrayIcon;VarsDimConst$xlToRight=-4161;0xFFFEFBFdimConst$ssClear=10002Dim$oMyErrorGlobal$oExcel,$oEventGlobal$OrdDef[1][4]=[[0,0,0,0]]Global$OrdSaved=TrueGlobal$CurOp="",$CurRow,$CurCol$oMyError=ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler ;MsgBox(64,"Windowdir", @WindowsDir)
InitExcel();Declare objects;Main GuiGuiCreate("Excel Object",802,590,(@DesktopWidth-802)/2,(@DesktopHeight-590)/2,$WS_OVERLAPPEDWINDOW+$WS_VISIBLE+$WS_CLIPSIBLINGS)$GUI_ActiveX=GUICtrlCreateObj($oExcel,10,10,780,550)GUICtrlSetState($GUI_ActiveX,$GUI_HIDE);GUICtrlSetStyle ( $GUI_ActiveX, $WS_HIDE );GUICtrlSetResizing ($GUI_ActiveX,$GUI_DOCKAUTO) ; Auto Resize ObjectGuiSetState()
EditOrder()While1$msg=GuiGetMsg()SelectCase$msg=$GUI_EVENT_CLOSEExitLoopEndSelectWEndExitFunc InitExcel()$oExcel=ObjCreate("OWC11.spreadsheet"); Default to 2003IfnotIsObj($oExcel)Then$oExcel=ObjCreate("OWC10.spreadsheet"); Office XP EndIfIFnotIsObj($oExcel)Then$oExcel=ObjCreate("OWC00.spreadsheet"); Office 2000EndIfIfNotIsObj($oExcel)ThenMsgBox(0,"Reply","No se encontro el Objeto Excel",4)ExitEndIf$oEvent=ObjEvent($oExcel,"EdEvent_")EndFuncFunc EditOrder()$CurOp="EditOrder"GUICtrlSetState($GUI_ActiveX,$GUI_SHOW)with$oExcel.Activewindow.DisplayWorkbookTabs=False.DisplayToolbar=False.Autofit=True.MoveAfterReturnDirection=$xlToRight$oEvent=ObjEvent($oExcel,"EdEvent_")GUICtrlSetState($GUI_ActiveX,$GUI_SHOW)EndWithEndFuncFunc EdEvent_EndEdit($Accept,$FinalValue,$Cancel,$ErrorDescription)MsgBox(64,"FinalValue",$FinalValue.Value)if$FinalValue>4Then$FinalValue.Value=$FinalValue.Value*2Else$Cancel.Value=TrueEndIfEndFunc;This is Sven P's custom error handlerFunc MyErrFunc()$HexNumber=hex($oMyError.number,8)Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"&@CRLF&@CRLF&_"err.description is: "&@TAB&$oMyError.description&@CRLF&_"err.windescription:"&@TAB&$oMyError.windescription&@CRLF&_"err.number is: "&@TAB&$HexNumber&@CRLF&_"err.lastdllerror is: "&@TAB&$oMyError.lastdllerror&@CRLF&_"err.scriptline is: "&@TAB&$oMyError.scriptline&@CRLF&_"err.source is: "&@TAB&$oMyError.source&@CRLF&_"err.helpfile is: "&@TAB&$oMyError.helpfile&@CRLF&_"err.helpcontext is: "&@TAB&$oMyError.helpcontext_)SetError(1); to check for after this function returnsEndfunc
also the AutoIt help File say
Limitations on COM Events in AutoIt
Some Objects (like the 'WebBrowser') pass arguments to their Event Functions 'by reference'. This is intended to allow the user change these arguments and passing it back to the Object. However, AutoIt uses it's own variable scheme, which is not compatible to COM variables. This means that all values from Objects need to be converted into AutoIt variables, thus loosing the reference to the original memory space. Maybe in the near future we can solve this limitation for you !