简体   繁体   English

Outlook-从带有.xls附件的电子邮件和特定发件人中保存文件,然后将电子邮件移至子文件夹

[英]Outlook - Save file from email with .xls attachment and from specific sender then move email to sub folder

I want to trigger a macro when a new email from a specific email address with an .xls attachment is received in an inbox. 我想在收件箱中收到来自具有.xls附件的特定电子邮件地址的新电子邮件时触发宏。 I have tried to set a rule in outlook but it doesn't filter on the sender nor if it has an attachment. 我试图在Outlook中设置一个规则,但它不会对发件人进行过滤,也不会具有附件。

What I would like to do is the following: 我想做的是以下几点:

  1. When a new email comes into the inbox check if it is from a certain email address ag:Myaddress.me.co.uk. 当新电子邮件进入收件箱时,检查它是否来自某个电子邮件地址ag:Myaddress.me.co.uk。 If the email is not from the correct address do nothing. 如果电子邮件不是来自正确的地址,则什么也不做。
  2. If the subject line has certain words eg: " Price Checks". 如果主题行中包含某些单词,例如:“ Price Checks”。 It the subject doesn't match do nothing. 如果主题不匹配,则不执行任何操作。
  3. If the email is from the correct address Check the new email has a .xls attachment. 如果电子邮件来自正确的地址,请检查新电子邮件是否带有.xls附件。 If it doesn't have the .xls attachment do nothing. 如果没有.xls附件,则不执行任何操作。
  4. Save the attachment in a folder eg:"C:\\MyFolder" 将附件保存在文件夹中,例如:“ C:\\ MyFolder”
  5. Mark the Email as Read and move to a sub folder eg: "PriceCheckFolder" 将电子邮件标记为已读,然后移到子文件夹,例如:“ PriceCheckFolder”

I have been using this code to check the inbox but it looks through all emails in the folder and I only want it to look at the first instance that fits the criteria. 我一直在使用此代码来检查收件箱,但它会查看文件夹中的所有电子邮件,而我只希望它查看符合条件的第一个实例。

Many Thanks Melinda 非常感谢梅琳达

‘in thisworkbook

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim SubFolder As MAPIFolder

  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub


Private Sub Items_ItemAdd(ByVal item As Object)

  On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem

  If TypeName(item) = "MailItem" Then
    Set Msg = item
    Call SaveAttachmentsToFolder
  End If

ProgramExit:
  Exit Sub

ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub


Sub SaveAttachmentsToFolder()

'Error handling
  On Error GoTo SaveAttachmentsToFolder_err


‘in module1

' Declare variables
  Dim ns As NameSpace
  Dim Inbox As MAPIFolder
  Dim SubFolder As MAPIFolder
  Dim item As Object
  Dim Atmt As Attachment
  Dim FileName As String
  Dim i As Integer
  Dim varResponse As VbMsgBoxResult
  Dim StringLength As Long
  Dim Filename1 As String
  Dim FilenameA As String
  Dim FilenameB As String

'Set the variable values to be used in the code
  Set ns = GetNamespace("MAPI")
  Set Inbox = ns.GetDefaultFolder(olFolderInbox)
  Set SubFolder = Inbox.Folders("Test")
  i = 0

' Check subfolder for messages and exit of none found
  If SubFolder.Items.Count = 0 Then
  ' "Nothing Found"
    Exit Sub
  End If

' Check each message for attachments
  For Each item In SubFolder.Items
    For Each Atmt In item.Attachments
      ' Check filename of each attachment and save if it has "xls" extension
      If Right(Atmt.FileName, 3) = "xls" Then
        StringLength = Len(Atmt.FileName)

        FileName = "\\feltfps0003\gengrpshare0011\Value Team\Melinda_BK\OutlookVBA\TestOutput\" & Left(Atmt.FileName, (StringLength - 13)) & Format(item.CreationTime, "ddmmmyyyy") & ".xls"
        Atmt.SaveAsFile FileName
        i = i + 1
      End If
    Next Atmt
  Next item

' Clear memory
SaveAttachmentsToFolder_exit:
  Set Atmt = Nothing
  Set item = Nothing
  Set ns = Nothing
  Exit Sub

' Handle Errors
SaveAttachmentsToFolder_err:
    MsgBox "An unexpected error has occurred." _
    & vbCrLf & "Please note and report the following information." _
    & vbCrLf & "Macro Name: GetAttachments" _
    & vbCrLf & "Error Number: " & Err.Number _
    & vbCrLf & "Error Description: " & Err.Description _
    , vbCritical, "Error!"sub
Resume SaveAttachmentsToFolder_exit
End Sub

I have tried to set a rule in outlook but it doesn't filter on the sender nor if it has an attachment. 我试图在Outlook中设置一个规则,但它不会对发件人进行过滤,也不会具有附件。

Create a rule calling the following script. 创建一个调用以下脚本的规则。

It will run on all incoming mail but only execute your code for whatever email address you look for 它将在所有传入的邮件上运行,但仅针对您要查找的任何电子邮件地址执行代码

Sub checkEmailSenderAndDoStuff(myItem As MailItem)

    'set this up as a script to run on all incoming mail
    Dim myTargetEmailAddress As String
    myTargetEmailAddress = "whatever@wherever.com"

    'this will check if the sender email is whatever sender
    'you want to check from
    If myItem.SenderEmailAddress = myTargetEmailAddress Then
        'do whatever you wanted to do with attachments, moving, etc
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Outlook 中通过文件保存电子邮件附件 - Save Email Attachment Over File From Outlook 从特定的Outlook文件夹下载电子邮件并保存 - Download Email from Specific Outlook Folder and Save 在 Outlook 中仅保存电子邮件中的附件 - Save only attachment from email in outlook VBA代码,用于将附件(excel文件)保存在另一封电子邮件中作为附件的Outlook电子邮件中 - VBA Code to save an attachment (excel file) from an Outlook email that was inside another email as an attachment 保存附件然后在 Outlook 2010 中移动电子邮件 - Save attachment then move email in outlook 2010 仅保存收件箱中由特定发件人在特定日期发送的那些 Outlook 电子邮件附件 - Save only those Outlook Email Attachments from Inbox, which are sent by specific sender on specific dates 我正在尝试在 Outlook 中编写 VBA 宏,它将电子邮件的附件保存到特定文件夹 - I'm trying to write VBA macro in Outlook that will save an email's attachment to a specific folder 如何自动保存来自特定发件人的附件? - How to automatically save attachment from specific sender? 将电子邮件中的附件保存到每月更改的文件夹中 - Save attachment from an email in to a folder that changes every month vba 从 Outlook 中的发件人下载文本文件附件并将邮件移动到子文件夹 - vba to download text file attachment from sender in Outlook and move message to subfolder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM