【问题标题】:How do I get Outlook to periodically check a folder for files then email them out?如何让 Outlook 定期检查文件夹中的文件,然后通过电子邮件将它们发送出去?
【发布时间】:2017-06-20 09:22:46
【问题描述】:

这是我被要求完成工作的建议情况:

  • 监控电子邮件收件箱
  • 将收到一封带有附件“thing.foo”的电子邮件
  • 我们希望能够剥离附件并保存到网络上的文件夹中
  • 这将通过监控文件夹的系统自动处理
  • 然后我们希望能够提取一个输出文件并将其返回给 .foo 来自的原始电子邮件的发件人(假设这始终是相同的地址并且已修复)

好的,到最后一点我都很好:

我将在服务器上的 Outlook 实例中使用一个小的 VBA 脚本来提取 thing.foo 文件,为其指定一个唯一的文件名 (uniqueThing.foo),然后将其放入网络文件夹中。 该过程(与我无关)将运行并保存为“uniqueThing_processed.foo”之类的内容(可能将原始文件移动到存档文件夹)......我没问题。

现在,我需要让这个 Outlook 实例定期(比如每 5 分钟)检查“********_processed.foo”文件,并将其附加到通过电子邮件发送(然后可能将文件移动到存档并附加“_sent”)

【问题讨论】:

  • 使用计时器,没有内置计时器,但您可以调用 API:stackoverflow.com/questions/12257985/… 或者作为替代方案,您可以创建一个提醒并挂钩到其提醒事件。
  • 你不能在 Outlook VBA 中写下全部内容吗?编写一些代码来完成您想要的一切,确保代码在其参数中传递MailItem,您可以设置 Outlook 规则以在某些电子邮件到达时运行该脚本。 support.microsoft.com/en-gb/help/306108/…
  • 是的,事实上我做到了,但只针对传入的邮件。回传需要每分钟运行一次,而不仅仅是在电子邮件到达时运行。

标签: vba outlook


【解决方案1】:

正如 Alex K. 所说,使用计时器: 将以下内容添加到“ThisOutlookSession”

Private Sub Application_Quit()
If TimerID <> 0 Then Call EndTimer 'Turn off timer upon quitting **VERY IMPORTANT**
End Sub

Private Sub Application_Startup()
'MsgBox "Activating the Timer."
Call StartTimer 'Set timer to go off every 1 minute
End Sub

在模块中添加以下内容:

Public Declare Function SetTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Public Declare Function KillTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
Public TimerID As Long, TimerSeconds As Single, tim As Boolean
Dim Counter As Long
Sub LookForNew()
  Dim mess_body As String, StrFile As String, StrPath As String
  Dim appOutLook As Outlook.Application
  Dim MailOutLook As Outlook.MailItem

  Set appOutLook = CreateObject("Outlook.Application")
  Set MailOutLook = appOutLook.CreateItem(olMailItem)
  Dim n As String, msg As String, d As Date
  msg = ""
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set fils = fso.GetFolder("<<<Put your folder here>>>").Files
  For Each fil In fils
    n = fil.Name
    d = fil.DateCreated
    If d >= Date - 1 Then
      msg = msg & n & vbTab & d & vbCrLf
    End If
  Next fil
  If msg <> "" Then
    StrPath = "<<<Put your folder here>>>\" 'attention to the extra "\"
      With MailOutLook
       .BodyFormat = olFormatRichText
       .To = "<<<Put your Mail-Adress here>>>"
       .Subject = "Scan"
       .HTMLBody = msg
       StrFile = Dir(StrPath & "*.*") '~~> *.* for all files
       Do While Len(StrFile) > 0 'loop through all files in the Folder
       .Attachments.Add StrPath & StrFile
       StrFile = Dir
       Loop
       .DeleteAfterSubmit = True 'delete Mail from Send Items
       .Send
      End With
    Kill StrPath & "*.*" 'delete all files from Folder
  End If
  Set fso = Nothing
End Sub

Sub StartTimer()'~~> Start Timer
'~~ Set the timer for 60 second
TimerSeconds = 60
TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
End Sub

Sub EndTimer()'~~> End Timer
On Error Resume Next
KillTimer 0&, TimerID
End Sub

Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _
ByVal nIDEvent As Long, ByVal dwTimer As Long)
Call LookForNew ' call your existing or modified code here
End Sub

【讨论】:

  • 感谢您花时间提交 Dan - 碰巧我在发布此内容后的第二天写了一个工作脚本,与您发布的内容非常相似,但有一些调整确切的需求。
猜你喜欢
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
相关资源
最近更新 更多