Macro to attach files to Outlook Email.

Hi

I am wanting to use a macro in Outlook to automatically attach files to the email message as attachments.  The file names and paths would be stored as bookmarks in the body of the message.  The text within the bookmarks gets populated from a merge process there may be up to 10 bookmarks, (AttachFile1, AttachFile2, etc etc etc).  Below is the simple AttachmentAdd which works, but the file names being attached  vary with each email message.  Therefore I am storing the filenames to be attached in a set of bookmarks,

Dim objMessage As Object
Set objMessage = ActiveInspector.CurrentItem
objMessage.Attachments.Add ("C:\Path\Filename.pdf")


Any suggestions would be greatly appreciated.

Many thanks
Regards

David
dbscpmAsked:
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.

NorieAnalyst Assistant Commented:
David

Perhaps you could try something like this.
Sub AttachBookmarks()
Dim objMessage As Object
Dim bk As Object

    Set objMessage = ActiveInspector.CurrentItem

    For Each bk In objMessage.GetInspector.WordEditor.bookmarks
        objMessage.Attachments.Add bk.Range.Text
    Next bk
    
End Sub

Open in new window

0
dbscpmAuthor Commented:
Hi
Thank you for the very quick reply.  I have tried your suggestion, however I am getting the error message:

Error.JPG
I have added the bookmarks into the body of the email and run the macro as suggested.  Below is a screenshot of the email.

EmailExample.JPG
0
NorieAnalyst Assistant Commented:
Andrew

Are all the bookmarks enclosed in [ ] and those weird looking quotes?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

dbscpmAuthor Commented:
Hi

Yes all the bookmarks are surrounded by [ ].

The example I am using to test only has the one bookmark.  It is called AttachFile1.  

If I use the function GoTo Bookmark,  it finds it.  

So I think it is defined correctly.   Is there a way I can get the macro to echo back the text to ensure it is using the full bookmark text??
0
NorieAnalyst Assistant Commented:
David.

The code I posted does return the 'full' text of the bookmark, but that appears to have some unneeded characters.

If It's definitely just the [ ] around the file name we could try something like this.
Sub AttachBookmarks()
Dim objMessage As Object
Dim bk As Object
Dim strFileName As String

    Set objMessage = ActiveInspector.CurrentItem

    For Each bk In objMessage.GetInspector.WordEditor.Bookmarks
        strFileName = bk.Range.Text
        strFileName = Replace(Replace(strFileName, "[", ""), "]", "")
        objMessage.Attachments.Add strFileName
    Next bk

End Sub

Open in new window

0
dbscpmAuthor Commented:
Hi

I have added some your suggestion and still get the error that "cannot find this file.  Verify the oath and file name are correct".  

I added a msg box to return the text and it is correct.
0
dbscpmAuthor Commented:
Hi Norie. I appreciate your assistance with my problem.  Have you any other suggestions about why the macro cannot attach the file as defined by the text of the bookmark???
0
NorieAnalyst Assistant Commented:
Are you sure there aren't any other strange characters in the text coming from the bookmark?

In the image you posted it appeared as though there were strange quotes, and perhaps even some spaces at the end.

How are the bookmarks being populated by the merge process you mentioned?
0
dbscpmAuthor Commented:
I have removed the quotes and the spaces from the bookmark text.  The only text between the [ ] is the file name and path.
0
NorieAnalyst Assistant Commented:
Are the filenames and paths definitely valid?
0
dbscpmAuthor Commented:
Yes.  I added a msgbox to display the result and it shows the correct file name and path.  The file is definitely in that location email etc.
0
NorieAnalyst Assistant Commented:
Does this make any difference?
Sub AttachBookmarks()
Dim objMessage As Object
Dim bk As Object
Dim strFileName As String

    Set objMessage = ActiveInspector.CurrentItem

    For Each bk In objMessage.GetInspector.WordEditor.Bookmarks
        strFileName = bk.Range.Text
        strFileName = Trim(Replace(Replace(strFileName, "[", ""), "]", ""))
        objMessage.Attachments.Add strFileName
    Next bk

End Sub

Open in new window

1
dbscpmAuthor Commented:
Thank you so much.  That worked.  Not sure what the exact problem was.  But using the trim seemed to correct it.  I could not see any leading or trailing spaces.

If I only wanted certain bookmarks to be identified by the macro, eg only bookmarks starting with "addfile" can that be done???
0
NorieAnalyst Assistant Commented:
This is off the top of my head.
Sub AttachBookmarks()
Dim objMessage As Object
Dim bk As Object
Dim strFileName As String

    Set objMessage = ActiveInspector.CurrentItem

    For Each bk In objMessage.GetInspector.WordEditor.Bookmarks
        If LCase(bk.Name) Like "addfile*" Then
            strFileName = bk.Range.Text
            strFileName = Trim(Replace(Replace(strFileName, "[", ""), "]", ""))
            objMessage.Attachments.Add strFileName
        End If
    Next bk

End Sub

Open in new window

1

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
dbscpmAuthor Commented:
Thank you Norie for your excellent assistance.
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.