OfficeInfo - MS Office COM constants

Post your working scripts, libraries and tools for AHK v1.1 and older
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

OfficeInfo - MS Office COM constants

03 Sep 2016, 19:02

OfficeInfo - MS Office COM constants
Get values of Microsoft Office constants for use with COM.

Tested with Office 2007 & 2010

ImportTypeLib is required. (In the example scripts, the folder "maul-esel-ImportTypeLib-28e9e8b" is in the same directory as the script. 「More Info 」 )

Example Usage: Show available constants
Spoiler
Example Usage: Excel - Add and format a table
Spoiler
OfficeInfo Class:

Code: Select all

; https://autohotkey.com/boards/viewtopic.php?f=6&t=23164
; Updated: Sept. 7, 2016
class OfficeInfo
{
    __New()
    {
        static Programs := { "Excel":      "{00020813-0000-0000-C000-000000000046}"
                           , "Office":     "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}"
                           , "Outlook":    "{00062FFF-0000-0000-C000-000000000046}"
                           , "PowerPoint": "{91493440-5A91-11CF-8700-00AA0060263B}"
                           , "Publisher":  "{0002123C-0000-0000-C000-000000000046}"
                           , "Word":       "{00020905-0000-0000-C000-000000000046}" }
        ObjRawSet(this, "_Unlocked", true)
        for ProgramName, ProgramGUID in Programs
            new this._ProgramInfo(ProgramGUID, this)
        this.Delete("_Unlocked")
    }
    
    __Get(aName)
    {
        if !this.HasKey("_Unlocked")
            throw Exception( "`nThe requested key does not exist. Specifically:`n`n"
                           . "Key: " aName )
    }
    
    __Set(aName, aValue)
    {
        if !this.HasKey("_Unlocked")
            throw Exception( "`nCannot create new keys. Specifically:`n`n"
                           . "Key: "   aName "`n"
                           . "Value: " aValue )
        else
            ObjRawSet(this, aName, aValue)
        return this[aName]
    }
    
    class _ProgramInfo
    {
        __New(GUID, Parent)
        {
            this.GUID := GUID
            this.Parent := Parent
            this.GetTypeLibVersion()
            this.GetTypeLib()
            this.GetFlatTypeLib()
        }

        GetTypeLibVersion()
        {
            Loop, Reg, % "HKCR\TypeLib\" this.GUID, K
            {
                for i, VerPart in StrSplit(A_LoopRegName, ".")
                {
                    if VerPart is integer
                        Version .= VerPart "."
                    else if VerPart is xdigit
                        Version .= Format("{:i}", "0x" VerPart) "."
                }
                return this.Version := RTrim(Version, ".")
            }
        }
        
        GetTypeLib()
        {
            try return this.TypeLib := ImportTypeLib(this.GUID, this.Version)
            catch e
                OutputDebug, % "Failed to load type library!`n" 
                             . "GUID: "    this.GUID    "`n"
                             . "Version: " this.Version "`n"
                             . "Message: " e.Message    "`n"
                             . "What: "    e.What       "`n"
                             . "Extra: "   e.Extra      "`n"
                             . "File: "    e.File       "`n"
                             . "Line: "    e.Line       "`n"
        }
        
        GetFlatTypeLib()
        {
            this.Enumerate(this.TypeLib)
            return this.Parent
        }
        
        ; Get each item in the type library and place it in the parent OfficeInfo object.
        Enumerate(a, Depth:=2)
        {
            try for k, v in a
            {
                if IsObject(v) && Depth > 1
                    this.Enumerate(v, Depth - 1)
                else if !this.Parent[k] && !ITL.Properties.IsInternalProperty(k)
                    this.Parent[k] := v  ; store retrieved value in parent
            }
        }
    }
}
Change Log:
Spoiler
Last edited by kon on 07 Oct 2016, 22:12, edited 1 time in total.
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: OfficeInfo - MS Office COM constants

07 Oct 2016, 18:49

I had trouble finding ImportTypeLib from the forum link above, I believe it is here -- I'm still not sure which files to grab and load but I'll figure that out eventually.

I was also a bit slow trying to figure out exactly what this script was supposed to do, but it LOOKS LIKE it provides all of the special enum constants that would normally be used during VBA programming, without having to go into VBA or MSDN docs to get the value and assign it manually.

So if my understanding is correct, these descriptive xlWhatEver variables in your example code get automatically populated with the right constant values:
  • c.xlSortOnValues
  • c.xlDescending
  • c.xlSortNormal
