Sign in to follow this  
Followers 0
DaleHohm

Extract Enum Constants and create Au3 constants

8 posts in this topic

#1 ·  Posted (edited)

This function will find the appropriate typelib file and extract enumerated constants from it and assign them to AutoIt constants in your script. This is useful for scripts that use COM to interface with the likes of InternetExplorer.Application, Word.Application or Excel.Application (good for translating VBA samples or Office Macros into AutoIt).

Beware that it can take a bit to run (20 seconds or more sometimes) so you may want to generate an external include file instead of running this each time. See the next reply for a VBScript to generate an include file for VBS, Javascript or AutoIt. See second code block below for a script to generate an include file for VBS, Javascript or AutoIt (VBScript sample converted by JerryD).

Thanks to Sven for fixing a COM bug to make this work and for pointing out how to do the real-time constant assignment.

Dale

Edit: This code relies upon the redistributable file TlbInf32.dll which ships with Visual Studio and modern versions of Microsoft Office. For help on using this library, see here: Help Files for Tlbinf32.dll. Tlbinf32.dll can be found in many places on the web, here is one.. If you download the dll, make certaint that you also register it -- e.g. regsvr32 tlbinf32.dll

#cs
Func AssignCOMEnums($sApp [, $bShow])
    ABSTRACT:
        Retrieve the Enumerated Constants for an Applcation Class and assign their values to AutoIt3 global variables
        Example: AssignCOMEnums("Excel.application", 1)
    REQUIRED:
        $sApp - string containing the class name.  Ex. "Excel.application"
    OPTIONAL:
        $bShow - if set to 1, ConsoleWrite will display the resulting constants and their associated values
#ce
Func AssignCOMEnums($sApp, $bShow = 0)

Dim $oTLA, $oTLI, $CstEnum, $CstObj, $CstString, $sApp, $oApp, $bShow

$oApp   = ObjCreate($sApp)
$oTLA   = ObjCreate("TLI.TLIApplication")
$oTLI   = $oTLA.InterfaceInfoFromObject($oApp).Parent
    
For $CstEnum in $oTLI.Constants 
    If "_" <> StringLeft($CstEnum.Name, 1) Then 
        For $CstObj In $CstEnum.Members 
            Assign("$" & $CstObj.Name, $CstObj.Value, 2)
            If $bShow Then ConsoleWrite("$" & $CstObj.Name & " = " & Eval("$" & $CstObj.Name) & @CR)
        Next 
    EndIf 
Next

