Consulting

Results 1 to 6 of 6

Thread: Adding hyperlink to message

  1. #1
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    3
    Location

    Adding hyperlink to message

    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:

    [VBA]
    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


    [/VBA]

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    msg.HTMLBody = msg.HTMLBody + "<a href=" & strClip & ">" & strShortClip & "</a>" & "<br>"

  3. #3
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    3
    Location
    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

  4. #4
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Quote Originally Posted by Scott B
    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}"

  5. #5
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    3
    Location
    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.
    [VBA]

    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

    [/VBA]

    [VBA]

    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

    [/VBA]

    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.

    [VBA]
    Sub FileName()
    Dim dob As New DataObject
    dob.SetText ActiveDocument.FullName
    dob.PutInClipboard
    End Sub
    [/VBA]

  6. #6
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •