VBScript中的COM事件处理程序

3
我想要捕获NewCivicAddressReport事件,这意味着我需要实现事件处理程序。有人能解释一下为什么嵌入在html页面中的VBScript代码可以工作,但是VBS文件不行吗?这里是处理NewCivicAddressReport事件的html页面,在CivicFactory_NewCivicAddressReport()函数中可以实现。我认为这是因为事件处理程序命名规则的原因,如果我错了请指正。
    <!-- Civic address Location report factory object -->
    <object id="CivicFactory" 
        classid="clsid:2A11F42C-3E81-4ad4-9CBE-45579D89671A"
        type="application/x-oleobject">
    </object>                 

    <script language="vbscript">

    Function CivicFactory_NewCivicAddressReport(report)
        MsgBox "New civic address report!"
    End Function

    Sub OnLoadPage()
        CivicFactory.ListenForReports(1000)
    End Sub

    Sub DisplayStatus(status)
        MsgBox "status displayed"
    End Sub

    </script>

以下是不起作用的VBS文件 - 事件处理程序函数似乎从未被调用。
Dim CivicFactory
Set CivicFactory = WScript.CreateObject("LocationDisp.CivicAddressReportFactory")

Function CivicFactory_NewCivicAddressReport(report)
    MsgBox "Location changed!"
    keepSleeping=false
End Function

CivicFactory.ListenForReports(1000)

dim keepSleeping
keepSleeping=true
while keepSleeping
    WScript.Sleep 200
wend

顺便问一下,有人能告诉我创建对象的两种方式:和WScript.CreateObject()的区别吗?
提前感谢!
1个回答

4

WScript.CreateObject的第二个参数是事件处理函数中使用的前缀。为了使其正常工作,请将CreateObject调用更改如下。

Set CivicFactory = _
    WScript.CreateObject("LocationDisp.CivicAddressReportFactory", _
        "CivicFactory_")

WScript.CreateObject和CreateObject的区别在于WScript.CreateObject支持事件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接