Monitoring For New Files

Sleven

New Member
Joined
Dec 23, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Dim MyExcel
Set MyExcel = GetObject("C:\ExcelFiles\Useful\MonitorDirectory.xls")
 
''
strComputer = "."
'// Note 4 forward slashes!
strDirToMonitor = "C:\\\\A_BOOK"
'// Monitor Above every 10 secs...
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) & "'")
 
 
Do While True
    Set objEventObject = colMonitoredEvents.NextEvent()
 
    Select Case objEventObject.Path_.Class
        Case "__InstanceCreationEvent"
            MsgBox "A new file was just created: " & _
                objEventObject.TargetInstance.PartComponent
            End With
            Exit Do
        Case "__InstanceDeletionEvent"
            MsgBox "A file was just deleted: " & _
                objEventObject.TargetInstance.PartComponent
            Exit Do
        Case "__InstanceModificationEvent"
            MsgBox "A file was just modified: " & _
                objEventObject.TargetInstance.PartComponent
            Exit Do
    End Select
Loop

Hello,
I currently use a variation of the above code to monitor a folder for a new file being created in it.
I was wondering it if was possibly (and if so how) to add a 2nd location to search.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Bump.
To be clear I want to monitor a 2nd location as well for any new files being created in it.
 
Upvote 0
Have you tried adding an OR clause to the SELECT query with the 2nd folder path (strDir2ToMonitor)? Something like this:

VBA Code:
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) & "'" _
             & " OR " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=" & Chr(34) & strDir2ToMonitor & Chr(34) & "')")

Another way is to have two queries, one for each folder and specify the timeout argument (in milliseconds) on both query's NextEvent calls. It should be something like this:

VBA Code:
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) & "'")
 
Set colMonitoredEvents2 = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN " & strTime & " WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=" & Chr(34) & strDir2ToMonitor & Chr(34) & "'")
 
Do While True

        Set objEventObject = Nothing
        On Error Resume Next
        Set objEventObject = colMonitoredEvents.NextEvent(100)
        If objEventObject Is Nothing Then Set objEventObject = colMonitoredEvents2.NextEvent(100)
        On Error GoTo 0
        
        If Not objEventObject Is Nothing Then
 
        Select Case objEventObject.Path_.Class
        Case "__InstanceCreationEvent"
            MsgBox "A new file was just created: " & _
                objEventObject.TargetInstance.PartComponent
            End With
            Exit Do
        Case "__InstanceDeletionEvent"
            MsgBox "A file was just deleted: " & _
                objEventObject.TargetInstance.PartComponent
            Exit Do
        Case "__InstanceModificationEvent"
            MsgBox "A file was just modified: " & _
                objEventObject.TargetInstance.PartComponent
            Exit Do
        End Select

        End If
Loop
 
Upvote 0
Hmmm so my 2nd location is a Folder in Outlook.
The issue is when I run the monitoring macro then Outlook doesn't update it's folders.

There should be a way to make a sound alert on new email... but I haven't figured out how to get that to work on a secondary folder.
 
Upvote 0
To be clear I run the VBA macro is run from Outlook.
By secondary folder I mean a Secondary Mailbox
 
Upvote 0
To be clear I run the VBA macro is run from Outlook.
By secondary folder I mean a Secondary Mailbox

Your OP didn't say the 2nd location is an Outlook folder, otherwise I would'nt have posted that code, which is for a Windows folder and the files in it. Outlook uses a single .pst file, in its data directory, which I assume changes whenever an email is sent, received or deleted.

There should be a way to make a sound alert on new email... but I haven't figured out how to get that to work on a secondary folder.
You could use Outlook's NewMail event for that:

 
Upvote 0
The inbox folders won't update with new mail while the macro is running...
That's unacceptable so changed to looking for an alternative way within Outlook (non VBA) to make a sound on new mail, then figure something different out for the windows folder.

I wasn't worried about pointing VBA code to an Outlook folder, I can do that, but needed help on how to implement a 2nd location to monitor.
I had to use your 2nd example - the 'OR' clause did not work out.
 
Upvote 0
Your OP didn't say the 2nd location is an Outlook folder, otherwise I would'nt have posted that code, which is for a Windows folder and the files in it. Outlook uses a single .pst file, in its data directory, which I assume changes whenever an email is sent, received or deleted.


You could use Outlook's NewMail event for that:


Okay I'm going to have to run the macro from Excel as I found no way to refresh the inboxes (receive new emails) while the macro is busy monitoring the inbox for new emails.

So how would I modify this code to point to an Outlook Inbox? ( I don't fully grasp/ know how to work with this chunk of code)

VBA Code:
Set colMonitoredEvents2 = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN " & strTime & " WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=" & Chr(34) & objFolder & Chr(34) & "'")
 
Upvote 0
as I found no way to refresh the inboxes (receive new emails) while the macro is busy monitoring the inbox for new emails.

You could call Outlook's NameSpace.SendAndReceive method:


So how would I modify this code to point to an Outlook Inbox?
As I said, that code is monitoring files in a Windows folder, and the only file that Outlook uses is a specific .pst file, in a specific Data File location. This .pst file contains all Outlook folders. As I said previously, you'd use Outlook's NewMail event to monitor the Inbox.
 
Upvote 0
Ya I couldn't get the SendAndReceive to work. It would bring up the dialog box, but would only sit in que until the macro found a new file and ended the macro monitoring the folder.
 
Upvote 0

Forum statistics

Threads
1,223,262
Messages
6,171,080
Members
452,377
Latest member
bradfordsam

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