2011-04-28 56 views
3

我想捕获NewCivicAddressReport事件,这意味着我需要实现事件处理程序。任何人都可以解释为什么HTML页面中嵌入的VBScript代码工作,但VBS文件不能?VBScript中的COM事件处理程序

以下是可以在CivicFactory_NewCivicAddressReport()函数中处理NewCivicAddressReport事件的html页面。我想这是因为事件处理程序的命名约定。如我错了请纠正我。

<!-- 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()?

在此先感谢!

回答

3

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

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

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