How do I rename a pdf attachment the same as the subject line and then email the renamed file to a different recipient.
For instance, the email I receive will have a subject line of "123456-CHM78912" but the attachment pdf name will be "INV-5".
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Temp\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Set your outlook rule and call the following scrip,
Option Explicit
Public Sub SaveAtmts(Item As Outlook.MailItem)
Dim Atmt As Outlook.Attachment
Dim Path As String
Dim SaveAtmt As String
Dim AtmtName As String
Path = "C:\Temp\"
AtmtName = Item.Subject & ".pdf"
For Each Atmt In Item.Attachments
If Right(Atmt.FileName, 3) = "pdf" Then
SaveAtmt = Path & AtmtName
Atmt.SaveAsFile SaveAtmt
End If
Next
' // Email the file
Set Item = Application.CreateItem(olMailItem)
With Item
.Subject = "Subject"
.body = AtmtName & " Report Attached "
.To = "0m3r#Email.com"
.Attachments.Add SaveAtmt
' // Display msg
' .Display
' ' Or Send
.Send
End With
End Sub
Related
I need VBA code to use in Outlook to extract the PDF attachments from emails and save into a designated folder. The user will choose the emails.
I have the below code but need it amended.
Public Sub SaveAutoAttach(item As Outlook.MailItem)
Dim object_attachment As Outlook.Attachment
Dim saveFolder As String
' Folder location when I want to save my file
saveFolder = "D:\Data\Archive"
For Each object_attachment In item.Attachments
' Criteria to save .doc files only
If InStr(object_attachment.DisplayName, ".doc") Then
object_attachment.SaveAsFile saveFolder & "\" & object_attachment.DisplayName
End If
Next
End Sub
As per your request, the following macro will save any PDF attachments from one or more user selected items.
Option Explicit
Sub SaveAttachmentsFromSelectedItemsPDF()
Dim currentItem As Object
Dim currentAttachment As Attachment
Dim saveToFolder As String
Dim savedFileCountPDF As Long
saveToFolder = "c:\users\domenic\desktop" 'change the path accordingly
savedFileCountPDF = 0
For Each currentItem In Application.ActiveExplorer.Selection
For Each currentAttachment In currentItem.Attachments
If UCase(Right(currentAttachment.DisplayName, 4)) = ".PDF" Then
currentAttachment.SaveAsFile saveToFolder & "\" & _
Left(currentAttachment.DisplayName, Len(currentAttachment.DisplayName) - 4) & "_" & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".pdf"
savedFileCountPDF = savedFileCountPDF + 1
End If
Next currentAttachment
Next currentItem
MsgBox "Number of PDF files saved: " & savedFileCountPDF, vbInformation
End Sub
I'm trying to save the daily system generated report attached to the e-mail to a folder.
Then, append the attachment filename with the date (modified date in the file). I am able to get the file saved to a folder. However, the renaming piece doesn't seem to work for me.
Can someone please help why the renaming piece isn't working? Much Thanks!
Public Sub saveAttachtoBIFolder(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim fso As Object
Dim oldName As Object
Dim file As String
Dim DateFormat As String
Dim newName As Object
saveFolder = "C:\BI Reports"
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
For Each objAtt In itm.Attachments
file = saveFolder & "\" & objAtt.DisplayName
objAtt.SaveAsFile file
Debug.Print "file="; file ' the full file path printed on immediate screen
Set oldName = fso.GetFile(file) ' issue seems to start from here
DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ")
newName = DateFormat & objAtt.DisplayName
oldName.Name = newName
Debug.Print "DateFormat="; DateFormat 'the date format printed on the immediate screen
Set objAtt = Nothing
Next
Set fso = Nothing
End Sub
Your newName needs to be string NOT Object so Dim newName As String also I would assign objAtt.DisplayName to string variable
See Example
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each objAtt In itm.Attachments
File = saveFolder & "\" & objAtt.DisplayName
objAtt.SaveAsFile File
Debug.Print File ' the full file path printed on immediate screen
Set oldName = FSO.GetFile(File) ' issue seems to start from here
Debug.Print oldName
Dim newName As String
Dim AtmtName As String
AtmtName = objAtt.DisplayName
Debug.Print AtmtName
DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ")
Debug.Print DateFormat
newName = DateFormat & " " & AtmtName
oldName.Name = newName
Debug.Print newName 'the date format printed on the immediate screen
Next
I am currently receiving weekly reports on outlook which I need to open and save in a specific folder. I have succeeded in renaming the file and transferring it to the desired file.
HOWEVER, the file format isn't the same as the file which is attached to the email, it is either registered as type "file" when I do not put a date format at the end or a type ".2016" file when I put one. When opened in Notepad the information is unreadable
Here is the code I currently use:
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormatdateFormat = Format(Now, "dd.mm.yyyy")
saveFolder = "C:\Users\mypathtotheattachment"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & "thenewnameofmyattachment" & ".csv"
Next
End Sub
Any help is welcome, I scanned all over the place for any information but I'm stuck...
Thanks!
Is this what your trying to do?
Option Explicit
Public Sub SaveAtmtToDisk(Item As Outlook.MailItem)
Dim Atmt As Outlook.Attachment
Dim SavePath As String
Dim FileName As String
' // Saved Location
SavePath = "C:\temp\"
' // 05 24 2016 Antoine.csv
FileName = Format(Now, "DD MM YYYY") & " Antoine.csv"
For Each Atmt In Item.Attachments
Atmt.SaveAsFile SavePath & "\" & FileName
Next
Set Atmt = Nothing
End Sub
Tested on Outlook 2010
I'm trying to create a macro where I can save the attachments from an email. The problem I'm currently having is that I want the macro to add the ReceivedTime of the email on the file name it saves (i.e.: File TESTSHEET.xls was received on 2016-01-01 3:02AM. I want the saved file to show 201601010302AM-TESTSHEET.xls or something similar)
Here's my current code:
Public itm As Object
Public Sub saveAttachtoDisk()
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Users\Username\Documents\TEST REPORTS"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile objAtt.DisplayName
Next objAtt
End Sub
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 objDate As String
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim StrDate As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Set itm = Application.CreateItem(olMailItem)
Dim CurrentMsg As Outlook.MailItem
' 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
' Set the Attachment folder.
strFolderpath = strFolderpath & "\TEST REPORTS\"
' Check each selected item for attachments. If attachments exist,
' save them to the strFolderPath folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
strDeletedFiles = ""
If lngCount > 0 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
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.Item(i).FileName
' Combine with the path to the Save folder.
StrDate = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
strFile = strFolderpath & StrDate & strFile
' Save the attachment as a file.
MsgBox strFile
objAttachments.Item(i).SaveAsFile strFile
' Delete the attachment.
objAttachments.Item(i).Delete
'write the save as path to a string to add to the message
'check for html and use html tags in link
If objMsg.BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
strFile & "'>" & strFile & "</a>"
End If
'Use the MsgBox command to troubleshoot. Remove it from the final code.
'MsgBox strDeletedFiles
Next i
' Adds the filename string to the message body and save it
' Check for HTML body
If objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & objMsg.Body
Else
objMsg.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & objMsg.HTMLBody
End If
objMsg.Save
End If
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
Thank you in advance for your help!!
Use objMsg not itm.
' StrDate = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
StrDate = Format(objMsg.ReceivedTime, "yyyy-mm-dd Hmm ")
Additionally drop other code with itm and as well objOL
' Set itm = Application.CreateItem(olMailItem)
' Dim CurrentMsg As Outlook.MailItem
' On Error Resume Next
' Instantiate an Outlook Application object.
' Set objOL = CreateObject("Outlook.Application")
Do not use On Error Resume Next until you know what you are doing.
I have the following vba code that saves attachments in an email.
This works fine for .docx, .jpg etc but I need to use it to extract multiple .msg attachments which doesn't work.
The code is
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Test\"
For Each objAtt In itm.Attachments
stFileName = saveFolder & "\" & objAtt.DisplayName
i = 0
JumpHere:
If Dir(stFileName) = "" Then
objAtt.SaveAsFile stFileName
Else
i = i + 1
stFileName = saveFolder & "\" & i & " - " & objAtt.DisplayName
GoTo JumpHere
End If
Set objAtt = Nothing
Next
End Sub
The error concerns the line - If Dir(stFileName) = "" Then
Following our chat, here is the final code :
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim i As Integer
saveFolder = "C:\Test\"
For Each objAtt In itm.Attachments
stFileName = saveFolder & objAtt.FileName
i = 0
'Loop to find the first available filename
Do While Dir(stFileName) <> ""
i = i + 1
stFileName = saveFolder & i & " - " & objAtt.FileName
Loop
objAtt.SaveAsFile stFileName
Next
End Sub
Regards,
Max