That looks awesome :dance:

Especially translating VBA code or examples to run in AutoHotkey where there are dozens of constants used... this could be a big timesaver.

Can't wait to get this running when I have a chance... thanks for posting.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: OfficeInfo - MS Office COM constants

07 Oct 2016, 19:35

To set it up exactly like the example scripts: go to the ImportTypeLib page and look for the link "Download Project (for developers)". The link should download a zip file that (at the time of this post) contains a folder named "maul-esel-ImportTypeLib-28e9e8b." Place that folder in the same dir as your script.

For the example script "Example - Show available constants.ahk" the file structure should look like this:

Code: Select all

....\%A_ScriptDir%\
........Example - Show available constants.ahk
........OfficeInfo.ahk
........maul-esel-ImportTypeLib-28e9e8b\                 | <-- Files below this line are part of ITL.
............Lib\
................ITL_FAILED.ahk
................ITL_FormatError.ahk
................ITL_FormatException.ahk
................etc.
............Misc\
................genLib.ahk
................GetParamInfo.ahk
................test.ahk
................Viewer.ahk
............ImportTypeLib.ahk
............ITL.ahk
............ITL_AbstractClassConstructor.ahk
............etc.
In the example scripts, this line changes the working directory used by #Include:
#Include %A_ScriptDir%\maul-esel-ImportTypeLib-28e9e8b
#Include wrote:Directory: Specify a directory instead of a file to change the working directory used by all subsequent occurrences of #Include
Then this line includes "Main.ahk" from ImportTypeLib:
#Include Main.ahk

Then "Main.ahk" #Includes some more files (this is why the working directory was changed).
You could also get the files from the github page. Just the folder name may be different.

I'm interested to hear how it works for you. As far as I know I'm the only one who has tried this so far, and I haven't used it extensively.
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: OfficeInfo - MS Office COM constants

08 Oct 2016, 03:05

I tried the examples. It works for Windows 10.0.14393, x64, Office Home 2007, AutoHotkey 1.1.24.01
Thank you very much!
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: OfficeInfo - MS Office COM constants

09 Oct 2016, 09:32

Thanks :) I hadn't tried it on Win10 yet.
hotkeyguy
Posts: 170
Joined: 11 Oct 2014, 12:22

Re: OfficeInfo - MS Office COM constants

20 Oct 2016, 18:53

Hello kon,

I reduced Programs just to Office

Code: Select all

static Programs := { "Office":       "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}" }
and got the following errors via (modified) OutputDebug messages:
** Program: Office **
=> Failed to load type library!
-> GUID: {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}
-> Version: 2.7
-> Message: Failed to load type library.
-> What: ImportTypeLib
-> Extra: LIBID "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}" could not be converted.
A_LastError: 1008 - Translated: Attempt to access an non-exisiting token.
HRESULT: -2147221005 - Translated: Invalid class string
-> File: ...\OfficeInfo.ahk
-> Line: 76


I'm using a non-English Win 7, 64 bit. Any hints? EDIT: I'm using Office 2010, so what's the correct version?


Many thanks and greetings
hotkeyguy


Maybe usefull for searching for the CLSIDs in your Registry RegScanner: Alternative to RegEdit find/search/scan of Windows and especially another tool RegDllView - Register dll/ocx/exe utility.
Last edited by hotkeyguy on 21 Oct 2016, 18:05, edited 3 times in total.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: OfficeInfo - MS Office COM constants

20 Oct 2016, 19:37

Hi :)

ImportTypeLib comes with "Viewer.ahk" which will show you the type libs in your registry. Does the GUID and Version of Office match what you are seeing in OutputDebug? Are there any other GUIDs for Office, or versions other than 2.7?

It seems to be getting a version number (2.7), so that's good... If you look in the registry at "HKCR\TypeLib\{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}" - are there any other versions other than 2.7?

What version of Office are you using? (2007, 2010 etc.)

OutputDebug is showing the line number from OfficeInfo.ahk... If you load the GUID with ImportTypeLib directly it may show you where exactly in ImportTypelib the error is coming from...
MyLib := ImportTypeLib(GUID, Version)

I'm not sure if it makes a difference that you are using a non-English version of Windows. At least I can't think of any problems that could cause.
hotkeyguy
Posts: 170
Joined: 11 Oct 2014, 12:22

Re: OfficeInfo - MS Office COM constants

21 Oct 2016, 18:32

Hello kon,

