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?
 
NorieVBA ExpertCommented:
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
 
NorieVBA ExpertCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
NorieVBA ExpertCommented:
Andrew

Are all the bookmarks enclosed in [ ] and those weird looking quotes?
0
 
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
 
NorieVBA ExpertCommented:
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
 
NorieVBA ExpertCommented:
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
 
NorieVBA ExpertCommented:
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
 
NorieVBA ExpertCommented:
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
 
dbscpmAuthor Commented:
Thank you Norie for your excellent assistance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.