VB Script is close to working

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
The windows script host seems to think this was OK because instead of a bunch of error messages, I got a " begin monitoring for folder changes" message. However, nothing happens when I make a change to the folder (ie paste in a file). I have an extremely simple macro that I am trying to test fire when I paste a file into "C:\Receive." Any suggestions?
PS I left in the Select case with a single case because I cannot seem to make an If statement work in this.

Code:
Dim xlApp 
Set xlApp = CreateObject("Excel.Application")
strComputer = "."
strDirToMonitor = "C:\\\\Receive"
strTime = "10"
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
Select Case  objLatestEvent.Path_.Class 
Case" _instanceCreationEvent "
 With xlApp
 .Workbooks.Activate ("C:\GetTest")
 .Application.Run "GetTest.xls!Button2_Click"
 .Application.Quit
 End With
End Select
 Loop
 Set objWMIService = nothing
 Set colMonitoredEvents = nothing
 Set objLatestEvent = nothing
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I finally got it to fire a message box with the following code.
The next challange...for tomorrow... will be to get it to run a sub.
Thanks for the help!!
Code:
strComputer = "."
strDirToMonitor = "C:\\\\Receive"
strTime = "10"
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 "There!"
End If
 Loop
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
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