Ending `Do While` loop with user input for the colMonitoredevents based FOLDER monitoring

ExcelMind1434

New Member
Joined
Apr 18, 2014
Messages
2
I am using a script to monitor a folder for addition of files. The script runs fine if 3 files (meeting the conditions) are added to the monitored folder. It nicely extracts the data from these files and adds to the open excel file. But if the conditions are not me the script keeps going through the <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: pre-wrap; color: rgb(0, 0, 0); line-height: 17.804800033569336px;">Do While Loop</code> and I am unable to use any buttons on the excel (Was thinking of using another command button to exit the loop) to let the user break the loop. Please Help!! Any suggestions are appreciated! Thanks!


Code:
Public vItem As Variant
'vItem contains the folder path that the user selects.  
'Another function deals with this and only its values is passed to `CommandButton2 Click()` 


Private Sub CommandButton2_Click()
Dim i As Integer
    i = 0
Dim fcounter, pcounter, vcounter As Integer
    fcounter = 0
    pcounter = 0
    vcounter = 0






Set objShell = CreateObject("Wscript.Shell")


strComputer = "."


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")


Dim vItemstr As String
vItemstr = Replace(vItem, "\", "\\\\")
MsgBox vItemstr






Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=" & Chr(34) & vItemstr & Chr(34) & "'")






Do While True


    Set objLatestEvent = colMonitoredEvents.NextEvent
    StrNewfile = objLatestEvent.TargetInstance.PartComponent




    arrNewFile = Split(StrNewfile, "=")
    strFileName = arrNewFile(1)
    strFileName = Replace(strFileName, "\\", "\")
    strFileName = Replace(strFileName, Chr(34), "")
        Dim justfilename, namestr As String
            justfilename = Dir(strFileName)
            Do While True
                     novaval = InStr(1, justfilename, "SampleResults")


                             If novaval > 0 Then
                                     namestr = "f"
                                     Exit Do
                             End If
                     novaval = InStr(1, justfilename, "v")
                             If novaval > 0 Then
                                     namestr = "v"
                                     Exit Do
                             End If
                     novaval = InStr(1, justfilename, "p")
                             If novaval > 0 Then
                                     namestr = "p"
                                     Exit Do
                             End If
            Loop


        If namestr = "f" And fcounter = 0 Then
            i = i + 1


            Dim OpenFileName As String
            Dim wb As Workbook


                Set wb = Workbooks.Open(strFileName, UpdateLinks:=0)
                    ThisWorkbook.Sheets(1).Range("K18:P18").Value = wb.Sheets(1).Range("G1:L1").Value
                    ThisWorkbook.Sheets(1).Range("K19:P19").Value = wb.Sheets(1).Range("G5:L5").Value
                    ThisWorkbook.Sheets(1).Range("K20:P20").Value = wb.Sheets(1).Range("G4:L4").Value
                    ThisWorkbook.Sheets(1).Range("K21:P21").Value = wb.Sheets(1).Range("G3:L3").Value
                    ThisWorkbook.Sheets(1).Range("K22:P22").Value = wb.Sheets(1).Range("G2:L2").Value
                    ThisWorkbook.Save
                wb.Close
            fcounter = fcounter + 1
        ElseIf namestr = "v" And vcounter = 0 Then
                i = i + 1
                Set wb = Workbooks.Open(strFileName, UpdateLinks:=0)
                    ThisWorkbook.Sheets(1).Range("C18:E18").Value = wb.Sheets(1).Range("C1:E1").Value
                    ThisWorkbook.Sheets(1).Range("C19:E19").Value = wb.Sheets(1).Range("C5:E5").Value
                    ThisWorkbook.Sheets(1).Range("C20:E20").Value = wb.Sheets(1).Range("C4:E4").Value
                    ThisWorkbook.Save
                wb.Close
            vcounter = vcounter + 1
        ElseIf namestr = "p" And pcounter = 0 Then
                i = i + 1
                Set wb = Workbooks.Open(strFileName, UpdateLinks:=0)
                    ThisWorkbook.Sheets(1).Range("F18:H18").Value = wb.Sheets(1).Range("X1:Z1").Value
                    ThisWorkbook.Sheets(1).Range("F19:H19").Value = wb.Sheets(1).Range("X5:Z5").Value
                    ThisWorkbook.Sheets(1).Range("F20:H20").Value = wb.Sheets(1).Range("X4:Z4").Value
                    ThisWorkbook.Save
                wb.Close
            pcounter = pcounter + 1


        End If




        If i = 3 Then
            Exit Do
        End If




Loop


End Sub
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Anyone? Please..
Just want to know how to END the monitoring using WMIService.. is there a way to let the user stop the monitoring (like by clicking a button or something?)
Thanks!
 
Upvote 0
Have you tried placing a DoEvents statement efter the line : Do While True
 
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