none
VBA for Outlook: How to create a Hyperlink via Macro

    Question

  • Hi everyone!

    I am trying to use VBA in MS Outlook to create a Hyperlink via a Macro. Can somebody please help me? Please, please, please...

    Here is what I'm trying to do:

    The situation is, that I often get emails saying sth like:  "Your file is at h:\file collection\marc"

    Normally I would then mark the path, copy it and paste it into the windows explorer. This works fine but I was thinking about how i can spead this process up with a macro.

    My idea was to
    1) let the user/me select the path given in the email
    2) then have a toolbar button or shortcut key, that starts a macro:
        the macro copies the selected text, and turns into into a hyperlink (insert a hyperlink with    
        the same address)
    3) the user then only has to click on the path with the mouth to "follow the hyperlink" and get     to the path  (or, even better: the user is automatically taken to that path. This is actually
        what this is all about: I want to to "taken to the path" with only a click)


    The problem is, that I the hyperlink always changes (and hence, the macro recorder is no good for this. It would always be a hyperlink to the same path!).

    The code from the recorder is:

    sub path
    Selection.Copy
        ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
            "h:\file collection\marc", SubAddress:="", _
            ScreenTip:="", TextToDisplay:= _
            "h:\file collection\marc"
    end sub

    Can any body help me with this problem please.... ! ? !

    Thank you sooo much !
    Marc


    Friday, May 19, 2006 3:37 PM

Answers

  • Hi Marc,

    You might try this code below to create a Hyperlink using the WordEditor of your selection in Active Item window:

    Note: You need to have Word Library ticked.

    Sub test()
    
       Dim objOL As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Dim objDoc As Word.Document
        Dim objSel As Word.Selection
      
        On Error Resume Next
        
        Set objOL = Application
        If objOL.ActiveInspector.EditorType = olEditorWord Then
            ' use WordEditor
            Set objDoc = objOL.ActiveInspector.WordEditor
            Set objNS = objOL.Session
            ' set current selection
            Set objSel = objDoc.Windows(1).Selection
                
            ' check selection
            ' Debug.Print objSel.Text
            objDoc.Hyperlinks.Add objSel.Range, Trim(objSel.Text), _
                              "", "", Trim(objSel.Text), ""
    
        End If
        
        Set objOL = Nothing
        Set objNS = Nothing
            
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Wednesday, February 15, 2012 8:01 PM
    Moderator

All replies

  • As a follow up to my previous message:

    Basically, the ideal solution would allow to me klick on the path or "select" it, and then I would be taken straight to this address...
    Friday, May 19, 2006 4:56 PM
  • Easier still. Let the user select/mark the path then click on a macro button or shortcut and it opens that location. Let me look around if it's possible to get selected text of current open email.
    Wednesday, January 11, 2012 8:36 PM
  • Hi Marc,

    You might try this code below to create a Hyperlink using the WordEditor of your selection in Active Item window:

    Note: You need to have Word Library ticked.

    Sub test()
    
       Dim objOL As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Dim objDoc As Word.Document
        Dim objSel As Word.Selection
      
        On Error Resume Next
        
        Set objOL = Application
        If objOL.ActiveInspector.EditorType = olEditorWord Then
            ' use WordEditor
            Set objDoc = objOL.ActiveInspector.WordEditor
            Set objNS = objOL.Session
            ' set current selection
            Set objSel = objDoc.Windows(1).Selection
                
            ' check selection
            ' Debug.Print objSel.Text
            objDoc.Hyperlinks.Add objSel.Range, Trim(objSel.Text), _
                              "", "", Trim(objSel.Text), ""
    
        End If
        
        Set objOL = Nothing
        Set objNS = Nothing
            
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Wednesday, February 15, 2012 8:01 PM
    Moderator