在接收Outlook邮件时调用excel宏 [英] Call excel macro on receiving outlook mail
本文介绍了在接收Outlook邮件时调用excel宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我在Excel中创建了这个宏来检查Outlook收件箱并从来自某个用户的邮件中下载附件。现在,我在特定时间手动运行宏。但我希望在特定邮件进入邮箱时触发宏。我主要使用excel宏,并希望它尽可能地从excel端运行。此外,邮箱将是别人的,所以我不想在那里做很多。
我尝试过:
So I have made this macro in excel that checks outlook inbox and downloads attachments from mails coming from certain user. For now, I run the macro manually at a specific time. But I want the macro to get fired when that particular mail enters the mailbox. I mostly work with excel macros and want it to operate from excel side only as much as possible. Moreover, the mailbox will be someone else's so I do not want to do much there.
What I have tried:
Public Sub db_upload()
'download file from outlook
Dim ns As Outlook.Namespace
Dim inbox As Outlook.MAPIFolder
Dim mail As Object
Dim attachmnt As Outlook.Attachment
Dim fname As String
Dim docPath As String
Dim i As Integer
Dim wsh As Object
Dim fs As Object
cnt = 0
Set ns = GetNamespace("MAPI")
Set inbox = ns.GetDefaultFolder(olFolderInbox)
maildt = Format(Now, "dd-mm-yyyy")
For Each mail In inbox.Items
On Error Resume Next
namesender = mail.SenderName
mailsub = mail.Subject
maildate = Format(mail.ReceivedTime, "dd-mm-yyyy")
If InStr(mail.Subject, "something") > 0 And Format(mail.ReceivedTime, "dd-mm-yyyy") = maildt Then
For Each attachmnt In mail.Attachments
fname = attachmnt.Filename
attachmnt.SaveAsFile "path" & attachmnt.Filename
cnt = cnt + 1
Next attachmnt
End If
Next mail
Set inbox = Nothing
Set ns = Nothing
Set fs = Nothing
Set wsh = Nothing
End Sub
推荐答案
你不能只在Excel方面这样做。正如Richard MacCutchan在问题评论中提到的那样,当新电子邮件发布时,你必须抓住Outtlok的事件。
我建议读这个:如何处理Microsoft Outlook中的传入邮件 [ ^ ] - 方法3.您可以在这里找到:使用宏处理传入的电子邮件 [ ^ ]
详情请见:Outlook VBA参考 [ ^ ]
You can't do that from Excel side only. As Richard MacCutchan mentioned in the comment to the question, you have to catch Outtlok's event when new email incomming.
I'd suggest to read this: How to process incoming messages in Microsoft Outlook[^] - method 3. The same you'll find here: Processing Incoming E-mails with Macros[^]
For further details, please see: Outlook VBA reference[^]
这篇关于在接收Outlook邮件时调用excel宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文