garbb

Application events won't fire for Word 2010 after clicking in document

13 posts in this topic

I am trying to use the microsoft word application.DocumentBeforeSave event in Office 2010 in autoit and I have found that for some strange reason, it will not trigger at all if I have clicked inside the text part of the open word document. Here is my test code (or course you will have to have microsoft word installed for this to work):

$oWord = ObjCreate("Word.Application")
$document = $oWord.Documents.Add
$oWord.Visible = 1

$oEvt2 = ObjEvent($oWord,'appevent_')

while 1
sleep(100)
WEnd

Func appevent_DocumentBeforeSave($foo)
ConsoleWrite('app_DocumentBeforeSave'&@CRLF)
EndFunc
When I run this it opens up a new word document. If I Just click save in word, the event happens and "app_DocumentBeforeSave" appears in the console. If I click in the body of the word document and then click on save nothing happens. This makes no sense to me. Is this just some wierd bug?

I have tried using the autoit com error handler with the code from the help file:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

Func _ErrFunc($oError)
; Do anything here.
ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
"err.windescription:" & @TAB & $oError.windescription & @CRLF & _
"err.description is: " & @TAB & $oError.description & @CRLF & _
"err.source is: " & @TAB & $oError.source & @CRLF & _
"err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
"err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
"err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
"err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
"err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc ;==>_ErrFunc
and haven't seen any errors in the console so I don't think there is some COM error in the background.

Also, completely unrelated: how do you preserve the tabs/indenting when pasting autoit code from scite into this forum, it seems like no matter how I tried to get it in here It would strip out the tabs and then when I tried to type them back pressing tab will just switch focus to another part of the webpage instead of adding a tab. Weirdly, when I clicked on the autoit code button and pasted my code into the box it appeared to preserve the indenting but then when I clicked OK and the code went into my post the indenting disappeared...

Share this post


Link to post
Share on other sites



So after some more testing I have found that if I use the "$oEvt = ObjEvent($oWord,'appevent_')" line after I click in the document then it will work (the application event will trigger). However as soon as I click in the document again then it will fail to work after that... So somehow clicking in the document is undoing/unbinding the object event to my function in autoit. Very strange....

Is there some way I can check to see if it is still working in a loop and then re-run the ObjEvent() function again?

Share this post


Link to post
Share on other sites

I tried the following script and it works just fine. Save and SaveAs trigger the event.

Do you use the 32 or 64 bit version of Office?

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

$oWord = ObjCreate("Word.Application")
$document = $oWord.Documents.Add
$oWord.Visible = 1

$oEvt2 = ObjEvent($oWord, 'appevent_')

While 1
    Sleep(100)
WEnd

Func appevent_DocumentBeforeSave($foo)
    ConsoleWrite('app_DocumentBeforeSave' & @CRLF)
EndFunc   ;==>appevent_DocumentBeforeSave

Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
            "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            "err.description is: " & @TAB & $oError.description & @CRLF & _
            "err.source is: " & @TAB & $oError.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

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

I am using the 32 bit version of Office 2010. I tried this on another computer also running Office 2010 32bit and got the same results.

I copied your code exactly and tried it and I have the same results as my original post. Did you try clicking in the main body of the document after is appears and before clicking save?

Try this:

run the script and the blank word document will open.

Click file>save and see the DocumentBeforeSave event fire and the text appear in the console.

Click on the blinking cursor in the body of the document (or optionally type some text)

Click file>save and see whether or not the event fires and the text appears in the console and please tell me your result.

For me it does not work after I click in the document.

Also, could you tell me how you got your code into your post? Did you just click on the autoit code button and paste it in from Scite? When I do that for some reason all the tabs disappear...

Share this post


Link to post
Share on other sites

Looks like you have to access one of the passed parameters. If I run the following code the function is triggered twice.

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

$oWord = ObjCreate("Word.Application")
$oDoc = $oWord.Documents.Add
$oWord.Visible = 1

$oEvt2 = ObjEvent($oWord, 'appevent_')

While 1
    Sleep(100)
WEnd

Func AppEvent_DocumentBeforeSave($oDoc, $bSaveAsUI, $bCancel)
    ConsoleWrite('app_DocumentBeforeSave ' & $bSaveAsUI & " " & $bCancel & @CRLF)
EndFunc   ;==>AppEvent_DocumentBeforeSave

Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
            "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            "err.description is: " & @TAB & $oError.description & @CRLF & _
            "err.source is: " & @TAB & $oError.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

I have the same problem with the editor.

Click on the code button and then click ok. Insert the code in the editor between

and

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

Sorry but I copied your code exactly again and I still get the same results: the event doesn't trigger for me after I click inside the body of the document. I assume that when you said "the function is triggered twice" you meant that it triggered before and after you clicked? Is this correct?

Could you tell me what version of Microsoft word you are using (and whether it is 32bit or 64bit)?

Share this post


Link to post
Share on other sites

I'm running the 32 bit version of Office 2010.

I run the following script:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

$oWord = ObjCreate("Word.Application")
$oDoc = $oWord.Documents.Add
$oWord.Visible = 1

$oEvt2 = ObjEvent($oWord, 'appevent_')

While 1
    Sleep(100)
WEnd

Func AppEvent_DocumentBeforeSave($oDoc, $bSaveAsUI, $bCancel)
    ConsoleWrite('app_DocumentBeforeSave ' & $bSaveAsUI & " " & $bCancel & @CRLF)
EndFunc   ;==>AppEvent_DocumentBeforeSave

Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
            "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            "err.description is: " & @TAB & $oError.description & @CRLF & _
            "err.source is: " & @TAB & $oError.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

I run it from SciTE using AutoIt 3.3.8.1 by pressing F5.

Word opens and displays an empty document

I select file -> save and save the document to C:temp. The SciTE console shows "app_DocumentBeforeSave True"

Then i click in the body of the document and add a few characters

I select file -> save. The SciTE console shows "app_DocumentBeforeSave False"


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, first thank you for the very detailed explanation.

Unfortunately when I use that code and follow those exact steps, the second trigger does not happen and the "app_DocumentBeforeSave False" does not appear. I have tried this on another computer as well with the same results. I have no idea why it doesn't work. Maybe some bug caused by the installation of some microsoft office patch?

Since you have been so helpful would you mind doing me one last thing and telling me the version of Word you see if you go to File>Help and look on the right side under 'About Microsoft Word'? Mine says '14.0.6123.5001'

Share this post


Link to post
Share on other sites

Mine says "14.0.6123.5001 (32-Bit)".

I'm running a german system.


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 so now as a test I tried installing office 2010 and autoit onto a virtual machine and it STILL doesn't work....(no trigger after clicking in the document)...

If anyone else reads this thread could you please try the code and instructions from post and see if the second save triggers? I really don't understand what the hell is going on...

Share this post


Link to post
Share on other sites

Did anyone find the issue? I'm also having the same problem.

Share this post


Link to post
Share on other sites

speculation... is there a Word/Office user defined versioning or auto-save option on/off by default?


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

I've seen other problems discussed on this forum which have been caused by badly written Add-Ons. Could you please disable all Add-Ons and try again?


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