EndFuncoÝ÷ Ù'+m¢§z¶­y©âÉnuçâçè­PR%«ÚIÊâ¦Ú+ëh"Ö®¶­sb6æ6ÇVFRfÇCµ7G&æræS2fwC°¤÷Bb33´×W7DFV6Æ&Uf'2b33²Â¤vÆö&Âb33cµFFÆRÂb33cµ&W` ¤b7G&æu&vB67&DæÖRÂBÒb33²æWRb33²FVà¢b33cµ&WbÒfÆTvWEfW'6öâ67&DgVÆÅF¤VÇ6P¢b33cµ&WbÒb33³ããb33°¤VæD` ¢b33cµFFÆRÒb33µô76vä4ôÔVçV×5FW7BFW7Bb33²fײb33cµ&W` ¤7&VFTæ6ÇVFTfÆRb33´çFW&æWDWÆ÷&W"äÆ6Föâb33²ÂgV÷C¶S2gV÷C²Âb33´çFW&æWDWÆ÷&W"æS2b33²ÂG'VR ¤gVæ27&VFTæ6ÇVFTfÆRb33c·5&ötBÂb33c·67$BÂb33c·4÷WDfÆRÂb33c¶%6÷tÖÖVFFR¢FÒb33c·7G$6öç7G0¢b33c·7G$6öç7G2Ò&WGW&åFÆ$VçV×4g&öÕ&ötBb33c·5&ötBÂb33c·67$B¢bb33c·7G$6öç7G2Òb33²b33²FVâW@¢fÆUw&FRb33c·4÷WDfÆRÂb33c·7G$6öç7G2¢bb33c¶%6÷tÖÖVFFRFVà¢'VâgV÷C¶æ÷FWBgV÷C²fײb33c·4÷WDfÆR¢VæD`¤VæDgVæ2³ÓÒfwC´7&VFTæ6ÇVFTfÆR ¤gVæ2&WGW&åFÆ$VçV×4g&öÕ&ötBb33c·5&ötBÂb33c·67$B¢FÒb33cµDÄÂb33cµDÄÂb33c´77DVçVÒÂb33c´77Dö&¢Âb33c´77E7G&ærÒb33²b33²Âb33c´6ô6Ç2Âb33c·d77BÂb33c·dVæFÂÂb33c·d6×BÂb33c·e&P¢b33c´6ô6Ç2Òö&¤7&VFRb33c·5&ötB¢bäõB4ö&¢b33c´6ô6Ç2FVà¢×6t&÷Âb33cµFFÆRÂgV÷C´6÷VÆFâb33·B7&VFRö&¦V7Bb33c´6ô6Ç2g&öÒb33c·5&ötBgV÷C²fײb33c·5&ötBfײb33²b33²¢&WGW&âb33²b33°¢VæD`¢b33cµDÄÒö&¤7&VFRgV÷CµDÄåDÄÆ6FöâgV÷C²¢b4ö&¢b33cµDÄFVà¢b33cµDÄÒb33cµDÄäçFW&f6Tæfôg&öÔö&¦V7Bb33c´6ô6Ç2å&Vç@¢vWDÆæu&×2b33c·67$BÂb33c·d77BÂb33c·dVæFÂÂb33c·d6×BÂb33c·e&R¢b33c´77E7G&ærÒ'VÆDçG&òb33c·5&ötBÂb33cµDÄä6öçFæætfÆRÂb33c·d6×B¢f÷"b33c´77DVçVÒâb33cµDÄä6öç7FçG0¢bgV÷CµògV÷C²fÇC²fwC²7G&ætÆVgBb33c´77DVçVÒäæÖRÂFVà¢b33c´77E7G&ærÒb33c´77E7G&ærfײ5$Äbfײb33c·d6×Bfײb33c´77DVçVÒäæÖRfײ5$Ä`¢f÷"b33c´77Dö&¢âb33c´77DVçVÒäÖVÖ&W'0¢b33c´77E7G&ærÒb33c´77E7G&ærfײb33c·d77BfײgV÷C²gV÷C²fײb33c·e&Rfײb33c´77Dö&¢äæÖRfײgV÷C²ÒgV÷C²fײb33c´77Dö&¢åfÇVRfײb33c·dVæFÂfײ5$Ä`¢æW@¢VæD`¢æW@¢&WGW&âb33c´77E7G&æp¢VÇ6P¢×6t&÷Âb33cµFFÆRÂgV÷C´6âb33·B7&VFRö&¦V7Bb33cµDÄgV÷C²¢&WGW&âb33²b33°¢VæD`¤VæDgVæ2³ÓÒfwCµ&WGW&åFÆ$VçV×4g&öÕ&öt@ ¤gVæ2vWDÆæu&×2b33c·67$BÂ'&Vbb33c·d77BÂ'&Vbb33c·dVæFÂÂ'&Vbb33c·d6×BÂ'&Vbb33c·e&R¢Æö6Âb33c·466U6VÆV7BÒ7G&ætÆ÷vW"b33c·67$B¢6VÆV7@¢66Rb33c·466U6VÆV7BÒgV÷C·f'2gV÷C³²ÂgV÷C·f'67&BgV÷C³ ¢b33c·d77BÒgV÷C´6öç7BgV÷C°¢b33c·dVæFÂÒgV÷C²gV÷C°¢b33c·d6×BÒgV÷C²b33²gV÷C°¢b33c·e&RÒgV÷C²gV÷C°¢66Rb33c·466U6VÆV7BÒgV÷C¶§2gV÷C³²ÂgV÷C¶§67&BgV÷C³ ¢b33c·d77BÒgV÷C·f"gV÷C°¢b33c·dVæFÂÒgV÷C³²gV÷C°¢b33c·d6×BÒgV÷C²òògV÷C°¢b33c·e&RÒgV÷C²gV÷C°¢66Rb33c·466U6VÆV7BÒgV÷C¶S2gV÷C³²ÂgV÷C¶WFöC2gV÷C³ ¢b33c·d77BÒgV÷C´6öç7BgV÷C°¢b33c·dVæFÂÒgV÷C²gV÷C°¢b33c·d6×BÒgV÷C³²gV÷C°¢b33c·e&RÒgV÷C²b33c²gV÷C°¢66RVÇ6P¢b33c·d77BÒgV÷C²gV÷C°¢b33c·dVæFÂÒgV÷C²gV÷C°¢b33c·d6×BÒgV÷C²gV÷C°¢b33c·e&RÒgV÷C²gV÷C°¢VæE6VÆV7@¤VæDgVæ2³ÓÒfwC¶vWDÆæu&×2  ¤gVæ2'VÆDçG&òb33c·5&ötBÂb33c·7'bÂb33c·d6×B¢&WGW&âb33c·d6×Bfײõ7G&æu&WVBcÂgV÷C²¢gV÷C²fײ5$Äbfײb33c·d6×BfײgV÷C´VçVÖW&FVB6öç7FçG2f÷"6ô6Æ72gV÷C²gV÷C²gV÷C²fײb33c·5&ötBfײgV÷C²gV÷C²gV÷C²gV÷C²fײ5$Äbð¢fײb33c·d6×BfײgV÷C´WG&7FVBg&öÒgV÷C²gV÷C²gV÷C²fײb33c·7'bfײgV÷C²gV÷C²gV÷C²gV÷C²fײ5$Äbfײb33c·d6×Bfײõ7G&æu&WVBcÂgV÷C²¢gV÷C²fײ5$Äb¤VæDgVæ2³ÓÒfwC´'VÆDçG&
Edited by DaleHohm
1 person likes this

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This VBScript will generate an include file that you can use with AutoIt, VBScript or Javascript to get the values for Enumerated constants.

