PDA

View Full Version : Adding hyperlink to message



Scott B
03-28-2012, 03:28 PM
I have a bit of code that takes what is on the clipboard and pastes it into a Outlook 2003 message as a hyperlink. The clipboard contents should be the fullname(path and filename) obtained from another macro in Word 2003 (ActiveDocument.FullName). The Outlook message format is set to HTML. I have a function(GetFilename) that takes the string(strClip) and returns only the filename to display only that for the hyperlink. My objective is to be able to paste multiple links to an email message. It all seems to work except that it won't allow me to paste another link into the email and the curser jumps back to the beginning of the pasted link. It overwrites the initial paste. For the carriage return I've tried "& vbCrLf", "& Chr(13)" and "& <br>" to get the return, but nothing has worked. Thanks in advance for any help with this. The code is below:


Sub PasteLink()
Dim MyData As DataObject
Dim strClip As String
Dim msg As Outlook.MailItem

Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText
strShortClip = GetFilename(strClip)

Set msg = Application.ActiveInspector.CurrentItem

msg.HTMLBody = "<a href=" & strClip & ">" & strShortClip & "</a>" & "<br>"

End Sub

skatonni
03-28-2012, 07:37 PM
msg.HTMLBody = msg.HTMLBody + "<a href=" & strClip & ">" & strShortClip & "</a>" & "<br>"

Scott B
03-29-2012, 10:39 AM
Thanks skatonni for the post. This certainly allows for multiple pastes but the curser jumps to the top of the message after a paste. I next need to get the curser to be positioned after the pasted link.

Thanks again.

Scott

skatonni
03-30-2012, 04:14 AM
Thanks skatonni for the post. This certainly allows for multiple pastes but the curser jumps to the top of the message after a paste. I next need to get the curser to be positioned after the pasted link.

Thanks again.

Scott

SendKeys "^{END}"

Scott B
03-31-2012, 03:15 PM
Here is the revised edition. I added an extra variable to handle the spaces in the hyperlink. Thanks again to skatonni, Sendkeys was my first attempt but I must have forgot to enclose it in quotes.
The first VBA block is the GetFilename function. This removes the path from the full pathname. The second is the Pastelink module.


Function GetFilename(ByVal strClip As String) As String
If Right$(strClip, 1) <> "\" And Len(strClip) > 0 Then
GetFilename = GetFilename(Left$(strClip, Len(strClip) - 1)) + Right$(strClip, 1)
End If
End Function





Sub PasteLink()
Dim MyData As DataObject
Dim strClip As String
Dim strHLink As String
Dim msg As Outlook.MailItem
Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText
strHLink = Chr(34) & "file://" & strClip & Chr(34)
strShortClip = GetFilename(strClip)
Set msg = Application.ActiveInspector.CurrentItem
msg.HTMLBody = msg.HTMLBody + "<a href=" & strHLink & ">" & strShortClip & "</a><br>"
SendKeys "^{END}"
End Sub



The following Word macro places the filename and path into the clipboard which is then used by the Outlook code above to paste the hyperlink into a message without the dispalying the path.


Sub FileName()
Dim dob As New DataObject
dob.SetText ActiveDocument.FullName
dob.PutInClipboard
End Sub

skatonni
04-02-2012, 05:45 PM
For the benefit of some who might like to try it.
To use the DataObject in your code, you must set a reference to the Microsoft Forms 2.0 Object Library.