0

I am trying to find a way to run a macro every time a text file, any text file, in a certain folder is saved.

Does anyone know of a way to do that?

Community
  • 1
  • 1
bpyne
  • 41
  • 1
  • 5
  • What text editor are you using. If it has macros you will need to look for a onSave event. If not, you would have to write an app that runs all the time and scans the files in the directory for a last saved date of within some time frame. – MatthewD Aug 21 '15 at 15:01
  • You could write a VBScript to either reproduce the functionality of the VBA script or e.g. launch the workbook that contains the script. The -- use Windows Task Scheduler to control the VBScript (e.g. run every 5 minutes). See this question: http://stackoverflow.com/questions/4249542/windows-scheduler-to-run-a-task-every-x-minutes – John Coleman Aug 21 '15 at 15:33

1 Answers1

1

From: 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
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Nice. I need to sit down and really learn VBScript one of these days (rather than just raiding it from VBA to get nifty things like dictionaries and regular expressions). – John Coleman Aug 21 '15 at 17:57