利用VBA自动保存outlook附件

创建VBA方法如下:

 

Public p As String '文件保存位置,也是解压文件存放位置
Public Sub SaveAttach(Item As Outlook.MailItem)
    p = "C:\Users\Administrator.TXV6HLXTU3ZW8KD\Desktop\"
    SaveAttachment Item, p, "*.rar"  '此处*.rar可以改成其他正则表达式
    ' MsgBox "附件已保存"
End Sub

' 保存附件
' path为保存路径,condition为附件名匹配条件
Private Sub SaveAttachment(ByVal Item As Object, path$, Optional condition$ = "*")
    Dim olAtt As Attachment
    Dim i As Integer
    Dim m As Long
    Dim s As String
    If Item.Attachments.Count > 0 Then
        For i = 1 To Item.Attachments.Count
            Set olAtt = Item.Attachments(i)
            '保存文件
            If olAtt.FileName Like condition Then
                olAtt.SaveAsFile path & olAtt.FileName
		'以下部分为解压rar文件,p为保存位置
                s = "C:\Program Files\WinRAR\WinRAR.exe" & " X " & path & olAtt.FileName & " " & p '注意找到解压软件位置
                m = Shell(s, vbHide)
            End If
        Next
    End If
    Set olAtt = Nothing
End Sub

在outlook中新建规则,选择动作为执行脚本,选择脚本为此方法,即可实现收到邮件后自动保存附件并解压到桌面。

 

保存附件代码

 

Sub 保存非标表格(mailitem As Outlook.mailitem)
    Dim olAtt As Attachment
    Set olAtt = mailitem.Attachments(1)
    olAtt.SaveAsFile "D:\baidu\Desktop\丝路非标邮件\非标\" & olAtt.FileName
End Sub

 

 

遍历文件夹获取正文HTML代码

 

Sub 遍历已有丝路()
    Dim NS As Outlook.NameSpace
    Dim folder As MAPIFolder
    Dim mailitem As mailitem
    Dim output, cmd, datetime As String
    Dim num, temp As Integer

    Set NS = Session.Application.GetNamespace("MAPI")
    Set folder = NS.GetDefaultFolder(olFolderInbox).Folders("丝路")
    num = folder.Items.Count
    temp = 0

    For i = 1 To num
        Set mailitem = folder.Items(i)
        If InStr(mailitem.Subject, "百度丝路运营数据报表") > 0 Then
            output = "D:\baidu\Desktop\丝路非标邮件\丝路\丝路邮件" & Right(mailitem.Subject, 10) & "_" & temp & ".txt"
            Open output For Output As #1
            Print #1, mailitem.HTMLBody
            '关闭文本文件
            Close #1
            temp = temp + 1
        End If
    Next

End Sub

 

 

 

 

 


 

