none
Custom Insert Hyperlink button RRS feed

  • Question

  • Hiya,

    Is it possible to create a custom Insert Hyperlink button that changes the URL of a local file to a remote one.

    So lets say the local file is Y:\Folder1\Document1.doc - I need this changed to https://docs.domainaname.com/Folder1/Document1.doc.

    I'm OK doing the string replace (replace Y:\ for https://docs.domainname.com/ and replace \ with /) as I'm fairly VB literate. Just not sure how I would invoke the existing Insert Hyperlink browse dialog, and then run the actual replace statements.

    Thanks

    • Moved by Max Meng Wednesday, January 30, 2013 6:34 AM
    Tuesday, January 29, 2013 9:05 AM

Answers

  • OK, with a combination of stuff online, I've come up with this. I understand that FileDialog is not natively supported in Outlook, hence the use of what's available in Word/Excel etc - can someone confirm this is still the case in Outlook 2010, and say if I can do this in a better way.

    Thanks.

    Sub DocumentHyperlink()
    
        Dim FileDialog As FileDialog
        Dim Word As Word.Application
        Dim DialogAction As Integer
        
        Set Word = New Word.Application
        Set FileDialog = Word.FileDialog(msoFileDialogFilePicker)
        Set Document = Application.ActiveInspector.WordEditor
        Set WordSelection = Document.Application.Selection
            
        FileDialog.InitialFileName = "Y:\Documents\"
        'FileDialog.AllowMultiSelect = False
        DialogAction = FileDialog.Show
        
        If DialogAction = -1 Then
            For Each File In FileDialog.SelectedItems
                File = Replace(File, "Y:\", "https:\\docs.domainname.com\")
                File = Replace(File, "\\corp.domainname.com\Public\Clients\", "https:\\docs.domainname.com\")
                File = Replace(File, "\Documents\", "\documents\")
                File = Replace(File, "\", "/")
                File = Replace(File, " ", "%20")
                WordSelection.Hyperlinks.Add Anchor:=WordSelection.Range, Address:=File, TextToDisplay:=File
                WordSelection.TypeText vbCrLf
            Next
        End If
        
        WordSelection.TypeText vbCrLf
        
        Set Document = Nothing
        Set WordSelection = Nothing
        
        Word.Quit
        Set Word = Nothing
        
    End Sub
    

    Tuesday, January 29, 2013 12:54 PM

All replies

  • OK, with a combination of stuff online, I've come up with this. I understand that FileDialog is not natively supported in Outlook, hence the use of what's available in Word/Excel etc - can someone confirm this is still the case in Outlook 2010, and say if I can do this in a better way.

    Thanks.

    Sub DocumentHyperlink()
    
        Dim FileDialog As FileDialog
        Dim Word As Word.Application
        Dim DialogAction As Integer
        
        Set Word = New Word.Application
        Set FileDialog = Word.FileDialog(msoFileDialogFilePicker)
        Set Document = Application.ActiveInspector.WordEditor
        Set WordSelection = Document.Application.Selection
            
        FileDialog.InitialFileName = "Y:\Documents\"
        'FileDialog.AllowMultiSelect = False
        DialogAction = FileDialog.Show
        
        If DialogAction = -1 Then
            For Each File In FileDialog.SelectedItems
                File = Replace(File, "Y:\", "https:\\docs.domainname.com\")
                File = Replace(File, "\\corp.domainname.com\Public\Clients\", "https:\\docs.domainname.com\")
                File = Replace(File, "\Documents\", "\documents\")
                File = Replace(File, "\", "/")
                File = Replace(File, " ", "%20")
                WordSelection.Hyperlinks.Add Anchor:=WordSelection.Range, Address:=File, TextToDisplay:=File
                WordSelection.TypeText vbCrLf
            Next
        End If
        
        WordSelection.TypeText vbCrLf
        
        Set Document = Nothing
        Set WordSelection = Nothing
        
        Word.Quit
        Set Word = Nothing
        
    End Sub
    

    Tuesday, January 29, 2013 12:54 PM
  • Hi Lanky Doodle,

    Sorry for the late in reply.

    I've found this page which I think you'll want to take a look.

    can someone confirm this is still the case in Outlook 2010

    The answer in the page above is nearly the same with the one you've posted, yet you can also see another solution which use Windows API to solve this issue.

    Hope it helps.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 5, 2013 6:40 AM
    Moderator
  • you can call CommandBars.ExecuteMso("HyperlinkInsert") and right after that (since it displays modal dialog your code will wait) iterate through Hyperlinks collection on word document object and replace local path with your own.
    Tuesday, February 5, 2013 7:43 AM