如何保存附件并重命名 [英] How to save attachments and rename it

查看:266
本文介绍了如何保存附件并重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码无法正常工作.这是我第一次运行VBA脚本.我有大量来自传真机的电子邮件,我希望能够下载附件,将文件重命名为主题行,然后将其存储在我的计算机上.

我的第一次尝试是尝试编写一个宏,以便可以手动进行操作,但是在进行了一些研究之后,我感到自己想使规则生效.

这是我第一次尝试VBA,所以我什至不确定我是否在正确运行宏或规则脚本,但是我感觉我只是在代码中遗漏了一些东西.有什么想法吗?

Public Sub saveAttachtoDiskRule(itm As Outlook.MailItem)

    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim fso As Object
    Dim oldName

    Dim file As String
    Dim DateFormat As String
    Dim newName As String

    Dim enviro As String
    enviro = CStr(Environ("USERPROFILE"))
    saveFolder = enviro & "\" & "\destinationfolder\"

    Set fso = CreateObject("Scripting.FileSystemObject")

     For Each objAtt In itm.Attachments
     file = saveFolder & objAtt.DisplayName
     objAtt.SaveAsFile file

     Set oldName = fso.GetFile(file)
     newName = itm.Subject
     oldName.Name = newName

     Set objAtt = Nothing
     Next

     Set fso = Nothing
End Sub

解决方案

简单的规则脚本

Public Sub saveAttachtoDisk(olItem As Outlook.MailItem)
    Dim olAttachment As Outlook.Attachment
    Dim SaveFolder As String

    SaveFolder = "c:\temp\"

    For Each olAttachment In olItem.Attachments
        olAttachment.SaveAsFile SaveFolder & "\" & olAttachment.DisplayName
        Set olAttachment = Nothing
    Next
End Sub

环境功能

Environ函数使您可以获取代码当前正在其上运行的计算机的Windows环境变量,例如user nametemporary folder

的名称

示例

用户的个人资料文件夹示例为

Environ("USERPROFILE") & "\Documents\Temp\"
result
Windows Vista/7/8:  C:\Users\Omar\
Windows XP:         C:\Documents and Settings\Omar\

所有用户"或通用"配置文件文件夹

Environ("ALLUSERSPROFILE")

临时文件夹示例是

Environ("TEMP") (or Environ("TMP") - is the same)
result: C:\Users\omar\AppData\Local\Temp

I am having an issue getting my code to work. This is my first time running a VBA script. I have a large amount of emails coming in from a fax machine and I want to be able to download the attachments, rename the file to the subject line and then store them on my computer.

My first attempt, I tried to just write a macro so that I could manually do it but after doing some research, I was under the impression that I wanted to make rules work.

This is my first attempt at VBA so I'm not even sure I am running the macro or rule script correctly but I have a feeling I am just missing something in the code. Any thoughts?

Public Sub saveAttachtoDiskRule(itm As Outlook.MailItem)

    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim fso As Object
    Dim oldName

    Dim file As String
    Dim DateFormat As String
    Dim newName As String

    Dim enviro As String
    enviro = CStr(Environ("USERPROFILE"))
    saveFolder = enviro & "\" & "\destinationfolder\"

    Set fso = CreateObject("Scripting.FileSystemObject")

     For Each objAtt In itm.Attachments
     file = saveFolder & objAtt.DisplayName
     objAtt.SaveAsFile file

     Set oldName = fso.GetFile(file)
     newName = itm.Subject
     oldName.Name = newName

     Set objAtt = Nothing
     Next

     Set fso = Nothing
End Sub

解决方案

Here is simple rule script

Public Sub saveAttachtoDisk(olItem As Outlook.MailItem)
    Dim olAttachment As Outlook.Attachment
    Dim SaveFolder As String

    SaveFolder = "c:\temp\"

    For Each olAttachment In olItem.Attachments
        olAttachment.SaveAsFile SaveFolder & "\" & olAttachment.DisplayName
        Set olAttachment = Nothing
    Next
End Sub

Environ Function

The Environ function lets you get the Windows environment variables for the computer your code is currently running on, Like user name or the name of the temporary folder

Examples

user's profile folder example is

Environ("USERPROFILE") & "\Documents\Temp\"
result
Windows Vista/7/8:  C:\Users\Omar\
Windows XP:         C:\Documents and Settings\Omar\

"All Users" or "Common" profile folder

Environ("ALLUSERSPROFILE")

Temporary folder example is

Environ("TEMP") (or Environ("TMP") - is the same)
result: C:\Users\omar\AppData\Local\Temp

这篇关于如何保存附件并重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