PC Review


Reply
Thread Tools Rate Thread

Automation Server Events Usage in VBA

 
 
urkec
Guest
Posts: n/a
 
      19th Apr 2008
I want to monitor file creation from an Excel workbook. For this I usually
use VBScript and WMI scripting library. The only VBA example I could find is
here:

http://msdn2.microsoft.com/en-us/library/aa390420(VS.85).aspx

The sample code works when I put it in a UserForm module, but I want to use
it from a Class module. I found some information here:

http://www.cpearson.com/excel/AppEvent.aspx

I created a Class module clsSink and set Instancing to PublicNotCreatable.
Based on the example, I put this code in clsSink:


Dim WithEvents sink As SWbemSink


Private Sub sink_OnObjectReady( _
ByVal objWbemObject As SWbemObject, _
ByVal objWbemAsyncContext As SWbemNamedValueSet)

On Error GoTo ErrorHandler

Sheets(1).Cells(1, 1) = _
objWbemObject.TargetInstance.Name
Sheets(1).Cells(2, 1) = _
objWbemObject.Path_.Class

Set objWbemObject = Nothing

Exit Sub ' Exit to avoid handler

ErrorHandler:

MsgBox "Error number: " _
& Str(Err.Number) & vbNewLine & _
"Description: " & Err.Description, _
vbCritical

End Sub

Private Sub Class_Initialize()

Dim services As SWbemServices
Dim strQuery As String
Dim cntxt As SWbemNamedValueSet

Set sink = New SWbemSink
Set services = GetObject("winmgmts:")

strQuery = _
"Select * From __InstanceOperationEvent " _
& "Within 3 " _
& "Where TargetInstance Isa 'Cim_DataFile' " _
& "And TargetInstance.Drive = 'C:' " _
& "And TargetInstance.Path = '\\scripts\\'"

services.ExecNotificationQueryAsync _
sink, strQuery, , , , cntxt

MsgBox "This Workbook will asynchronously " _
& "process Cim_DataFile events."

End Sub


In ThisWorkbook I put this code:


Public sink As clsSink

Private Sub Workbook_Open()
Set sink = New clsSink
End Sub


This code works, when a file is created, deleted, modified etc. in
c:\scripts, workbook receives those events.

This is the first time I use this in Excel VBA, so my questions are:

Is this the right way to handle events from external automation objects?
Should I add or modify any of this code to make it more reliable (because it
is possible that the workbook will be open for a long time)?
Are there any samples that show how to use COM Automation server (any
automation object) events in Excel VBA?

Thanks in advance.

--
urkec
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
object does not source automation events =?Utf-8?B?am9zaDc=?= Microsoft Excel Programming 3 19th Aug 2005 04:05 PM
VC Build Automation Object Usage Questions =?Utf-8?B?SmVua3Nh?= Microsoft VC .NET 0 1st Nov 2004 03:45 PM
Automation Events =?Utf-8?B?QUEyZTcyRQ==?= Microsoft Excel Programming 4 27th May 2004 03:33 PM
powerpoint automation and events Alex Krumm-Heller Microsoft Powerpoint 2 3rd Mar 2004 12:41 AM
powerpoint automation events Alex Krumm-Heller Microsoft Powerpoint 1 18th Feb 2004 06:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:46 PM.