VBA to AHK translation – SetMapping error type mismatch Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bachelar
Posts: 28
Joined: 26 Aug 2020, 12:30

VBA to AHK translation – SetMapping error type mismatch

Post by Bachelar » 19 May 2022, 12:33

Hi!

I'm looking for help in translating a piece of code from Word VBA to AHK. What I've gathered so far is attached below and is all well, besides the last step that return error 0x80020005 – type mismatch. I pretty sure I've seen somewhere mentions that „SetMapping” is unreliable, but I cannot find it. Should be mentioned, that VBA macro is working just fine.

VBA:

Code: Select all

Sub Macro1()
  Dim aCC As ContentControl, xPart As CustomXMLPart
  Set aCC = ActiveDocument.ContentControls.Add(wdContentControlText)
  aCC.Title = "Title"
  Set xPart = ActiveDocument.CustomXMLParts.SelectByNamespace("http://schemas.openxmlformats.org/package/2006/metadata/core-properties")(1)
  aCC.XMLMapping.SetMapping XPath:="/ns1:coreProperties[1]/ns0:title[1]", Source:=xPart
End Sub

AHK:

Code: Select all

    oWord := ComObjActive("Word.Application")
    acc := oWord.ActiveDocument.ContentControls.Add(wdContentControlText := 1)
    acc.Title := "Title"
    xPart := oWord.ActiveDocument.CustomXMLParts.SelectByNamespace("http://schemas.openxmlformats.org/package/2006/metadata/core-properties")(1)
    acc.XMLMapping.SetMapping(XPath := "/ns1:coreProperties[1]/ns0:title[1]", Source := xPart)

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: VBA to AHK translation – SetMapping error type mismatch

Post by flyingDman » 19 May 2022, 13:39

acc.XMLMapping.SetMapping(XPath := "/ns1:coreProperties[1]/ns0:title[1]",, Source := xPart) add a comma?
14.3 & 1.3.7

Bachelar
Posts: 28
Joined: 26 Aug 2020, 12:30

Re: VBA to AHK translation – SetMapping error type mismatch

Post by Bachelar » 19 May 2022, 14:10

Unfortunately, adding a comma did nothing and the error is still thrown.

colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: VBA to AHK translation – SetMapping error type mismatch

Post by colt » 19 May 2022, 16:52

Both your vba and ahk code produce the same result in word for me.
It seems to execute but throws an error. Just put it in a try catch wrapper to ignore the error.
I am probably missing something tho...

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: VBA to AHK translation – SetMapping error type mismatch  Topic is solved

Post by FanaticGuru » 19 May 2022, 18:02

Bachelar wrote:
19 May 2022, 14:10
Unfortunately, adding a comma did nothing and the error is still thrown.

Adding a comma did something and is certainly needed.

MS documentation:
expression. SetMapping( _XPath_ , _PrefixMapping_ , _Source_ )

AHK does not have named parameters so you got to get the parameters in the right slot position. The XPath := and Source := are not really needed in AHK. In VBA it means something and has an effect, in AHK it is just kind of like active commenting.

acc.XMLMapping.SetMapping("/ns1:coreProperties[1]/ns0:title[1]",,xPart) is fine for AHK with each parameter in the correct position.

The comma fixed one problem. But there is still another problem. The xPart line needs an explicit Item property not just a default () reference.

This code works for me:

Code: Select all

oWord := ComObjActive("Word.Application")
oWord.ActiveDocument.BuiltInDocumentProperties("Title") := "FG Example" ; Give document title for example clarity
acc := oWord.ActiveDocument.ContentControls.Add(wdContentControlText := 1)
acc.Title := "Control Title"
xPart := oWord.ActiveDocument.CustomXMLParts.SelectByNamespace("http://schemas.openxmlformats.org/package/2006/metadata/core-properties").Item(1)
acc.XMLMapping.SetMapping("/ns1:coreProperties[1]/ns0:title[1]",,xPart)

FG
Last edited by FanaticGuru on 19 May 2022, 18:26, edited 2 times in total.
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: VBA to AHK translation – SetMapping error type mismatch

Post by FanaticGuru » 19 May 2022, 18:06

colt wrote:
19 May 2022, 16:52
Both your vba and ahk code produce the same result in word for me.
It seems to execute but throws an error. Just put it in a try catch wrapper to ignore the error.
I am probably missing something tho...

It only appears to work. The control is created but with the error it is not mapped to the Title information of the document. But if you have not set a Title in the document info then you will not really noticed. When properly mapped the control contents should change dynamically when the document Title is changed.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Bachelar
Posts: 28
Joined: 26 Aug 2020, 12:30

Re: VBA to AHK translation – SetMapping error type mismatch

Post by Bachelar » 20 May 2022, 10:53

FanaticGuru wrote:
19 May 2022, 18:02
Bachelar wrote:
19 May 2022, 14:10
Unfortunately, adding a comma did nothing and the error is still thrown.

Adding a comma did something and is certainly needed.

MS documentation:
expression. SetMapping( _XPath_ , _PrefixMapping_ , _Source_ )

AHK does not have named parameters so you got to get the parameters in the right slot position. The XPath := and Source := are not really needed in AHK. In VBA it means something and has an effect, in AHK it is just kind of like active commenting.

acc.XMLMapping.SetMapping("/ns1:coreProperties[1]/ns0:title[1]",,xPart) is fine for AHK with each parameter in the correct position.

The comma fixed one problem. But there is still another problem. The xPart line needs an explicit Item property not just a default () reference.

This code works for me:

Code: Select all

oWord := ComObjActive("Word.Application")
oWord.ActiveDocument.BuiltInDocumentProperties("Title") := "FG Example" ; Give document title for example clarity
acc := oWord.ActiveDocument.ContentControls.Add(wdContentControlText := 1)
acc.Title := "Control Title"
xPart := oWord.ActiveDocument.CustomXMLParts.SelectByNamespace("http://schemas.openxmlformats.org/package/2006/metadata/core-properties").Item(1)
acc.XMLMapping.SetMapping("/ns1:coreProperties[1]/ns0:title[1]",,xPart)

FG
Wow, thanks aplenty, that did the trick and clarified a lot. I will now go and have read of documentation and tutorials, because VBA and its translation to AHK is still a mystery to me.

Post Reply

Return to “Ask for Help (v1)”