2011-04-28 53 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支持事件。