This was adapted from an example I found in another scripting forum...

Edit: See the base note for information on the necessary Tlbinf32.dll

Edit: An example output file from this routine for Excel has been attached

Option Explicit 


Dim fs 
Set fs = CreateObject("Scripting.FileSystemObject") 

'Create IncludeFiles - uncomment one of these lines or make your own
'CreateIncludeFile "Excel.Application", "vbs", "C:\xlsConsts.vbs", false
'CreateIncludeFile "Excel.Application", "js",  "C:\xlsConsts.js", false
CreateIncludeFile "Excel.Application", "au3", "C:\xlsConsts.au3", true 
'CreateIncludeFile "MindManager.Application", "au3", "C:\MindManagerConsts.au3", false

Sub CreateIncludeFile (sProgID, scrID, sOutFile, bShowImmediate) 
  Dim strConsts 
  strConsts = ReturnTlbEnumsFromProgId (sProgID, scrID) 
  fs.CreateTextFile(sOutFile, true, 0).Write strConsts 
  If bShowImmediate Then CreateObject("WScript.Shell").Run "notepad " & sOutFile 
End Sub 


Function ReturnTlbEnumsFromProgId (sProgId, scrID) 
  Dim TLA, TLI, CstEnum, CstObj, CstString, CoCls, vCst, vEndl, vCmt, vPre 
  Set CoCls = CreateObject(sProgId) 
  Set TLA   = CreateObject("TLI.TLIApplication") 
  Set TLI   = TLA.InterfaceInfoFromObject(CoCls).Parent 
  Call getLangParams (scrID, vCst, vEndl, vCmt, vPre) 
  CstString = BuildIntro (sProgId, TLI.ContainingFile, vCmt) 
  For each CstEnum in TLI.Constants 
    If "_" <> Left(CstEnum.Name, 1) Then 
      CstString = CstString & vbCrlf & vCmt & CstEnum.Name & vbCrlf 
      For Each CstObj In CstEnum.Members 
        CstString = CstString & vCst & " " & vPre & CstObj.Name _ 
                  & " = " & CstObj.Value & vEndl & vbCrLf 
      Next 
    End If 
  Next 
  ReturnTlbEnumsFromProgId = CstString 
