Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a hyperlink to an email to a file located on the hard drive. 1

Status
Not open for further replies.

navyguy

Programmer
Aug 10, 2002
30
US
I have the following code that I can run in MS Outlook to remove a file from an email message and save it to a hard drive location. Then text is added to the email with the address of where the file was stored.

What i would really like to do is to have a hyperlink to the file added to the body of the email so the attachmant. Does anybody know the VBA code that I would use to do this?


Sub SaveAttachment()

'Declaration
Dim myItems, myItem, myAttachments, myAttachment As Object
Dim myOrt As String
Dim myOlApp As New Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection

'Ask for destination folder
myOrt = InputBox("Destination", "Save Attachments", "C:\")

On Error Resume Next

'work on selected items
Set myOlExp = myOlApp.ActiveExplorer
Set myOlSel = myOlExp.Selection

'for all items do...
For Each myItem In myOlSel

'point on attachments
Set myAttachments = myItem.Attachments

'if there are some...
If myAttachments.Count > 0 Then

'add remark to message text
myItem.Body = myItem.Body & vbCrLf & _
"Removed Attachments:" & vbCrLf

'for all attachments do...
For i = 1 To myAttachments.Count

'save them to destination
myAttachments(i).SaveAsFile myOrt & _
myAttachments(i).DisplayName

'add name and destination to message text
'myItem.Body = myItem.Body &
'"File: " & myOrt & _
'myAttachments(i).DisplayName & vbCrLf

'add name and destination to message text
myItem.Body = myItem.Body & myOrt & myAttachments(i).DisplayName & " " & vbCrLf

Next i

'for all attachments do...
While myAttachments.Count > 0

'remove it (use this method in Outlook XP)
'myAttachments.Remove 1

'remove it (use this method in Outlook 2000)
myAttachments(1).Delete

Wend

'save item without attachments
myItem.Save
End If

Next

'free variables
Set myItems = Nothing
Set myItem = Nothing
Set myAttachments = Nothing
Set myAttachment = Nothing
Set myOlApp = Nothing
Set myOlExp = Nothing
Set myOlSel = Nothing

End Sub
 
APPEND "FILE:\\" TO YOUR PATH AND FILE NAME VARIABLE
 
Thanks, I tried it and it worked.

I had a problem however. The link ended if I had a space in the file name. I have figured out that the name needs to be in quotation marks.

This is probably a no-brainer but how do you append a quote in visual basic code.

If I use the code below the editor doesent accept a quote in quotes - it thinks I am missing another quote.

Any ideas on how I get around this?

myItem.Body = myItem.Body & """ & "file://" & _
myOrt & myAttachments(i).DisplayName & """ & vbCrLf
 
...you can search your string and replace each space with %20
(the hex representation of space)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top