many thanks for your fast answer!
ImportTypeLib comes with "Viewer.ahk" which will show you the type libs in your registry. Does the GUID and Version of Office match what you are seeing in OutputDebug? Are there any other GUIDs for Office, or
versions other than 2.7?
Thanks for that hint. When I start Viewer.ahk the following messages are shown again and again: -2147221005 - Translated: Invalid class string. I don't get those message windows closed. It's the same error as mentioned.
It seems to be getting a version number (2.7), so that's good... If you look in the registry at "HKCR\TypeLib\{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}" - are there any other versions other than 2.7?
I have entries for 2.3, 2.5, 2.6, 2.7.
What version of Office are you using? (2007, 2010 etc.)
Sorry, I forgot to mention that - question updated: I'm using Office 2010, so what's the correct version? I searched for it without success.
I'm working on a developer PC, so there may be e. g. fragments from tests.
OutputDebug is showing the line number from OfficeInfo.ahk... If you load the GUID with ImportTypeLib directly it may show you where exactly in ImportTypelib the error is coming from...
MyLib := ImportTypeLib(GUID, Version)
Tried it with all versions (2.3, 2.5, 2.6, 2.7) - always the same problem.
I'm not sure if it makes a difference that you are using a non-English version of Windows. At least I can't think of any problems that could cause.
Same opinion.


Many thanks and greetings
hotkeyguy
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: OfficeInfo - MS Office COM constants

21 Oct 2016, 19:28

I just checked a machine with 2010 installed and it only has Office type lib version 2.5.

Registry key: HKEY_CLASSES_ROOT\TypeLib\{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2.5\0\win32
Value: C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE14\MSO.DLL

^If you look in your registry what path do you see? Is the DLL present at that path?

Just as a test, I am abe to load the type lib with the DLL path. No version number is required...

Code: Select all

for k, v in ImportTypeLib("C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE14\MSO.DLL")
    if IsObject(v)
        try for i, x in v
            if IsObject(x)
                continue
            else
                MsgBox, % "i: " i "`nx: " x
LIBID "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}" could not be converted.
If I'm not mistaken, this error is from ITL_GUID_FromString():

Code: Select all

		hr := ITL_GUID_FromString(lib, libid)
		if (ITL_FAILED(hr))
		{
			throw Exception(ITL_FormatException("Failed to load type library.", "LIBID """ lib """ could not be converted.", ErrorLevel, hr)*)
		}

Code: Select all

ITL_GUID_FromString(str, byRef mem)
{
	VarSetCapacity(mem, 16, 00)
	return DllCall("Ole32\CLSIDFromString", "Str", str, "Ptr", &mem)
}
I'm not sure what would cause that to fail though. I'll think about it some more...
hotkeyguy
Posts: 170
Joined: 11 Oct 2014, 12:22

Re: OfficeInfo - MS Office COM constants

22 Oct 2016, 19:21

Hello again kon,

many thanks for all your efforts!
Just as a test, I am abe to load the type lib with the DLL path. No version number is required...
Here my results with all version of my registry:
2.3 MSO.DLL - Office 2003, Version 11
HKEY_CLASSES_ROOT\TypeLib\{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2.3\0\win32
C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE11\MSO.DLL
HRESULT: -2147312566 - Translated: Error loading of the Typlibrary/DLL.
Invalid ITypeLibrary pointer: 0

2.5 MSO.DLL - Office 2010, Version 14
HKEY_CLASSES_ROOT\TypeLib\{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2.5\0\win32
C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE14\MSO.DLL
HRESULT: -2147312566 - Translated: Error loading of the Typlibrary/DLL.
Invalid ITypeLibrary pointer: 0

2.6 VBEUI.DLL
HKEY_CLASSES_ROOT\TypeLib\{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2.6\0\win32
C:\PROGRA~2\COMMON~1\MICROS~1\VBA\VBA7.1\VBEUI.DLL
NOT tested, even don't know what's that for an entry.

2.7 MSO.DLL - Office 2013, Version 15
HKEY_CLASSES_ROOT\TypeLib\{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2.7
C:\Program Files\Microsoft Office 15\root\VFS\ProgramFilesCommonX86\Microsoft Shared\Office15\MSO.DLL
HRESULT: -2147312566 - Translated: Error loading of the Typlibrary/DLL.
Invalid ITypeLibrary pointer: 0

I really don't understand that.


Many thanks and greetings
hotkeyguy

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: hiahkforum and 147 guests