ActiveX event handlers in an HTA using JScript

前端 未结 2 1526
忘了有多久
忘了有多久 2020-12-06 15:48

In C# I can write event handlers as follows:

var wdApp = new Microsoft.Office.Interop.Word.Application();
wdApp.DocumentBeforeSave += (Document doc, ref bool          


        
2条回答
  •  不知归路
    2020-12-06 16:09

    I could not make the object::event syntax work. So, I delegated the event handling to an auxiliary script, which follows.

    caller.js file:

    var WordApp = WScript.CreateObject("Word.Application", "wd_");
    WordApp.Visible = true;
    
    WScript.Sleep(20000);
    
    function wd_Quit() { //'object_event' works with js inside wscript.exe
        WScript.Echo("word quit");
    }
    

    Then, call this script from within a HTA file

    main.hta file:

    
    
    
    

    Both files must be on the same folder. Run main.hta and the code in caller.js will be called, without polluting the global namespace of the HTA.

    This example is for testing purposes. There is a 20 seconds timeout for the script. After this time, it will quit and events won't be processed.

    If it works, tell me so we can create a better way of communicating from the HTA (mshta.exe) to the JS (wscript.exe). There is a chance of using "GetObject" from the HTA to control the word application (I did it once, but with VBScript), while the events are processed by the JS file.

提交回复
热议问题