Я хочу переименовать вложение в Outlook, используя приведенный ниже код. Но я не знаю, как это сделать.
В идеале было бы хорошо, если бы он переименовывал вложенный файл с указанием человека, который его отправил, и текущей даты. Поэтому, если бы он был отправлен [email protected], он переименовал бы файл как [email protected].
См. ниже код
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)
objMsg.SenderEmailAddress предоставит вам адрес электронной почты отправителя
objMsg.ReceivedTime даст вам время
Используйте обновленный ответ :)
Работает как сон. Спасибо. Я новичок в программировании vba, поэтому объекты и пути к файлам незнакомы.
@don ... Рад помочь. Принять ответ :)
Последнее, что я вижу только сейчас. Возможно, могут быть два файла с одинаковым именем. Как сделать так, чтобы эта программа не перезаписывала уже сохраненный файл? может быть два файла в один и тот же день с одним и тем же отправителем (Джилл в данном случае), но файлы разные. Я попробовал strFile = strFolderpath & j & strFile с переменной j, но она теряет свою эффективность при поиске всех файлов, отправленных jill при поиске
Он работает как положено. Единственная мелочь заключается в том, что он не распознает расширение файла, поскольку, если он сохраняет его, он имеет [email protected], он видит расширение как .com230719. Есть ли способ просто сохранить первые пять букв, а затем дату? как в Джилл @ 230719