COM Object Behavior Doesn't Match VBA

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

COM Object Behavior Doesn't Match VBA

30 Sep 2016, 20:39

With OneNote 2016, the method I was expecting to instantiate a COM Object doesn't work for me the same way it does in OneNote 2010. Furthermore, if I try what I understand to be as close to equivalent code as possible in VBA (using Excel), it works ok.

The following code works when executed in Excel VBA:

Code: Select all

Public Sub test()
    
    Dim onApp As OneNote.Application
    Set onApp = CreateObject("OneNote.Application")
    Debug.Print onApp.Windows.CurrentWindow.CurrentPageId

End Sub
Above routine will print something like below, which confirms that the Object is valid and accessing data as expected:
{19D95969-D617-4F12-8361-64E5A678C0B8}{1}{E1951646190563752473342929088298487209719561}

If I try what appears to be an equivalent in AutoHotkey however, there are problems.
The following code uses EnumComMembers from Lexicos to see if anything is there (spolier: there are no object members listed)

Code: Select all

obj := ComObjCreate(ProgID:="OneNote.Application")
    Members := EnumComMembers(obj)
    MsgBox % _ 
    . "ProgID="          quote(ProgID) "`n" _ 
    . "Interface: "      quote(ComObjType(obj)) "`n" _ 
    . "Interface name: " quote(ComObjType(obj, "name")) "`n" _
    . "Interface IID: "  quote(ComObjType(obj, "IID"))
Output of the MsgBox looks like so:
ProgID="OneNote.Application"
Interface: "9"
Interface name: ""
Interface IID: ""


Use "OneNote.Application.12" returns the following details, but still no object members:
ProgID="OneNote.Application.12"
Interface: "9"
Interface name: "IApplication"
Interface IID: "{2DA16203-3F58-404F-839D-E4CDE7DD0DED}"


I also looked in the registry to see which OneNote.Application.X items were registered--there were 3 of them (12, 13 and 15?) and I believe the standard "OneNote.Application" was mapped to 15. At any rate the results above were indicative for all of them.

Any ideas on how to get a valid COM Object returned with access for OneNote.Application?

Any other debugging ideas I could try in VBA or AHK?
lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: COM Object Behavior Doesn't Match VBA

01 Oct 2016, 01:04

Did you actually try MsgBox % obj.Windows.CurrentWindow.CurrentPageId?

The AutoHotkey code and VBA code you posted are not equivalent. The VBA code does not require the object to return type information. If an object does not return type information, that does not mean the object is invalid.
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: COM Object Behavior Doesn't Match VBA

02 Oct 2016, 02:11

The returned object is valid, it just doesn't appear to have any of the properties or methods available that would be normally be returned using the same syntax with OneNote 2010, or using the VBA code above with OneNote 2016.

For Office 2010: The items are enumerated correctly for the object returned (tested on a different machine with Office 2010):
  • ID, Name, Kind
  • 100, Windows, get
  • 102, Dummy1, get
  • 104, COMAddIns, get
  • 105, LanguageSettings, get
For Office 2010: MsgBox % obj.Windows.CurrentWindow.CurrentPageId ...yields... {04F9A47F-FEC1-48BE-A13F-73F629E1C852}{1}{B0}} (some pageId)
For Office 2016: MsgBox % obj.Windows.CurrentWindow.CurrentPageId ...yields... Error 0x8002006 - Unknown name. Specifically: Windows

And a slightly different error when trying to assign an object:

For Office 2016: objWin:=obj.Windows.CurrentWindow ... yields.. 0x8002801D - Library not registered

I also checked behavior on a second machine with a clean install of Office 2016 to try and ensure it wasn't an installation gone wrong, registry error, or some other potential error affecting one machine only. Both machines had identical behavior when running the same code.

The error above occurs for any attempts to access any methods or properties of the object, which is what led to subsequent troubleshooting...
  • Check for a valid object: IsObject(obj) → Yes, Equals 1
  • Enumerate properties and methods using EnumComMembers(obj) → None Returned
  • See if some kind of VBA code works → Yes, Details Above
  • Query interface details to see what comes back → Details Above (Null results do not match 2010 where an IID is returned and IApplication is returned for the Interface Name)
  • Check Running Object Table for OneNote 2016 items → Yes, two items listed, identical to OneNote 2010 items
    (For viewing the Running Object Table I am using Alax.Info ROT View to enumerate the Display Names)
  • Check that test code is not running as an privileged/UAC process (i.e., has access to the normal ROT) → Yes
lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: COM Object Behavior Doesn't Match VBA

02 Oct 2016, 06:27

JJohnston2 wrote:For Office 2016: MsgBox % obj.Windows.CurrentWindow.CurrentPageId ...yields... Error 0x8002006 - Unknown name. Specifically: Windows

And a slightly different error when trying to assign an object:

For Office 2016: objWin:=obj.Windows.CurrentWindow ... yields.. 0x8002801D - Library not registered
Are you sure about that? It doesn't make any sense. The first error indicates that evaluation halted at obj.Windows, because the name "Windows" could not be resolved. The second error indicates that either evaluation of obj.Windows succeeded and .CurrentWindow produced the error, or it failed but giving a different error despite being the exact same sub-expression. The assignment never comes into play unless you choose "Yes" to continue at the error dialog.

