我如何知道文件何时在 VBA 宏中被修改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1586169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I know when a file has been modified in a VBA Macro?
提问by TheFlash
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 thisonly I don't want to know when a file is unused, just when its modified.
有没有办法在 VBA(本质上是 VB6)中查看文件,以便我知道文件何时被修改?- 与此类似,只是我不想知道文件何时未使用,只是何时修改。
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?
我找到的答案建议使用“FileSystemWatcher”和 Win32 API“FindFirstChangeNotification”。我不知道如何使用这些,知道吗?
采纳答案by TheFlash
Okay, I put together a solution that is able to detect file system changes, in VBA (VB6).
好的,我在 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 & "\root\cimv2")
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
回答by stuartd
You should consider using a WMI temporary event consumer to watch the file, along the lines suggested herebut narrowing it down to a specific file instead of a folder
您应该考虑使用 WMI 临时事件使用者来观看文件,按照此处建议的行,但将其范围缩小到特定文件而不是文件夹
(This is assuming you can't just keep an eye on the file's Modified Date property..)
(这是假设您不能只关注文件的修改日期属性..)
回答by Wim Coenen
回答by hack0573
I take it into vb6,run,display:ERROR watching.
我把它带入 vb6,run,display:ERROR 看。