Macro to automate attaching two files to a new email

I always have to attach two files to multiple emails I send each day.  I'd like a macro that I can run after I draft a new email which attaches two files:

  • Attach the file: J:\Portfolio\_Reporting & Operations\REPO\RepoTkt.xlsm
  • Attach the newest PDF saved to U:\

The U:\ drive is our scan folder on our network.  I scan the image from the copier and immediately draft my email -- so the scan I need to attach is always the most recent. The filename is something like DOC_20170329101015.PDF  (format is "DOC_" followed by numeric string consisting of the date and time in the format of YYYYMMDDHHMMSS.pdf

Thank you!
Kevin BucklandInvestment OfficerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
Sub InsertAttachments()
    Const strPath As String = "U:\" '// Change as required
    Const strFileType As String = "DOC*.pdf"
    Dim strFile As String
    Dim myAttachments As Outlook.Attachments

    Dim NewMail As MailItem, oInspector As Inspector
    Set oInspector = Application.ActiveInspector
    If oInspector Is Nothing Then
        MsgBox "No active inspector"
    Else
        Set NewMail = oInspector.CurrentItem
        If NewMail.Sent Then
            MsgBox "This is not an editable email"
        Else
            Set myAttachments = NewMail.Attachments
            myAttachments.Add "J:\Portfolio\_Reporting & Operations\REPO\RepoTkt.xlsm"
            strFile = ""
            On Error Resume Next
            strFile = Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & strPath & strFileType & """ /O:-D /B /A:-D").StdOut.ReadAll, vbCrLf)(0)
            On Error GoTo 0
            If strFile <> "" Then myAttachments.Add strPath & strFile
        End If
    End If
End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin BucklandInvestment OfficerAuthor Commented:
Works wonderfully! Thank you!!
0
Kevin BucklandInvestment OfficerAuthor Commented:
I posted a follow up that is for a specific file (and doesn't attach the most recent scan):

https://www.experts-exchange.com/questions/29012843/Macro-to-automate-attaching-specific-file-to-a-new-email.html

Thanks
0
Kevin BucklandInvestment OfficerAuthor Commented:
I also posted another follow up email to get a clean macro that only attaches the latest scan and not the other specific file:

https://www.experts-exchange.com/questions/29012846/Macro-to-automate-attaching-the-newest-scan-to-a-new-email.html

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.