vbScript runs wild

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
The following vbScript works fine at monitoring a folder for new file additions; and I need it to do so continuously, which it does. The problem is that it loops through the If statement more than once and creates all kinds of problems by trying to run macros more than once. Even in the example below, I get 2 message boxes, no matter how long I ask it to sleep. Does anyone have a suggestion on how to make it loop through once only for each folder change event? Thank you.
Code:
Dim xlApp
Set xlApp = CreateObject("Excel.Application")
strComputer = "."
strDirToMonitor = "C:\\\\srvr\\\\ftp\\\\rie\\\\incoming"
strTime = "2"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN " & strTime & " WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=" & Chr(34) & strDirToMonitor & Chr(34) & "'")
 Wscript.Echo vbCrlf & Now & vbTab & _
 "Begin Monitoring for a Folder Change Event..." & vbCrlf

Do 
  Set objLatestEvent = colMonitoredEvents.NextEvent
   If objLatestEvent.Path_.Class = "__InstanceCreationEvent"  Then
    MsgBox "Hello"
        wscript.Sleep 10000
     End If
 Loop
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I found another solution, which I think is the right way to go. First, I need the do loop because I want to monitor the folder continuously and this seems to be the only way to do so. To prevent serial detection of the same file, what I finally figured out is that the first order of business is to get the new file out of the monitored folder. DevGuru is the best website I have found for vbScript and I found this there.
First create a File System Object
Code:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Then, in my do loop
Code:
fso.MoveFile "C:\monitored folder\*.xls", "C:\destination folder\"
The first time I tried this I got one message box only and I have since gone on to have it successfully move files and run macros on all of the new files coming in...this is tricky to! Thanks for the suggestions...it is apprecitated!
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top