If you are using AutoHotkey 64-bit, I suggest trying 32-bit - or vice versa. COM objects are registered separately for 32-bit and 64-bit. VBA is most likely 32-bit.
By default, Office 2010 installs the 32-bit version. You must explicitly choose to install the 64-bit version during setup.
Source: 64-Bit Visual Basic for Applications Overview
(Possibly applies to later versions as well.)
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: COM Object Behavior Doesn't Match VBA

02 Oct 2016, 15:53

lexikos wrote:The second error indicates that either evaluation of obj.Windows succeeded and .CurrentWindow produced the error, or it failed but giving a different error despite being the exact same sub-expression.
FYI, attempted assignment of objWin:=obj.Windows also results in 0x8002801D - Library not registered

Looking at 32/64-bit differences is a great suggestion... pretty sure VBA is running 32-bit as noted (standard Office install) and 64-bit AutoHotkey, so that should be a straightforward check for behavioral differences running on 32-bit.

I haven't had any issues getting objects for the other Office programs with the 32/64-bit setup, but that code is different and gets an object via Accessibility Object From Window. Unfortunately I don't think OneNote supports that like the other Office programs do, but maybe that's another thing I'll go back and double-check... quite possible it's supported even though it's not documented on MSDN, as is the case with PowerPoint.
lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: COM Object Behavior Doesn't Match VBA

02 Oct 2016, 17:13

JJohnston2 wrote:FYI, attempted assignment of objWin:=obj.Windows also results in 0x8002801D - Library not registered
No. Maybe I'm splitting hairs, but that's not accurate. As I said, the assignment never comes into play unless you choose "Yes" to continue at the error dialog. If you choose "No" (or use try-catch), the assignment isn't even attempted. The sub-expression obj.Windows is what throws the error.

That is why I find it hard to believe MsgBox % obj.Windows.CurrentWindow.CurrentPageId throws "Unknown name" at obj.Windows. It should throw "Library not registered". Perhaps there is some other difference in the code (or outside the code) between your two tests.

If code that hasn't even executed yet affects the result, sometimes that's a sign that memory has been corrupted. In that case, anything goes.
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: COM Object Behavior Doesn't Match VBA

02 Oct 2016, 19:43

lexikos wrote:the assignment never comes into play unless you choose "Yes" to continue at the error dialog
Understood... it's not even making it to the assignment part of the statement at the time it throws an error. I was just pointing out that that particular line of code fails with the same error whether it's obj.Windows.CurrentWindow or obj.Windows.
lexikos wrote:Perhaps there is some other difference in the code
To prove or disprove this I rearranged the test code to get one error after the other. This quickly proved your hunch correct → Some other difference in the code (presumably from not keeping track of which test case was getting executed). Took a bit to figure out what it was, but turned out to be the Prog ID used since there are currently several listed in the test code--these were getting enabled or commented out along the way testing various possibilities.

So for OneNote.Application.12 both statements show Error: 0x80020006 - Unknown name
And for OneNote.Application both statements show Error: 0x8002801D - Library not registered

Running as a 32-bit compiled program appears to have identical results (and I inserted a MsgBox of A_ProgramFiles in just to confirm it was compiling with the right image--i.e., checked for (x86) to make sure I wasn't fooling myself and still running a 64-bit program on accident). The COM error is the same for the compiled code and the interface information returned is all identical.

I also double checked whether there are any OneNote controls with accessibility... there are a few, but none directly for the top-level Application object. I need to check now and see which objects are actually being returned (and whether any of them can return an Application object directly, or a parent [or other] object to use indirectly). Doesn't fix the original problem but might provide a suitable workaround.
lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: COM Object Behavior Doesn't Match VBA

02 Oct 2016, 20:05

D'oh! :idea:
Dim onApp As OneNote.Application
This is early binding. In other words, VBA has type information for OneNote, so is not using the IDispatch interface. Does it still work if you remove As OneNote.Application? Is OneNote listed in the VBA project references? What if you use similar code in VBScript?

Maybe the problem is like the error message tells you: registration of a specific OneNote type library is missing. It doesn't affect VBA, because VBA has a direct reference to that type library.
JJohnston2 wrote:I was just pointing out that that particular line of code fails with the same error whether it's obj.Windows.CurrentWindow or obj.Windows.
Of course. "Evaluation halted at obj.Windows" implies that .CurrentWindow wasn't evaluated, because the error was thrown before it could be. It's the same as the assignment.
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: COM Object Behavior Doesn't Match VBA

02 Oct 2016, 22:55

lexicos wrote:Does it still work if you remove As OneNote.Application?
The OneNote Object Library is listed in the VBA references from running previous test code in VBA, otherwise VBA will highlight the part of the Dim statement you mentioned and return Compile error: User-defined type not defined

Deleting altogether results in:

Run-time error '-2147319779 (8002801d)': Automation Error; Library not registered.

So pretty much what you were describing.

And in this case, the AutoHotkey error matches the VBA error, so all is right with the world (except for still not having any usable code for OneNote automation :( ).

Maybe it will still be achievable using one of the other objects identified using Accessible Object From Window.
lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: COM Object Behavior Doesn't Match VBA

03 Oct 2016, 22:17

You could use the OneNote interfaces via DllCall, but it's a lot more involved. (Search term: vtable.)
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: COM Object Behavior Doesn't Match VBA

04 Oct 2016, 11:39

I'll look into that, thanks.
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: COM Object Behavior Doesn't Match VBA

31 Jan 2018, 16:16

Have you had any success with automating oneNote?
ciao
toralf

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bassy, inseption86, Mannaia666, Pareidol and 152 guests