End Function 


Sub getLangParams (ByVal scrID, ByRef vCst, ByRef vEndl, ByRef vCmt, ByRef vPre) 
  Select Case LCase(scrID) 
    Case "vbs", "vbscript": 
      vCst  = "Const" : vEndl = ""   : vCmt = "'" : vPre = ""
    Case "js", "jscript": 
      vCst  = "var"   : vEndl = ";"  : vCmt = "//" : vPre = ""
    Case "au3", "autoit3":
      vCst  = "Const" : vEndl = ""   : vCmt = ";" : vPre = "$"
    Case Else 
      vCst  = ""      : vEndl = ""   : vCmt = ""  : vPre = ""
    End Select 
End Sub 


Function BuildIntro (sProgId, srv, vCmt) 
  BuildIntro = vCmt & String (60, "*") & vbCrLf _ 
    & vCmt & "Enumerated Constants for CoClass """ & sProgID & """" & vbCrLf _ 
    & vCmt & "Extracted from """ & srv & """" & vbCrLf _ 
    & vCmt & String (60, "*") & vbCrLf 
End Function

xlsConsts.au3

Edited by DaleHohm

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

Share this post


Link to post
Share on other sites

THANKS SO MUCH! I have been looking for something like this.

Okay, now I am greedy - can you also enumerate methods/properties ?

Share this post


Link to post
Share on other sites

THANKS SO MUCH!  I have been looking for something like this.

Okay, now I am greedy - can you also enumerate methods/properties ?

<{POST_SNAPBACK}>

Go ahead and be greedy :(

I haven't gone any further because this is what I needed. It looks like TLA can do it though... take a look here: CodeProject ObjViewer

I'd be happy to see enhancements added...

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

Share this post


Link to post
Share on other sites

Sorry for geing so ignorant, but does this code work in v3.1.0 ? I couldn't find the ObjCreate() api in 3.1.0.

Share this post


Link to post
Share on other sites

Sorry, I'm usually careful to point this out... you need V3.1.1++ from the developer forum to get the ObjCreate COM support.

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

Share this post


Link to post
Share on other sites

Very cool stuff here.

I managed to convert DaleHohm's VB code into AutoIt:

#include <String.au3>
Opt('MustDeclareVars', 1)
Global $Title, $Rev

If StringRight(@ScriptName, 4) = '.exe' Then
    $Rev = FileGetVersion(@ScriptFullPath)
Else
    $Rev = '1.0.0'
EndIf

$Title = '_AssignCOMEnumsTest Test ' & $Rev

CreateIncludeFile('InternetExplorer.Application', "au3", 'InternetExplorer.au3', True)

Func CreateIncludeFile($sProgID, $scrID, $sOutFile, $bShowImmediate)
    Dim $strConsts
    $strConsts = ReturnTlbEnumsFromProgId($sProgID, $scrID)
    If $strConsts = '' Then Exit
    FileWrite($sOutFile, $strConsts)
    If $bShowImmediate Then
        Run("notepad " & $sOutFile)
    EndIf
EndFunc  ;==>CreateIncludeFile 

Func ReturnTlbEnumsFromProgId($sProgID, $scrID)
    Dim $TLA, $TLI, $CstEnum, $CstObj, $CstString = '', $CoCls, $vCst, $vEndl, $vCmt, $vPre
    $CoCls = ObjCreate($sProgID)
    If NOT IsObj($CoCls) Then
        MsgBox(0, $Title, "Couldn't create object $CoCls from $sProgID (" & $sProgID & ')')
        Return ''
    EndIf
    $TLA = ObjCreate("TLI.TLIApplication")
    If IsObj($TLA) Then
        $TLI = $TLA.InterfaceInfoFromObject ($CoCls).Parent
        getLangParams($scrID, $vCst, $vEndl, $vCmt, $vPre)
        $CstString = BuildIntro($sProgID, $TLI.ContainingFile, $vCmt)
        For $CstEnum In $TLI.Constants
            If "_" <> StringLeft($CstEnum.Name, 1) Then
                $CstString = $CstString & @CRLF & $vCmt & $CstEnum.Name & @CRLF
                For $CstObj In $CstEnum.Members
                    $CstString = $CstString & $vCst & " " & $vPre & $CstObj.Name & " = " & $CstObj.Value & $vEndl & @CRLF
                Next
            EndIf
        Next
        Return $CstString
    Else
        MsgBox(0, $Title, "Can't create object $TLA")
        Return ''
    EndIf
EndFunc  ;==>ReturnTlbEnumsFromProgId

Func getLangParams($scrID, ByRef $vCst, ByRef $vEndl, ByRef $vCmt, ByRef $vPre)
    Local $sCaseSelect = StringLower($scrID)
    Select
        Case $sCaseSelect = "vbs";, "vbscript":
            $vCst = "Const"
            $vEndl = ""
            $vCmt = "'"
            $vPre = ""
        Case $sCaseSelect = "js"; , "jscript":
            $vCst = "var"
            $vEndl = ";"
            $vCmt = "//"
            $vPre = ""
        Case $sCaseSelect = "au3";, "autoit3":
            $vCst = "Const"
            $vEndl = ""
            $vCmt = ";"
            $vPre = "$"
        Case Else
            $vCst = ""
            $vEndl = ""
            $vCmt = ""
            $vPre = ""
    EndSelect
EndFunc  ;==>getLangParams 


Func BuildIntro($sProgID, $srv, $vCmt)
    Return $vCmt & _StringRepeat(60, "*") & @CRLF & $vCmt & "Enumerated Constants for CoClass """ & $sProgID & """" & @CRLF _
             & $vCmt & "Extracted from """ & $srv & """" & @CRLF & $vCmt & _StringRepeat(60, "*") & @CRLF 
EndFunc  ;==>BuildIntro

OK, you guys might not be impressed, but I don't really know VB, so I'm fairly pleased with myself! :lmao:

Anyway, while I'm here, I have two questions.

First, is there any way to enumerate all available COM objects? You'll notice I didn't use the Excel example because I don't have it installed, and it failed. I really wanted to present a drop down list of available COM objects to generate the include file for (and more stuff too).

Second question - I've seen references for this ObjViewer before, but the only thing posted at the link above is the source (as far as I can tell). Other than the MS SDK Object viewer, is there one a little more friendly available?

Thanks.

Share this post


Link to post
Share on other sites

OK, you guys might not be impressed, but I don't really know VB, so I'm fairly pleased with myself! :lmao:

Anyway, while I'm here, I have two questions.

First, is there any way to enumerate all available COM objects? You'll notice I didn't use the Excel example because I don't have it installed, and it failed. I really wanted to present a drop down list of available COM objects to generate the include file for (and more stuff too).

Second question - I've seen references for this ObjViewer before, but the only thing posted at the link above is the source (as far as I can tell). Other than the MS SDK Object viewer, is there one a little more friendly available?

Thanks.

Thanks Jerry... I can now take taht conversion task off of my Someday/Maybe list ;)

Regarding the Object Viewer, it gets a lot easier if you read the nice writeup that SVenp did for it in the Obj/COM section of the helpfile -- pictures with circles and arrows and all...

kjactive is also working on a viewer (see this post) -- the thread includes some sample code from Sven that is a derivitaive of the code here in this post.

There are other viewers that are part of Microsost Office and Visual Studio... there are others you can buy and there may well be other freeware ones now, but this was the best I could find when I wrote this post.

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0