Jump to content

Using Execute() with COM Objects


Recommended Posts

  • Moderators

For reasons unknown to me some COM Methods in MS Word cannot be called with a so called null ($var = "") parameter. Do to this I'm needing a way to call the method with only the parameters called with the function.

Here is an example to better explain this...

; You can run this without error
_WordMacroRun($oWordApp, "TestMacro")

Func _WordMacroRun(ByRef $o_object, $s_MacroName)
    $o_object.Run ($s_MacroName)
EndFunc   ;==>_WordMacroRun

; You can also run this without error
_WordMacroRun($oWordApp, "TestMacro", 1, 2)

Func _WordMacroRun(ByRef $o_object, $s_MacroName, $v_Arg1 = "", $v_Arg2 = "")
    $o_object.Run ($s_MacroName, $v_Arg1, $v_Arg2)
EndFunc   ;==>_WordMacroRun

; This however will give an error
_WordMacroRun($oWordApp, "TestMacro")

Func _WordMacroRun(ByRef $o_object, $s_MacroName, $v_Arg1 = "", $v_Arg2 = "")
    $o_object.Run ($s_MacroName, $v_Arg1, $v_Arg2)
EndFunc   ;==>_WordMacroRunoÝ÷ ØÚ«mzjm¶Þ²ozØb°kçmçèZ0xºÛ(}ªáj÷¡iÙèç$jëh×6_WordMacroRun($oWordApp, "TestMacro", 1, 2, 3)

Func _WordMacroRun(ByRef $o_object, $s_MacroName, $v_Arg1 = "", $v_Arg2 = "", $v_Arg3 = "", $v_Arg4 = "", $v_Arg5 = "")
    Local $s_Expression = ""

    If @NumParams > 2 Then
        For $i = 1 To @NumParams
            $s_Expression &= Eval("v_Arg" & "i")
            If $i <> @NumParams Then
                $s_Expression &= ", "
            EndIf
        Next
    EndIf
    ; This is where I expect the issue to be
    Execute("$o_object.Run ($s_MacroName, " & Eval("s_Expression") & ")")
EndFunc   ;==>_WordMacroRun

Any help would be greatly appreciated!

Edited by big_daddy
Link to comment
Share on other sites

I actually use EXECUTE a few times in IE.au3

A couple of times like this:

$oItems = Execute("$o_object.elements('" & $s_Name & "')")

and once like this:

$aVcard[1][$i] = Execute('$o_object.document.parentwindow.top.navigator.userProfile.getAttribute("' & $aVcard[0][$i] & '")')

What exactly is going wrong for you and have you played with the Default keyword?

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

You can't send an empty string through COM and have it pass as a "skipped" parameter. I have never seen that work. You have to use the "Default" keyword in place of a null string, which will act as a VBA NULL.

-S

(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

Func _WordMacroRun(ByRef $o_object, $s_MacroName, $v_Arg1 = "", $v_Arg2 = "")
    If $v_Arg1 = "" And $v_Arg2 = "" Then $o_object.Run ($s_MacroName, Default, Default)
    If $v_Arg1 <> "" And $v_Arg2 = "" Then $o_object.Run ($s_MacroName, $v_Arg1, Default)
    If $v_Arg1 = "" And $v_Arg2 <> "" Then $o_object.Run ($s_MacroName, Default, $v_Arg2)
    If $v_Arg1 = <> "" And $v_Arg2 <> "" Then $o_object.Run ($s_MacroName, $v_Arg1, $v_Arg2)
EndFunc   ;==>_WordMacroRun

Sloppy workaround, but it should work. Basically it ends up being a logic issue.

-S

Edited by Locodarwin
(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

  • Moderators

Here's another couple examples.

With the Default Keyword in the Method Call it runs fine.

_WordMacroRun($oWordApp, "TestMacro", 1, 2)

Func _WordMacroRun(ByRef $o_object, $s_MacroName, $v_Arg1 = "", $v_Arg2 = "")
    $o_object.Run ($s_MacroName, $v_Arg1, $v_Arg2, Default)
EndFunc   ;==>_WordMacroRun

I guess the real question is how can I build the Method Call based on the value of the @NumParams Macro? As you can see I tried this above, but was never able to get the result I needed.

@Locodarwin - Thanks for the workaround, but that would be unacceptable for 30 optional parameters.

Link to comment
Share on other sites

Because "Default" isn't a value, it's a keyword. When AutoIt sees it in a function call, it knows to pass, for example, a NULL value if appropriate to the underlying COM function.

-S

P.S. Yeah, my workaround wouldn't be good in your scenario. Different, scalable logic would be necessary in that case. It was just an example.

Edited by Locodarwin
(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

  • Moderators

Because "Default" isn't a value, it's a keyword. When AutoIt sees it in a function call, it knows to pass, for example, a NULL value if appropriate to the underlying COM function.

I know Default is a keyword and not a value. If what you say is true then my second example above should work, but it does not.

I checked the variable with IsKeyword() right before the Method Call and it was still a keyword, but it errors unless Default is used directly within the Method Call. I'm thinking this may be a bug with how AutoIt handles keyword variables from within an Object Call.

Link to comment
Share on other sites

Here's another couple examples.

With the Default Keyword in the Method Call it runs fine.

_WordMacroRun($oWordApp, "TestMacro", 1, 2)

Func _WordMacroRun(ByRef $o_object, $s_MacroName, $v_Arg1 = "", $v_Arg2 = "")
    $o_object.Run ($s_MacroName, $v_Arg1, $v_Arg2, Default)
EndFunc   ;==>_WordMacroRun

I guess the real question is how can I build the Method Call based on the value of the @NumParams Macro? As you can see I tried this above, but was never able to get the result I needed.

@Locodarwin - Thanks for the workaround, but that would be unacceptable for 30 optional parameters.

hi Big daady !

I hope you will resolve your question .

my question is what are $v_Arg1 = "", $v_Arg2 = "" are they parameters which should pass to the macro ?

According to your help file , was using without extra parameters . The additional parameters may add more powerful feature of calling macro from Autoit

Link to comment
Share on other sites

I don't see it as a bug, I see it as a design mismatch between AutoIt and COM.

I know Default is a keyword and not a value. If what you say is true then my second example above should work, but it does not.

Your second example doesn't work, I suspect, because you are trying to assign a keyword to a variable:

$v_Arg1 = Default

I don't believe this will work, because "Default" isn't a value that you can just assign to a variable.

Perhaps a different solution should be proposed for assigning NULL values to COM parameters.

-S

(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

  • Moderators

I don't see it as a bug, I see it as a design mismatch between AutoIt and COM.

If this was the case then it wouldn't work at all. This is what leads me to believe it's a bug with the way AutoIt deals with keyword variables when using then with COM.

Your second example doesn't work, I suspect, because you are trying to assign a keyword to a variable:

$v_Arg1 = Default

I don't believe this will work, because "Default" isn't a value that you can just assign to a variable.

As I stated above I checked the variable with IsKeyWord() and it returned true. They also show it being assigned to a variable in the help file.
Link to comment
Share on other sites

To test whether it is an issue with passing a variable that resolves to Default rather than passing Default directly, might you try passing Eval($v_Arg1) ??

Not a conclusion about whether this is the way it should work or not, but the answer would be instructive.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

This is a problem with the parser. Default is a keyword with value semantics (True and False also behave the same way). You can assign Default to any variable and that variable is supposed to be functionally equivalent to Default itself. I suspect the problem here is the parser looks for Default explicitly instead of looking to see if the variable itself is of type "Default" (simplification). IMO, it should work like the OP expects.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...