VBA选择触发脚本的电子邮件

VBA选择触发脚本的电子邮件,vba,email,outlook,Vba,Email,Outlook,我正在使用Outlook脚本,该脚本将自动选择并从特定发件人下载附件。目前,报告是在数据库上生成的,并通过电子邮件发送到指定的地址-下一步是自动将这些报告下载到指定的文件夹。当前,如果电子邮件来自指定的发件人,脚本将从当前选定的电子邮件下载电子邮件,我需要编写脚本以从触发脚本的发件人处选择电子邮件 我是VBA的新手,任何帮助都将不胜感激 Public Sub SaveAttachments(Item As Outlook.MailItem) Dim objOL As Outlook.Applic

我正在使用Outlook脚本,该脚本将自动选择并从特定发件人下载附件。目前,报告是在数据库上生成的,并通过电子邮件发送到指定的地址-下一步是自动将这些报告下载到指定的文件夹。当前,如果电子邮件来自指定的发件人,脚本将从当前选定的电子邮件下载电子邮件,我需要编写脚本以从触发脚本的发件人处选择电子邮件

我是VBA的新手,任何帮助都将不胜感激

Public Sub SaveAttachments(Item As Outlook.MailItem)
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String

'Get the path to the target folder
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next

'Instantiate an Outlook Application Object
Set objOL = CreateObject("Outlook.Application")

'Get the collection of selected objects
Set objSelection = objOL.ActiveExplorer.Selection

'Set the Attachment folder
strFolderpath = strFolderpath & "\Attachments\"

'Check each selected item for attachements. If attachments exist, save them
'to the strFOlderPath folder and strip them from the item.
For Each objMsg In objSelection

'This code only strips attachments from mail items.
'If objMsg.class=olMail Then
'Get the Attachments collection of the item
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
strDeletedFile = ""

If lngCount > 0 Then

    'A count down loop needs to be used for removing items
    'from a collection. Otherwise the loop counter gets
    'confused and only every other item is removed

    For i = lngCount To 1 Step -1

        'Save attachment before deleting from item.
        'Get the file name
        strFile = objAttachments.Item(i).FileName

        'Combine with the path to the Temp folder.
        strFile = strFolderpath & strFile

        'Save the attachment as a file
        objAttachments.Item(i).SaveAsFile strFile

        'Delete the attachment
        objAttachments.Item(i).Delete

        'write the save as path to a string to add to the
        'message check from html and use html tags in link
        If objMsg.BodyFormat <> olFormatHTML Then
            strDeletedFile = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
        Else
            strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
            strFile & "'>" & strFile & "</a>"
        End If

        'use the MsgBox command to troubleshoot. Remove it from the final code.
        'MsgBox strDeletedFiles

    Next i

    'Adds the filename string to the message body and save it
    'Checks for HTML body
    If objMsg.BodyFormat <> olFormatHTML Then
        objMsg.Body = vbCrLf & "The File(s) were saved to " & strDeletedFiles & vbCrLf & objMsg.Body
    Else
        objMsg.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & objMsg.HTMLBody
    End If
    objMsg.Save
End If
Next

Exit Sub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
Public子存储附件(项目为Outlook.MailItem)
将objOL设置为Outlook.Application
将objMsg设置为Outlook.MailItem对象
作为Outlook.Attachments的Dim OBJAAttachments
Dim objSelection作为Outlook.Selection
我想我会坚持多久
暗计数等于长
作为字符串的Dim strFile
将strFolderpath设置为字符串
将strDeletedFile设置为字符串
'获取目标文件夹的路径
strFolderpath=CreateObject(“WScript.Shell”).SpecialFolders(16)
出错时继续下一步
'实例化Outlook应用程序对象
Set objOL=CreateObject(“Outlook.Application”)
'获取选定对象的集合
设置objSelection=objOL.ActiveExplorer.Selection
'设置附件文件夹
strFolderpath=strFolderpath&“\Attachments\”
'检查每个选定项目的附件。如果存在附件,请保存它们
'转到strFOlderPath文件夹,并将其从项目中删除。
对于objSelection中的每个objMsg
'此代码仅从邮件项目中删除附件。
'如果objMsg.class=olMail,则
'获取项目的附件集合
设置objAttachments=objMsg.Attachments
lngCount=objAttachments.Count
strDeletedFile=“”
如果lngCount>0,则
'需要使用倒计时循环来删除项目
“从一个集合中。否则,循环计数器将
“混乱,只有其他项目被删除
对于i=lngCount到1步骤-1
'在从项目中删除之前保存附件。
'获取文件名
strFile=objAttachments.Item(i).FileName
'与临时文件夹的路径合并。
strFile=strFolderpath&strFile
'将附件另存为文件
objAttachments.Item(i).SaveAsFile strFile
'删除附件
附件。第(i)项。删除
'将另存为路径写入要添加到
'从html检查消息并在链接中使用html标记
如果objMsg.BodyFormat olFormatHTML,则
strDeletedFile=strDeletedFile&vbCrLf&“
其他的
strDeletedFiles=strDeletedFiles&“
”&” 如果结束 '使用MsgBox命令进行故障排除。将其从最终代码中删除。 'MsgBox strDeletedFiles 接下来我 '将文件名字符串添加到消息正文并保存它 '检查HTML正文 如果objMsg.BodyFormat olFormatHTML,则 objMsg.Body=vbCrLf&“文件已保存到”&strDeletedFiles&vbCrLf&objMsg.Body 其他的 objMsg.HTMLBody=“”和“文件已保存到”&strDeletedFiles&“