### 回答1: 可以使用Outlook VBA编写代码来实现自动保存附件的功能。具体实现步骤如下: 1. 打开Outlook并进入VBA编辑器界面。 2. 在VBA编辑器中,选择“这台电脑上的项目”并新建一个模块。 3. 在模块中编写代码,实现自动保存附件的功能。例如,以下代码可以将所有收件箱中的邮件附件保存到指定的文件夹中: Sub SaveAttachments() Dim objOL As Outlook.Application Dim objMsg As Outlook.MailItem 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 your My Documents 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 ' Loop through each item in the collection. For Each objMsg In objSelection Set objAttachments = objMsg.Attachments lngCount = objAttachments.Count If lngCount > Then ' We need to use a count down loop 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 ' 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 Next i End If Next ExitSub: Set objAttachments = Nothing Set objMsg = Nothing Set objSelection = Nothing Set objOL = Nothing End Sub 4. 保存代码并关闭VBA编辑器。 5. 在Outlook中,打开收件箱并选择要自动保存附件的邮件。 6. 运行刚才编写的VBA代码,即可自动将邮件附件保存到指定的文件夹中。 ### 回答2: 在Outlook使用VBA自动保存附件,需要先开启Outlook的宏功能以及启用安全性设置,防止过多的垃圾邮件对电脑造成不必要的损害。 首先,打开OutlookVBA编辑器,选择“工具——引用”,勾选“Microsoft Outlook yy.0 Object Library”和“Microsoft Visual Basic for Applications Extensibility xx.0”两个选项,然后编写自动保存附件的代码。以下是一些示例代码,可能需要根据自己的需求进行修改。 1.自动保存指定文件夹中的附件。 ``` Sub AutoSaveAttachments() Dim oNS As Outlook.NameSpace Dim oFolder As Outlook.MAPIFolder Dim oMessage As Outlook.MailItem Dim oAttachment As Outlook.Attachment Dim sSaveFolder As String Set oNS = Application.GetNamespace("MAPI") '设定保存的文件夹路径,需事先创建好 sSaveFolder = "C:\Attachments\" Set oFolder = oNS.GetDefaultFolder(olFolderInbox) For Each oMessage In oFolder.Items For Each oAttachment In oMessage.Attachments oAttachment.SaveAsFile sSaveFolder & oAttachment.FileName Next Next Set oAttachment = Nothing Set oMessage = Nothing Set oFolder = Nothing Set oNS = Nothing End Sub ``` 2.自动保存指定邮件中的附件。 ``` Sub AutoSaveAttachments() Dim oNS As Outlook.NameSpace Dim oMessage As Outlook.MailItem Dim oAttachment As Outlook.Attachment Dim sSaveFolder As String Set oNS = Application.GetNamespace("MAPI") '设定保存的文件夹路径,需事先创建好 sSaveFolder = "C:\Attachments\" '设定需要保存附件的邮件主题 Set oMessage = oNS.GetDefaultFolder(olFolderInbox).Items.Find("[Subject]='主题名称'") For Each oAttachment In oMessage.Attachments oAttachment.SaveAsFile sSaveFolder & oAttachment.FileName Next Set oAttachment = Nothing Set oMessage = Nothing Set oNS = Nothing End Sub ``` 3.自动保存指定发件人发送的邮件中的附件。 ``` Sub AutoSaveAttachments() Dim oNS As Outlook.NameSpace Dim oFolder As Outlook.MAPIFolder Dim oMessage As Outlook.MailItem Dim oAttachment As Outlook.Attachment Dim oRecipient As Outlook.Recipient Dim sSaveFolder As String Set oNS = Application.GetNamespace("MAPI") '设定保存的文件夹路径,需事先创建好 sSaveFolder = "C:\Attachments\" Set oFolder = oNS.GetDefaultFolder(olFolderInbox) '设定需要保存附件的发件人名称 For Each oMessage In oFolder.Items.Restrict("[SenderName] = '发件人名称'") For Each oAttachment In oMessage.Attachments oAttachment.SaveAsFile sSaveFolder & oAttachment.FileName Next Next Set oAttachment = Nothing Set oMessage = Nothing Set oFolder = Nothing Set oNS = Nothing End Sub ``` 以上是一些常见的自动保存附件VBA代码,可根据实际情况进行修改和自定义。需要注意的是,自动保存附件可能会占据过多的磁盘空间,对电脑性能产生影响,应在必要时定期清理保存的附件。 ### 回答3: Outlook是微软公司的邮件管理软件,而VBA(Visual Basic for Applications)是一种基于Microsoft Visual Basic的编程语言。在Outlook VBA中,可以编写代码来自动执行一些任务,如自动保存附件。 首先,需要在Outlook中打开Visual Basic编辑器。选择“Tools”菜单下的“Macro”选项,再选择“Visual Basic Editor”。 然后,在VBA编辑器中,选择“Insert”菜单下的“Module”选项,输入下面的代码: Sub SaveAttachments() Dim objOL As Outlook.Application Dim objMsg As Outlook.MailItem Dim objAttachments As Outlook.Attachments Dim i As Long Dim lngCount As Long Dim strFile As String Dim strFolderpath As String ' 插入附件存储路径 strFolderpath = "C:\Attachments\" ' 创建Outlook应用程序对象 Set objOL = CreateObject("Outlook.Application") ' 循环浏览每封邮件 For Each objMsg In objOL.ActiveExplorer.Selection Set objAttachments = objMsg.Attachments lngCount = objAttachments.Count If lngCount > 0 Then ' 循环浏览每个附件 For i = 1 To lngCount ' 获取附件名称 strFile = strFolderpath & objAttachments.Item(i).FileName ' 存储附件 objAttachments.Item(i).SaveAsFile strFile Next i End If Next ExitSub: Set objAttachments = Nothing Set objMsg = Nothing Set objOL = Nothing End Sub 以上代码解释: 首先,定义一些变量,包括Outlook应用程序对象、邮件对象、附件对象、附件数量、附件文件名和附件存储路径。 然后,处理每个选定的邮件,获取其附件并保存。如果邮件没有附件,则跳过这个循环。 最后,清除每个对象的引用。 以上是使用Outlook VBA自动保存附件的基本步骤和代码示例。可以根据具体的需求来修改代码,如更改附件存储路径、筛选特定类型的附件等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值