The follwing vbscript now works fine except that it will not delete the file as instructed on the second to last line. Would appreciate any suggestions. (By the way the wscript.sleep line seems to be the only way to get it not to repeat the If-then part several times over...this seems to be more of a problem when monitoring a subfolder than a folder on the c drive) I would appreciate any suggestions on that as well. Many thanks.
Code:
Dim xlApp
Dim bk1
Dim bk2
Dim sh1
Dim sh2
Set xlApp = CreateObject("Excel.Application")
strComputer = "."
strDirToMonitor = "C:\\\\Gather\\\\Receive"
strTime = "5"
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 While True
Set objLatestEvent = colMonitoredEvents.NextEvent
If objLatestEvent.Path_.Class = "__InstanceCreationEvent" Then
xlApp.visible = true
Set bk1 = xlApp.Workbooks.Open("c:\Gather\Receive\Transfile2.xls")
Set bk2 = xlApp.Workbooks.Open("C:\Test.xls")
Set sh1 = bk1.Worksheets("Sheet1")
Set sh2 = bk2.Worksheets("Sheet1")
wscript.Sleep 1200
sh1.Activate
sh1.Range("A1:N3").Copy
sh2.Activate
sh2.Range("A6").Select
sh2.Paste
bk1.Close False
bk1.Delete
End If
Loop