”&objMsg.HTMLBody 如果结束 objMsg.Save 如果结束 下一个 出口接头: Set objAttachments=Nothing 设置objMsg=Nothing Set objSelection=Nothing Set objOL=无 端接头
将触发脚本的mailitem作为参数传递

例如,若您传递了项,则处理项

Sub CustomMailMessageRule(Item As MailItem)
    MsgBox "Mail message arrived: " & Item.Subject
End Sub

我做了一些修改,这段代码达到了预期的目的

Public Sub SaveAttachments(Item As Outlook.MailItem)
Dim objOL As Outlook.Application
Dim objAttachments As Outlook.Attachments
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String


'Get the path to the target folder
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next

'Instantiate an Outlook Application Object
Set objOL = CreateObject("Outlook.Application")

'Set the Attachment folder
strFolderpath = strFolderpath & "\Attachments\"

'Check each selected item for attachements. If attachments exist, save them
'to the strFOlderPath folder and strip them from the item.
For Each objAttachments In Item.Attachments

    'This code only strips attachments from mail items.
    'If objMsg.class=olMail Then
    'Get the Attachments collection of the item
    Set objAttachments = Item.Attachments
    lngCount = objAttachments.Count
    strDeletedFile = ""

    If lngCount > 0 Then

        'A count down loop needs to be used for removing items
        'from a collection. Otherwise the loop counter gets
        'confused and only every other item is removed

        For i = lngCount To 1 Step -1

            'Save attachment before deleting from item.
            'Get the file name
            strFile = objAttachments.Item(i).FileName

            'Combine with the path to the Temp folder.
            strFile = strFolderpath & strFile

            'Save the attachment as a file
            objAttachments.Item(i).SaveAsFile strFile

            'Delete the attachment
            objAttachments.Item(i).Delete

            'write the save as path to a string to add to the
            'message check from html and use html tags in link
            If Item.BodyFormat <> olFormatHTML Then
                strDeletedFile = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
            Else
                strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
                strFile & "'>" & strFile & "</a>"
            End If

            'use the MsgBox command to troubleshoot. Remove it from the final code.
            'MsgBox strDeletedFiles

        Next i

        'Adds the filename string to the message body and save it
        'Checks for HTML body
        If Item.BodyFormat <> olFormatHTML Then
             Item.Body = vbCrLf & "The File(s) were saved to " & strDeletedFiles & vbCrLf & Item.Body
         Else
            Item.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & Item.HTMLBody
        End If
        Item.Save
    End If
Next objAttachments

Exit Sub:

Set objAttachments = Nothing
Set objOL = Nothing
End Sub
Public子存储附件(项目为Outlook.MailItem)
将objOL设置为Outlook.Application
作为Outlook.Attachments的Dim OBJAAttachments
我想我会坚持多久
暗计数等于长
作为字符串的Dim strFile
将strFolderpath设置为字符串
将strDeletedFile设置为字符串
'获取目标文件夹的路径
strFolderpath=CreateObject(“WScript.Shell”).SpecialFolders(16)
出错时继续下一步
'实例化Outlook应用程序对象
Set objOL=CreateObject(“Outlook.Application”)
'设置附件文件夹
strFolderpath=strFolderpath&“\Attachments\”
'检查每个选定项目的附件。如果存在附件,请保存它们
'转到strFOlderPath文件夹,并将其从项目中删除。
对于项目附件中的每个OBJA附件
'此代码仅从邮件项目中删除附件。
'如果objMsg.class=olMail,则
'获取项目的附件集合
Set objAttachments=Item.Attachments
lngCount=objAttachments.Count
strDeletedFile=“”
如果lngCount>0,则
'需要使用倒计时循环来删除项目
“从一个集合中。否则,循环计数器将
“混乱,只有其他项目被删除
对于i=lngCount到1步骤-1
'在从项目中删除之前保存附件。
'获取文件名
strFile=objAttachments.Item(i).FileName
'与临时文件夹的路径合并。
strFile=strFolderpath&strFile
'将附件另存为文件
objAttachments.Item(i).SaveAsFile strFile
'删除附件
附件。第(i)项。删除
'将另存为路径写入要添加到
'从html检查消息并在链接中使用html标记
如果Item.BodyFormat为HTML格式,则
strDeletedFile=strDeletedFile&vbCrLf&“
其他的
strDeletedFiles=strDeletedFiles&“
”&” 如果结束 '使用MsgBox命令进行故障排除。将其从最终代码中删除。 'MsgBox strDeletedFiles 接下来我 '将文件名字符串添加到消息正文并保存它 '检查HTML正文 如果Item.BodyFormat为HTML格式,则 Item.Body=vbCrLf&“文件已保存到”&strDeletedFiles&vbCrLf&Item.Body 其他的 Item.HTMLBody=“”和“文件已保存到”&strDeletedFiles&“

”&Item.HTMLBody 如果结束 项目。保存 如果结束 下一个对象 出口接头: Set objAttachments=Nothing 设置objOL=Nothin