如何保存附件并重命名

编程入门 行业动态 更新时间:2024-10-26 02:27:26
本文介绍了如何保存附件并重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的代码无法正常工作.这是我第一次运行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 name或temporary 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

更多推荐

如何保存附件并重命名

本文发布于:2023-11-06 07:54:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:附件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!