Share via

Need help writing a macro to insert a link from another document

Anonymous
2023-03-12T23:23:19+00:00

I want to create a macro called InsertLink that inserts a link from another document from the clipboard at the current selected text in my document. For example, I have a document with the words ListView and TreeView. I selected ListView in my document. I then went to the ListView class in System.Windows.Controls and selected its URL to use as my link. I created a macro in my document called InsertLink to be activated by the keys Alt + I + L. I created the macro as follows using the keyboard:

Alt, N, I, I, Ctrl + V, Enter

The Alt, N switches Word to the Insert page.

The first 'I' opens the Link dropdown.

The second 'I' opens the Insert Link... dialog box.

Ctrl + V inserts the URL.

Enter closes the dialog box and the ListView link is set in my document.

I then ended (saved) the macro.

However, if I then select TreeView, go to the TreeView class in System.Windows.Control and select its URL to use as the TreeView link, when I run the macro, it inserts the ListView link into the selected TreeView text. How do I set up the macro so that each time I run it, it uses the current text in the clipboard, not the text that was put there the first time it was run?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2023-03-13T13:15:10+00:00

    Sorry, I forgot something.

    In the macro editor, click the Tools menu and click References in that menu. This dialog opens:

    Click the check box for "Microsoft Forms 2.0 Object Library" and then click OK. That will make the Forms library available, and it contains the definition of the DataObject. Save the document that contains the macro -- the reference will be saved with it.

    3 people found this answer helpful.
    0 comments No comments

Answer accepted by question author

  1. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2023-03-13T02:03:56+00:00

    If you open the macro in the macro editor and look at the code that the recorder created, you'll find that it didn't record the series of steps you used to get to the Insert Hyperlink dialog, but only the final result of inserting the hyperlink. The macro includes the text of the URL you entered while recording, not the act of pasting it from the clipboard. It looks something like this, but with your first URL:

    Sub Macro1()

    ' 
    
    ' Macro1 Macro 
    
    ' 
    
    '
    
         ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
    
            "https://wordmvp.com/FAQs/InterDev/ControlWordFromXL.htm", SubAddress:="" _
    
             , ScreenTip:="", TextToDisplay:= _ 
    
            "https://wordmvp.com/FAQs/InterDev/ControlWordFromXL.htm" 
    
    End Sub
    

    So every time you run that macro, you'll get the same URL in the hyperlink.

    There is no way to get the recorder to do what you want. The macro has to be written by a person, not by the recorder.

    This version will do what you want:

    Sub PasteURLtoHyperlink() 
    
        Dim oData As DataObject 
    
        Set oData = New DataObject 
    
        oData.GetFromClipboard 
    
        ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _ 
    
                Address:=oData.GetText(1), SubAddress:="", _ 
    
                ScreenTip:="", TextToDisplay:=oData.GetText(1) 
    
    End Sub
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2023-03-13T17:41:15+00:00

    That's good. Thanks for letting me know.

    0 comments No comments
  2. Anonymous
    2023-03-13T16:29:23+00:00

    Thanks again, Jay.

    However, I did have to make one change. I changed TextToDisplay from 'oData.GetText(1)' to 'Selection.Range' to show the selection as a hyperlink instead of inserting the actual URL that was in the clipboard.

    0 comments No comments
  3. Anonymous
    2023-03-13T12:57:46+00:00

    Thanks, Jay. However, when I insert that macro and run it, Microsoft Visual Basic for Applications reports a 'Compile error: User-defined type not defined" and it highlights the error as "oData As DataObject". Do you know how to fix that?

    0 comments No comments