Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

events - How do I know when a file has been modified in a VBA Macro?

Is there a way to watch a file in VBA (which is essentially VB6), so that I know when the file has been modified? -- similar to this only I don't want to know when a file is unused, just when its modified.

The answers I've found have recommended using "FileSystemWatcher" and the Win32 API "FindFirstChangeNotification". I can't figure out how to use these though, any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay, I put together a solution that is able to detect file system changes, in VBA (VB6).

Public objWMIService, colMonitoredEvents, objEventObject

'call this every 1 second to check for changes'
Sub WatchCheck()
On Error GoTo timeout
    If objWMIService Is Nothing Then InitWatch 'one time init'
    Do While True
        Set objEventObject = colMonitoredEvents.NextEvent(1) 
         '1 msec timeout if no events'
        MsgBox "got event"

        Select Case objEventObject.Path_.Class
            Case "__InstanceCreationEvent"
                MsgBox "A new file was just created: " & _
                    objEventObject.TargetInstance.PartComponent
            Case "__InstanceDeletionEvent"
                MsgBox "A file was just deleted: " & _
                    objEventObject.TargetInstance.PartComponent
            Case "__InstanceModificationEvent"
                MsgBox "A file was just modified: " & _
                    objEventObject.TargetInstance.PartComponent
        End Select
    Loop
Exit Sub
timeout:
    If Trim(Err.Source) = "SWbemEventSource" And Trim(Err.Description) = "Timed out" Then
        MsgBox "no events in the last 1 sec"
    Else
        MsgBox "ERROR watching"
    End If
End Sub

Copy and paste this sub near the above, it is called automatically if needed to initialize the global vars.

Sub InitWatch()
On Error GoTo initerr
    Dim watchSecs As Integer, watchPath As String
    watchSecs = 1 'look so many secs behind'
    watchPath = "c:\\scripts" 'look for changes in this dir'

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & strComputer & "
ootcimv2")
    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
        ("SELECT * FROM __InstanceOperationEvent WITHIN " & watchSecs & " WHERE " _
            & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
                & "TargetInstance.GroupComponent= " _
                    & "'Win32_Directory.Name=""c:\\scripts""'")

    MsgBox "init done"
Exit Sub
initerr:
    MsgBox "ERROR during init - " & Err.Source & " -- " & Err.Description
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...