Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Embedded Windows Scripting (VBScript & JScript) and COM


  • Please log in to reply
68 replies to this topic
erictheturtle
  • Members
  • 101 posts
  • Last active: Sep 04 2011 02:07 PM
  • Joined: 27 Jun 2007
Thanks AHKnow for the very thorough review. Much of what you have expressed is exactly what I've thought myself. Those are some good links you've gathered as well.

I wish I could offer help with ADO, but I've never used it before. I know a simple google search brings up many examples.

This weekend I think I'll pull myself away from my other projects and update ws4ahk to a stdlib-ready version.
-m35

erictheturtle
  • Members
  • 101 posts
  • Last active: Sep 04 2011 02:07 PM
  • Joined: 27 Jun 2007
Major update of ws4ahk.

Now finally in the tenths digit: v0.11 :)

Full documentation also included.

Check out the home page for brief list of changes.
<!-- m -->https://ahknet.autoh...y.com/~easycom/<!-- m -->
-m35

AHKnow*
  • Guests
  • Last active:
  • Joined: --
That is awesome. AutoHotkey does come well, thanks to scripts like this and the author.

Great work!

Joy2DWorld
  • Members
  • 562 posts
  • Last active: Jun 30 2014 07:48 PM
  • Joined: 04 Dec 2006
two tiny suggest:

your library is "WS",

*everything* GLOBAL in that library should be "WS_"


so.. "__IUnknown_Release(ppv)" won't conflict with another "__IUnknown_Release()" function in someone's script, keep it within your library... ie. "WS__IUnknown_Release(ppv)"


this way,

there will NEVER BE A CONFLICT WITH ANY OTHER LIBRARY/FUNCTION..


include your webpage address in the function notes...


hope is helpful.
Joyce Jamce

WashBoard
  • Guests
  • Last active:
  • Joined: --
Hi, erictheturtle
I tried
WS_Initialize()
pWord := WS_GetObject("Word.Application")
clipboard := ErrorLevel . ";" . pWord
WS_UnInitialize()
and it works perfecly. But I think you know is it possible, in VBScript, to specify a file name to know if the file is loaded in Word (in this case). The syntax of GetObject is : GetObject([pathname] [, class])
So, if pWord := WS_GetObject("Word.Application") works great,
pWord := WS_GetObject("Z:\Try.doc") should work,
and pWord := WS_GetObject("Z:\Try.doc,Word.Application") too, but no one of the two last forms works.
The error code in ErrorLevel is "-2147221005 (800401F3) incorrect class string"
Please where is the problem ? Is it a problem with the Microsoft Scripting Control ?
Is there a solution ? If it is, which one please ?
Thanks by advance

erictheturtle
  • Members
  • 101 posts
  • Last active: Sep 04 2011 02:07 PM
  • Joined: 27 Jun 2007
Sorry WashBoard, ws4ahk's WS_GetObject() function doesn't provide a way to create an object from a file.

ws4ahk syntax:
WS_GetObject(sProgIDorClassID [, sInterfaceID = IDispatch ] ) ; the sInterfaceID parameter can usually be ignored
VB syntax:
GetObject([pathname] [, class])

The work around would be to use VBScript to get the object.

WS_Eval(pWord, "GetObject(""Try.doc"")")


; could also be written other ways:

; * put the code in a variable before evaluating it (no need to escape quotes)
sCreateObjCode = 
(
    GetObject("Try.doc")
)
WS_Eval(pWord, sCreateObjCode)

