Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何重命名附件并将其保存在outlook中?_Vba_Outlook_Ms Office - Fatal编程技术网

Vba 如何重命名附件并将其保存在outlook中?

Vba 如何重命名附件并将其保存在outlook中?,vba,outlook,ms-office,Vba,Outlook,Ms Office,我想使用以下代码重命名outlook中的附件。但是我不知道怎么做 理想情况下,如果将文件附件与发送人和当前日期一起重命名,则效果会更好。所以如果它是由jill@gmail.com它会将文件重命名为jill@gmail.com230719 请参阅下面的代码 Public Sub SaveAttachments() Dim objOL As Outlook.Application Dim objMsg As Outlook.MailItem 'Object Dim objAttachments As

我想使用以下代码重命名outlook中的附件。但是我不知道怎么做

理想情况下,如果将文件附件与发送人和当前日期一起重命名,则效果会更好。所以如果它是由jill@gmail.com它会将文件重命名为jill@gmail.com230719

请参阅下面的代码

Public Sub SaveAttachments()
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 your My Documents folder
    strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
    On Error Resume Next

    ' Instantiate an Outlook Application object.
    Set objOL = Application

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

' The attachment folder needs to exist
' You can change this to another folder name of your choice

    ' Set the Attachment folder.
    strFolderpath = strFolderpath & "\OLAttachments\"

    ' Check each selected item for attachments.
    For Each objMsg In objSelection

    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count

    If lngCount > 0 Then

    ' 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

此行在代码中指定名称:

strFile = objAttachments.Item(i).FileName
strFile = Split(objMsg.SenderEmailAddress, "@")(0) & Format(objMsg.ReceivedTime, "ddmmyyyy") & "." & Split(Right(objAttachments.Item(i).FileName,8), ".")(1)
将其更改为:

strFile = objAttachments.Item(i).FileName
strFile = Split(objMsg.SenderEmailAddress, "@")(0) & Format(objMsg.ReceivedTime, "ddmmyyyy") & "." & Split(Right(objAttachments.Item(i).FileName,8), ".")(1)

objMsg.SenderEmailAddress
将为您提供发件人的电子邮件地址


objMsg.ReceivedTime
将为您提供时间

它正常工作。唯一的一件小事是它无法识别文件扩展名,因为如果它保存文件扩展名,它会jill@gmail.com230719它将扩展名视为.com230719。有没有办法只保存前五个字母,然后保存日期?如jill@230719Use更新后的答案:)就像做梦一样。谢谢我是vba编程的新手,所以对象和文件路径unfamiliar@don…很乐意帮忙。接受答案:)我现在只看到最后一件事。可能存在两个同名文件。如何避免此程序覆盖已保存的文件?同一天可能有两个文件与同一个发件人(本例中为jill),但文件不同。我尝试了strFile=strFolderpath&j&strFile,其中j是一个变量,但在搜索时搜索吉尔发送的所有文件会失去效果