本文介绍了如何知道文件何时在VBA宏中修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有没有办法在VBA(本质上是VB6)中观看文件,以便我知道文件何时被修改? - 仅类似于此,我不想知道文件何时 我发现的答案建议使用FileSystemWatcher和Win32 APIFindFirstChangeNotification。我不知道如何使用这些,但是,任何想法?解决方案在VBA(VB6)中检测文件系统更改。 公共objWMIService,colMonitoredEvents,objEventObject '每1秒钟调用一次以检查变化' Sub WatchCheck() On Error GoTo timeout 如果objWMIService不是,那么InitWatch'一次init' Do While True 设置objEventObject = colMonitoredEvents.NextEvent(1)'1 msec超时,如果没有事件' MsgBox有事件 选择案例objEventObject.Path_.Class 案例__InstanceCreationEvent MsgBox刚创建一个新文件:& _ objEventObject.TargetInstance.PartComponent 案例__InstanceDeletionEvent MsgBox一个文件刚刚删除:& _ objEventObject.TargetInstance.PartComponent 案例__InstanceModificationEvent MsgBox刚刚修改了一个文件:& _ objEventObject.TargetInstance.PartComponent 结束选择循环退出子超时:如果修剪(Err.Source)=SWbemEventSource和修剪(Err.Description)=超时然后 MsgBox最近1秒没有事件 Else MsgBoxERROR看看如果结束Sub 将此子复制并粘贴到上面附近,如果需要初始化全局变量,它将自动调用 Sub InitWatch()错误GoTo initerr Dim watchSecs As Integer,watchPath As String watchSecs = 1'看起来这么多秒后面' watchPath =c:\\\\\scripts'寻找这个目录中的变化' strComputer = 设置objWMIService = GetObject(winmgmts:\\& strComputer&\root\cimv2)设置colMonitoredEvents = objWMIService.ExecNotificationQuery _ (SELECT * FROM __InstanceOperationEvent WITHIN& watchSecs&WHERE_ &Targetinstance ISA'CIM_DirectoryContainsFile'和_ &TargetInstance.GroupComponent =_ &'Win32_Directory .Name =c:\\\\\scripts') MsgBoxinit done Exit Sub initerr: MsgBoxinit期间的错误 - & Err.Source& - & Err.Description End Sub 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? 解决方案 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 LoopExit Subtimeout: 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 IfEnd SubCopy 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 Subiniterr: MsgBox "ERROR during init - " & Err.Source & " -- " & Err.DescriptionEnd Sub 这篇关于如何知道文件何时在VBA宏中修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 12:33