如何在每次保存文本文件时运行VBA宏?

时间:2015-08-21 14:58:50

标签: vba excel-vba save excel

我试图找到一种方法来在每次保存某个文件夹中的文本文件,任何文本文件时运行宏。

有谁知道这样做的方法?

1 个答案:

答案 0 :(得分:1)

来自:http://blogs.technet.com/b/heyscriptingguy/archive/2005/04/04/how-can-i-monitor-for-different-types-of-events-with-just-one-script.aspx

Sub Monitor()

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
        ("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE " _
            & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
                & "TargetInstance.GroupComponent= " _
                    & "'Win32_Directory.Name=""c:\\\\_Stuff""'")

    Do While True

        Set objEventObject = colMonitoredEvents.NextEvent()

        Select Case objEventObject.Path_.Class
            Case "__InstanceCreationEvent"
                Debug.Print "A new file was just created: " & _
                    objEventObject.TargetInstance.PartComponent
            Case "__InstanceDeletionEvent"
                Debug.Print "A file was just deleted: " & _
                    objEventObject.TargetInstance.PartComponent
        End Select
    Loop

End Sub