click email address hyperlink and attached open excel file

Hi Experts using excel 2007

is it possible when you click a hyperlink of a persons name, which is the email address to at the same time attach a copy of the open and current excel file to the are about to send.
Who is Participating?
Ken ButtersConnect With a Mentor Commented:
ok... let me expand on this...

The followhyperlink will execute after you have clicked on the link.  you will now have two simultaneous processes working.  Your spreadsheet and outlook.

So here is an overview of the order of events :

1) you click on the hyperlink
2) Outlook is opened an an email is created
3) the worksheet_FollowHyperlink function is called if it exists

When the worksheet_followhyperlink function is called, we need to add some specific code to the worksheet_followHyperlink function to add an attachment to the email.

To get at the Microsoft outlook library functions you should add a reference to :
Microsoft Outlook xx.x Object Library  xx.x will change depending upon your version of outlook.

In VB Editor go to tools / References and look for Microsoft outlook 12.0 object library or whatever the latest number you have available.

Then in the VBE project pane, select the worksheet that has the hyperlink

Add the following code to that worksheet.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    Dim newAttachment As attachment
    Dim mail As MailItem
    Dim olApp As Outlook.Application
    Dim inspectorObj As Inspector
    Dim TimeOutCounter As Integer
    Set olApp = New Outlook.Application
    Set inspectorObj = olApp.ActiveWindow
    '  May need to wait until outlook has opened and started email message
    '   --- also need a way to time-out in case outlook doesn't open the email
    TimeOutCounter = 0
        If inspectorObj.CurrentItem.Class = olMail Then: Exit Do
        Sleep 1000
        TimeOutCounter = TimeOutCounter + 1
        If TimeOutCounter > 5 Then: Exit Do
    If inspectorObj.CurrentItem.Class <> olMail Then: Exit Sub
    Set mail = inspectorObj.CurrentItem
    Set newAttachment = mail.Attachments.Add(ThisWorkbook.FullName, olByReference, , ThisWorkbook.FullName)
    newAttachment.DisplayName = ThisWorkbook.FullName

End Sub

Open in new window

Now when you click on the hyperlink, the current book should be added as an attachment to the email.

you should note that the workbook being attached is the version that was last saved.  So if you made changes you want emailed... you would have to save the workbook first.

you could do this by adding something like "" prior to adding it as an attachment.
Ken ButtersCommented:
Is the hyperlink in an excel worksheet?

If so you can use the following worksheet function:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
End Sub

Open in new window

route217Author Commented:
Excellent feedback let me test...

and yes it is an excel 2007 workbook...
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

route217Author Commented:
Hi buttersk

just tried vba in the worksheet function and its not adding an workbook attachment...
route217Author Commented:
Hi bittersk

any thought on the worksheet function....
route217Author Commented:
Excellent ignore my previous email..buttersk
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.