Thomymaster

need help on writing VBA code in AutoIT

4 posts in this topic

Hi There

I need to write a UDF which fills a bookmark in a word file with a value and then recreates the bookmark (because it gets deleted after the filling).

The original VBA code found on the internet is:

Set rngAdresse = ActiveDocument.Bookmarks!TM_Test

rngAdresse.Text = "Test value"

ActiveDocument.Bookmarks.Add "TM_Test", rngAdresse

but i don't know how to write this in AutoIT. Here is what i have so far:

Func _WordLib_BookmarkFill(ByRef $oWordDoc,$sBookmarkName,$sBookmarkText)
    _Log("_WordLib_BookmarkExist():" & @ScriptLineNumber,"1",$LOG_ERROR)
    Local $vRangeBookmark=$oWordDoc.Bookmarks & "!" & $sBookmarkName

    _Log("_WordLib_BookmarkExist():" & @ScriptLineNumber,"2",$LOG_ERROR)
    $vRangeBookmark.Text=$sBookmarkText

    _Log("_WordLib_BookmarkExist():" & @ScriptLineNumber,"3",$LOG_ERROR)
    $oWordDoc.Bookmarks.Add('"' & $sBookmarkName & '"',$vRangeBookmark)

    _Log("_WordLib_BookmarkExist():" & @ScriptLineNumber,"4",$LOG_ERROR)
    Return 1
EndFunc

Once running, the text is not inserted and i get the following in my log (a function named errfunc() is used to capture the COM errors)

[03/02/2015 15:56:59] [ VM-TOMMY ] errfunc():1642: a COM error has occured in line 53, number is: 80020006 Description is: Unbekannter Name.
[03/02/2015 15:57:00] [ VM-TOMMY ] _WordLib_BookmarkExist():73: 1
[03/02/2015 15:57:00] [ VM-TOMMY ] _WordLib_BookmarkExist():75: 2
[03/02/2015 15:57:00] [ VM-TOMMY ] errfunc():1642: a COM error has occured in line 76, number is: 80020006 Description is: Unbekannter Name.
[03/02/2015 15:57:00] [ VM-TOMMY ] _WordLib_BookmarkExist():77: 3
[03/02/2015 15:57:00] [ VM-TOMMY ] errfunc():1642: a COM error has occured in line 78, number is: 80020009 Description is: Ausnahmefehler aufgetreten.
[03/02/2015 15:57:00] [ VM-TOMMY ] _WordLib_BookmarkExist():79: 4

Can somebody help me on how to format that AutoIT code properly? :(

Cheers

Thomy

Share this post


Link to post
Share on other sites



In VBA "!" stands for the default property of an object. Seems to be the Item property for the Bookmarks object.

So it should be:

Func _WordLib_BookmarkFill(ByRef $oWordDoc,$sBookmarkName,$sBookmarkText)
     _Log("_WordLib_BookmarkExist():" & @ScriptLineNumber,"1",$LOG_ERROR)
     Local $vRangeBookmark=$oWordDoc.Bookmarks.Item($sBookmarkName)
     _Log("_WordLib_BookmarkExist():" & @ScriptLineNumber,"2",$LOG_ERROR)
     $vRangeBookmark.Text=$sBookmarkText
     _Log("_WordLib_BookmarkExist():" & @ScriptLineNumber,"3",$LOG_ERROR)
     $oWordDoc.Bookmarks.Add('"' & $sBookmarkName & '"',$vRangeBookmark)
     _Log("_WordLib_BookmarkExist():" & @ScriptLineNumber,"4",$LOG_ERROR)
     Return 1 
EndFunc

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

OK, thanks a lot. That helped :)

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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