Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 438925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:43:34+00:00 2026-05-12T20:43:34+00:00

Is there a way to watch a file in VBA (which is essentially VB6),

  • 0

Is there a way to watch a file in VBA (which is essentially VB6), so that I know when the file has been modified? — similar to this only I don’t want to know when a file is unused, just when its modified.

The answers I’ve found have recommended using “FileSystemWatcher” and the Win32 API “FindFirstChangeNotification”. I can’t figure out how to use these though, any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:43:34+00:00Added an answer on May 12, 2026 at 8:43 pm

    Okay, I put together a solution that is able to detect file system changes, in VBA (VB6).

    Public objWMIService, colMonitoredEvents, objEventObject
    
    'call this every 1 second to check for changes'
    Sub WatchCheck()
    On Error GoTo timeout
        If objWMIService Is Nothing Then InitWatch 'one time init'
        Do While True
            Set objEventObject = colMonitoredEvents.NextEvent(1) 
             '1 msec timeout if no events'
            MsgBox "got event"
    
            Select Case objEventObject.Path_.Class
                Case "__InstanceCreationEvent"
                    MsgBox "A new file was just created: " & _
                        objEventObject.TargetInstance.PartComponent
                Case "__InstanceDeletionEvent"
                    MsgBox "A file was just deleted: " & _
                        objEventObject.TargetInstance.PartComponent
                Case "__InstanceModificationEvent"
                    MsgBox "A file was just modified: " & _
                        objEventObject.TargetInstance.PartComponent
            End Select
        Loop
    Exit Sub
    timeout:
        If Trim(Err.Source) = "SWbemEventSource" And Trim(Err.Description) = "Timed out" Then
            MsgBox "no events in the last 1 sec"
        Else
            MsgBox "ERROR watching"
        End If
    End Sub
    

    Copy and paste this sub near the above, it is called automatically if needed to initialize the global vars.

    Sub InitWatch()
    On Error GoTo initerr
        Dim watchSecs As Integer, watchPath As String
        watchSecs = 1 'look so many secs behind'
        watchPath = "c:\\\\scripts" 'look for changes in this dir'
    
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
            ("SELECT * FROM __InstanceOperationEvent WITHIN " & watchSecs & " WHERE " _
                & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
                    & "TargetInstance.GroupComponent= " _
                        & "'Win32_Directory.Name=""c:\\\\scripts""'")
    
        MsgBox "init done"
    Exit Sub
    initerr:
        MsgBox "ERROR during init - " & Err.Source & " -- " & Err.Description
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there's a way to watch all RoutedEvents that are raised
Is there way that I can read the file from remote server using fopen
Is there a way that I can script the initialization file for GDB so
Is there a way to watch an object graph for changes on any object,
Is there a way to watch the changes of the retain count of an
Is there way to set @include mixin(); to variable? I tried this @mixin bg-gradient($fallback,
Is there a simple way to detect when a file for writing is closed?
Is there a way using Rake to watch depedencies for changes and execute tasks
Is there a better way to check if a file is not locked then
Is there a way to create a simple guard watch? I want to run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.