在没有轮询的情况下接收VBA中文件创建的通知

时间:2013-08-14 21:02:48

标签: vba ms-access

我正在编写一个与ScanSnap扫描仪集成的程序。 ScanSnap扫描仪不支持TWAIN。扫描文档后,它会自动保存为PDF格式。

我想监视保存文件的目录,并在文件出现时执行某些操作(并完成写入)。一种简单的方法是使用MS Access表单Timer事件,并在一小段时间内检查现有文件。

通过Windows Messaging,FileSystemObject或支持回调的某些Windows API函数是否有更好的替代方案?

4 个答案:

答案 0 :(得分:1)

Excel中没有任何内容。

您可以创建另一个监视文件系统的应用程序,并执行Excel宏,根据需要打开工作簿,根据需要打开Excel。

答案 1 :(得分:1)

答案 2 :(得分:1)

@Steve有效地回答了我问的问题。我应该问的是如何监视与MS Access UI线程分开的线程中的文件系统更改。对此问题的简单回答是does not support multi-threading中的 VBA Office applications

有多种解决方法通常涉及调用外部COM库或integrating with an external application。我认为这些都没有吸引力,而是决定使用FileSystemWatcher类在VB.Net中实现该解决方案。

答案 3 :(得分:0)

不确定这是否真的解决了您的问题,但这是一种使用Excel VBA的方法,帮助我监视特定文件夹中的特定文件并执行某些操作(此处:将文件复制到另一个文件夹中)如果文件被修改并保存(即文件的时间戳更改时):

Option Explicit

Const SourcePath = "C:\YourFolder\"
Const TargetPath = "C:\YourFolder\YourFolder_Changes\"
Const TargetFile = "YourFileName"

Private m_blnLooping As Boolean

Private Sub CommandButton1_Click()

Dim FSO As Scripting.FileSystemObject
Dim n, msg, dt, inttext As String
Dim file, files As Object
Dim d1, d2 As Date
Dim cnt As Integer
Dim wsshell

Application.ScreenUpdating = False
On Error Resume Next

Set FSO = CreateObject("Scripting.FileSystemObject")
Set files = FSO.GetFolder(SourcePath).files
Set wsshell = CreateObject("WScript.Shell")

msg = "FileWatcher started. Monitoring of " & TargetFile & " in progress."
cnt = 0

'Initialize: Loop through Folder content and get file date
For Each file In files
    n = file.name
    'Get Initial SaveDate of Target File
    If n = TargetFile Then
        d1 = file.DateLastModified
    End If
Next file

m_blnLooping = True

inttext = wsshell.popup(msg, 2, "FileWatcher Ready", vbInformation)
'Message Box should close after 2 seconds automatically

Shell "C:\WINDOWS\explorer.exe """ & TargetPath & "", vbNormalFocus
'Open Windows Explorer and display Target Directory to see changes

Do While m_blnLooping
    For Each file In files
        n = file.name
        If n = TargetFile Then
            d2 = file.DateLastModified
            If d2 > d1 Then
                dt = Format(CStr(Now), "yyyy-mm-dd_hh-mm-ss")
                'FSO.CopyFile (SourcePath & TargetFile), (TargetPath & Left(TargetFile, Len(TargetFile) - 4) & "_" & dt & ".txt"), True  'Option with file name extension
                FSO.CopyFile (SourcePath & TargetFile), (TargetPath & TargetFile & "_" & dt), True                                      'Option without file name extension
                cnt = cnt + 1
                d1 = d2
            End If
        End If
    Next file
    'Application.Wait (Now() + CDate("00:00:02")) 'wait 2 seconds, then loop again
DoEvents
Loop

msg = "File " & TargetFile & " has been updated " & cnt & " times."
inttext = wsshell.popup(msg, 2, "FileWatcher Closed", vbInformation)
'Message Box should close after 2 seconds automatically

Application.ScreenUpdating = True

End Sub

Private Sub CommandButton2_Click()

m_blnLooping = False

End Sub

通过CommandButton(" START")激活该过程,并循环显示特定文件夹(继续观察文件),直到按下另一个CommandButton(" STOP")。但是,您可能需要调整代码来监视文件创建而不是文件更改(file.DateCreated而不是file.DateLastModified)。本准则旨在为您提供可能解决您的问题的提示。