; * use the printf style (if you're comfortable with it)
; woops! a ws4ahk bug prevents this from working. 
; Edit: this is now fixed in v0.12!
WS_Eval(pWord, "GetObject(%s)", "Try.doc")

; * using the VBStr function (not helpful in this case)
WS_Eval(pWord, "GetObject(" . VBStr("Try.doc") . ")")


I spent about an hour searching for what VB's GetObject() function does internally, but couldn't find anything except this little snippit
[quote name="http://www.cgoakley.demon.co.uk/prog/oleaut.html"]GetObject will not be explored here. Suffice it to say that it uses non-automation OLE interfaces to read an object from a file, or other persistent storage.[/quote]
It also seems that Python's COM libraries don't provide the VB style GetObject function either. Could this functionality be so complicated that even Python doesn't mess with it?

So I have no idea how VB's can get an object from a file. If anyone knows, maybe this feature can be added to ws4ahk.
-m35

Washboard
  • Guests
  • Last active:
  • Joined: --
Thanks, erictheturtle, for your works.
The first one, of course, Ws4Ahk.
The second one, your work on my question. Your aswer is very instructive.

I made, too, some trys.
After a certain number of trys, the syntax
GetObject([pathname] [, class])
is effectively not supported by Ws4Ahk, but this one
GetObject([pathname])
yes (I made many trys...).

My results :
With the syntax :
myDoc := "D:\Try.doc"
ppWord := WS_Eval(pWord, "GetObject(%s)", myDoc)
or
ppWord := WS_Eval(pWord, "GetObject(%s)", VBStr(myDoc))
I had no success.

With the syntax :
myDoc := "D:\Try.doc"
ppWord := WS_Eval(pWord, "GetObject(" . VBStr(myDoc) . ")")
, if Word is launched and it has the file loaded in it, success...
But, surprise, if Word is launched but empty or when Word is not launched at all, the result is ... success !!! If Word is empty, the file isn't loaded in it...
So GetObject seems to try to launch the file in Word but seems to not do it... Strange...
If I try with a doc file with macros in it, I have the window to choose to activate them or not. When I go to Word to see if the file is loaded, I don't see anything... Is it loaded in invisibly mode ? I don't know... I can make any choice (activate the macros or not, cancel), the file is not displayed in Word if it is launched... But the same window (choice to activate macros or not) appears even if there isn't any Word instance launched...

With the syntax
myDoc := "D:\Try.doc"
sCreateObjCode =
(
    GetObject("%myDoc%")
)
ppWord := WS_Eval(pWord, sCreateObjCode)
the results are the same.

So for me the solution is probably to verify with AHK commands if there is a Word window which exists with the file name I want to use, and after to use one of these syntaxes to act on the file if it is already loaded in Word.

Thanks again, erictheturtle, for your beautifull work and your nice help.

erictheturtle
  • Members
  • 101 posts
  • Last active: Sep 04 2011 02:07 PM
  • Joined: 27 Jun 2007
Thank you Washboard, I'm glad you're finding some use in ws4ahk.

With the syntax :

myDoc := "D:\Try.doc"
ppWord := WS_Eval(pWord, "GetObject(%s)", myDoc) [color=green]; correct[/color]
or
ppWord := WS_Eval(pWord, "GetObject(%s)", VBStr(myDoc)) [color=red]; xx incorrect[/color]
I had no success.

The first syntax is the correct one. Did you try this with the new v0.12? It should work the same as your other examples now.

With the syntax :

myDoc := "D:\Try.doc"
ppWord := WS_Eval(pWord, "GetObject(" . VBStr(myDoc) . ")")
...
With the syntax
myDoc := "D:\Try.doc"
sCreateObjCode =
(
    GetObject("%myDoc%")
)
ppWord := WS_Eval(pWord, sCreateObjCode)
the results are the same.

I didn't specifically mention it in my previous post, but all 3 of those bits of Autohotkey code will execute the exact same VBScript code. I just provided the 3 different AHK syntaxes to give you (and others) options. Use whichever one you are most comfortable with.

While all of your examples will work, I see you use "ppWord := WS_Eval(...)". The syntax of WS_Eval is
blnSuccess := WS_Eval(ByRef ReturnValue, sScriptCode [, values for %v or %s])
so after the call, ppWord will hold True or False. Is this what you were intending?

Washboard
  • Guests
  • Last active:
  • Joined: --

Did you try this with the new v0.12? It should work the same as your other examples now.

I see it now. I will try.

I just provided the 3 different AHK syntaxes to give you (and others) options. Use whichever one you are most comfortable with.

I understood. Thanks for giving us the choice. IMO, I prefer the second syntax...

so after the call, ppWord will hold True or False. Is this what you were intending?

Yes absolutely, for try purpose. I put pWord, ppWord and ErrorLevel in the clipboard to test them and see what are the return values.

Concerning the strange behavior I spoke about (when you test if the file is loaded in Word and there isn't any active Word instance or an instance without the file loaded in it, it seems that there are trys to load the file and the result of the function is positive), it seems that it's a VBScript itself behavior.

tank
  • Guests
  • Last active:
  • Joined: --
@erictheturtle

Im absolutely enthralled
one very important question
#Include ws4ahk.ahk
WS_Initialize()
Code = 
(
Dim foo1
foo1 = "bar"
)
WS_Exec(Code)

this will of course create a visual basic variable called foo1
i would like to retreive the value "bar" and use it in ahk script
conceptually to set a edit feild with the value arrived at from vb
unfortuanately this is part of something larger im working and vb is the only language i can interact with another application with
(local pc policy) if that even sounds reasonable

erictheturtle
  • Members
  • 101 posts
  • Last active: Sep 04 2011 02:07 PM
  • Joined: 27 Jun 2007
If I understand what you are saying, then the WS_Eval() function is what you need.

#Include ws4ahk.ahk
WS_Initialize()
Code =
(
Dim foo1
foo1 = "bar"
)
WS_Exec(Code)

WS_Eval(getval, "foo1")

Msgbox % getval ; displays a message box with 'bar'
Hope this provides what you are looking for.

Note: I'm out of town for winter vacation starting tomorrow, so I can't say when I may be able to post additional replies. If I'm not back before new years, happy holidays everyone!
-m35

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
erictheturtle, you are definitely a man that knows his business
You did accurately interpret my question
FYI
I have VB code almost 2000 lines for interacting with a very archaic system and only respond well to VB
Some of the functions scrape data off a terminal application and store those values in a variable.
Without this answer i was going to have to abandon my current project all together, as some of these values were for error checking within the app. Others were to post results to a GUI form I’m creating with AHK
The app in question will be used in a call center of a bank.
:D :p
Never lose.
WIN or LEARN.

erictheturtle
  • Members
  • 101 posts
  • Last active: Sep 04 2011 02:07 PM
  • Joined: 27 Jun 2007
Glad you've making progress with your program tank.

ws4ahk has been updated v0.13, which adds some missing error handling, and a few comments improvements/corrections.

Don't forget, ws4ahk is still considered beta software. While I try my best to ensure quality, it can't be guaranteed to be wholly reliable.
-m35

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
well understood however i test every script I write to fault. actually given the extent that I plan to utilize it ill follow up with you if I find anything in the way of consistent unusually behavior. Some of my vb code will be moved to ahk script.
Never lose.
WIN or LEARN.

erictheturtle
  • Members
  • 101 posts
  • Last active: Sep 04 2011 02:07 PM
  • Joined: 27 Jun 2007
ws4ahk v0.20

- WS_Exec()/WS_Eval(): Better handling of errors.
Removed printf() style functionality, moved to codef() function.
Removed leftover Clipboard debug.
- codef(): New function to handle printf() style formatting of code.
Also fixes the bug if in hex mode.

Note that if you are using the printf style in WS_Exec or WS_Eval (I doubt anyone is), you will need to slightly modify your code to use the new codef() function.
-m35