none
Handle a click on a link in the document RRS feed

  • Question

  • Hi all;

    I thought I had asked this before but I searched and can't find it. What I need to do is have a link in the document that when the user clicks on it, I get an event and handle the click. It does not have to be an actual hyperlink, but it does need to look like one (underlined, link color, finger cursor when the mouse is over it).

    ??? - thanks - dave


    Very funny video - What's your Metaphor?
    Sunday, March 6, 2011 8:47 PM

Answers

  • David,

    Yes, you are right.  Whether you choose to create the bookmark so that it spans all of the text of the hyperlink, or whether you create the bookmark so that it is at the insertion point at, say, the beginning of the hyperlink, is a matter that doesn't affect the general purpose of the solution. 

    The idea is that, when the user clicks the hyperlink, it jumps to the bookmark, which also happens to be at the hyperlink location.  After that, the code checks to see if the bookmark is selected: if it is selected, then your code needs to fire. 

    The point is, of course, that the user can make a selection anywhere in the document and that will fire the WindowSelectionChange event and you don't want your code to fire if the bookmark at the hyperlink is not selected.

    I assume from your reply that, if the user selects the bookmark range using the keyboard, then you would not want your code to fire.  Sorry I don't have time today to investigate this, but I shall take a look as soon as possible, probably this weekend.  

    My initial reaction would be to seek a solution whereby a Boolean flag is set to TRUE (if possible) whenever the hyperlink is clicked, so that the code could then check whether the bookmark range is selected AND if the flag is TRUE.  (The code would then have to set the flag to FALSE, of course.)  However, off the top of my head, I don't know if it's possible to set the flag in the way I've just suggested.  That doesn't mean there might not be another solution.  There usually is!

    Also, I think I've created a new event class that has a Document property, so you pass in a reference to the document that has the hyperlink — just in case the user has more than one document open and just in case the other document has a similar hyperlink/bookmark pair.  I shall look into this too and let you have revised code after the weekend. 

     

    Rich,

    My first reaction was to create a macrobutton, as you suggest, but David's original requirement was for a link that highlights when the mouse if moves over it.  I think that's why I went down the route I did.

     

    A General Observation

    As an Access programmer/developer, I'm astonished at how few events the Word object model supports.

     

    Regards
    Geoff

     

    • Marked as answer by DavidThi808 Tuesday, May 3, 2011 3:09 PM
    Friday, March 25, 2011 11:37 AM
  • Hi Dave,

    Maybe this link can help.

    http://msdn.microsoft.com/en-us/library/bb608621.aspx

    What are you writing your add-in with?


    Regards
    • Marked as answer by DavidThi808 Tuesday, May 3, 2011 3:09 PM
    Friday, March 25, 2011 8:40 PM

All replies

  • Hello Dave,

    Thanks for posting. If the link in the document is an actually hyperlink, by reading over Word Object model, there is no an event for clicking exposed by object model. For more information, we also could refer to the Cindy's post in this MSDN page: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f27c8dab-9665-4584-8965-04f11dd1719c.

    So as far as I see, even if the link is not an actually hyperlink, the scenario is the same.

    If you have any concern on this post, just feel free to follow up. Have a nice day.

     


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 7, 2011 7:57 AM
  • Hi;

    Cindy's post was exactly what I needed. Ok, I'm not using VSTO so what do I have to do to add a LinkLabel to a document? I like that approach best because I assume I can use it in Excel & PPT too.

    thanks - dave


    Very funny video - What's your Metaphor?
    Monday, March 7, 2011 6:28 PM
  • Hello again Dave,

    As far as I see, the linklable could only be added into a document using VSTO. Please refer to this MSDN page: http://msdn.microsoft.com/en-us/library/x97a5x3s(VS.80).aspx.

    Thanks for your understanding, and have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 8, 2011 6:09 AM
  • Hi;

    Is there any explination about how VSTO accomplishes this? Because if I can also create a LinkLabel in WOrd (and Excel/PPT), then that would be a very good solution for us.

    thanks - dave


    Very funny video - What's your Metaphor?
    Wednesday, March 9, 2011 5:41 PM
  • Hello again Dave,

    In the MSDN page above, it says, Visual Studio Tools for Office provides a number of helper methods that can be used to programmatically add Windows Forms controls to Word documents. Each method is overloaded so that you can pass in a Range or the coordinates of a specific location. When you create a control by passing in a Range, the control is added as an InlineShape, placed at the start of the range. You cannot change the Top and Left properties of controls that are added in line with text. When you create a control by passing in coordinates, the control is added as a floating Shape, placed at the location specified.

    So I think it could explain how VSTO to add a LinkLabel to a document.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 10, 2011 7:57 AM
  • An InlineShape is easy to create and add - we can do that. How do we get an event when they click on that shape?

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, March 10, 2011 4:48 PM
  • Hello again Dave,

    Based on my experience, there is also no sucn an event for a shape/Inlineshape exposed by Word object model. Have a nice weekend.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 11, 2011 9:27 AM
  • David,

    > What I need to do is have a linke in the document
    > that when the user clicks on it, I get an event and
    > handle the click.  It does not have to be an actual
    > hyperlink, but it does need to look like one
    > (underlined, link color, finger cursor when the
    > mouse is over it).

     

    The following steps will create the above using VBA.

     

    1.     Click in the document where you want the hyperlink.

    2.     Insert a bookmark, named "Hyperlink1".

    3.     Insert a hyperlink that goes to the bookmark "Hyperlink1".  (The bookmark and the hyperlink are at the same insertion point in the document.)

    4.     In the VBA editor, insert a class (not a standard) module into the document.

    5.     Give the class module the name "EventClassModule".

    6.     Paste the following code into the class module:

     

    Option Explicit
    
    ' This is the code for the class module
    ' named "EventClassModule".
    
    
    ' Declare a WithEvents variable to store
    ' reference to the Word Application object:
    Public WithEvents mobjWORD As Word.Application
    
    
    Private Sub Class_Initialize()
    
     ' This Class_Initialize event procedure
     ' runs when this class is instantiated
     ' (when a new instance of this class is created).
     
     ' Store reference to the Word.Application object:
     Set mobjWORD = Word.Application
    
    End Sub
    
    Private Sub Class_Terminate()
    
     ' This Class_Terminate event procedure
     ' runs when this class terminates.
     
     ' Destroy reference:
     Set mobjWORD = Nothing
    
    End Sub
    
    Private Sub mobjWORD_WindowSelectionChange(ByVal Sel As Selection)
    
     ' This is the event procedure for the
     ' Word Application WindowSelectionChange event.
     '
     ' This event occurs when the selection changes
     ' in the active document window.
     '
     ' IN:
     '
     ' Sel (Automatically provided by Word):
     ' The new selection.
     '
     
     
     ' Declare the name of the bookmark at the
     ' beginning of the hyperlink:
     Const strcBookmark1 As String = "MyHyperlink1"
     
     Dim objDOC As Word.Document
     Dim objBKM As Word.Bookmark
     Dim objBKMRNG As Word.Range
     
     
     ' Point to the document in which
     ' the new selection has been made:
     Set objDOC = Sel.Document
     
     ' See if bookmark exists in document:
     On Error Resume Next
     Set objBKM = objDOC.Bookmarks(strcBookmark1)
     If Err.Number <> 0 Then
     ' Bookmark does not exist!
     GoTo Exit_mobjWORD_WindowSelectionChange
     End If
     
     ' Point to bookmark range:
     Set objBKMRNG = objBKM.Range
     
     ' Compare bookmark range with new selection range:
     If (Sel.Range.Start = objBKMRNG.Start) _
     And (Sel.Range.End = objBKMRNG.End) Then
     
     ' Ranges are the same.
     ' Call code:
     Call EventHandlerForHyperlink1
     
     End If
    
    
    Exit_mobjWORD_WindowSelectionChange:
    
     Exit Sub
    
    End Sub
    
    Private Sub EventHandlerForHyperlink1()
    
     MsgBox "EventHandlerForHyperlink1 has run!"
    
    End Sub
    

     

    7.     Insert a standard module.

    8.     Paste the following code into the standard module:

    Option Explicit
    
    '  This is the code for a standard module.
    
    '  Declare public variable to store pointer
    '  to the EventClassModule class:
    Public gobjEH As EventClassModule
    
    

     

    9.     Open the "ThisDocument" module.

    10.     Paste the following code into the "ThisDocument" module:

    Option Explicit
    
    '  This code is in the ThisDocument module.
    
    Private Sub Document_New()
      
      '  This is the Document_New event procedure.
      '  This procedure runs whenever a new document
      '  is created from a template.
      
      Call StartUp
    
    End Sub
    
    Private Sub Document_Open()
      
      '  This is the Document_Open event procedure.
      '  This procedure runs whenever this document
      '  is opened.
      
      Call StartUp
    
    End Sub
    
    Private Sub StartUp()
    
      Set gobjEH = New EventClassModule
    
    End Sub
    
    

    11.     To insert the above code from scratch, you could select "Document" in the object drop-down list and "Open" or "New" in the properties drop-down list. 

    12.    Paragraph 10 includes code for the Open and New events, but you will only need one of these procedures.  If the user is opening a document, retain the Document_Open procedure.  If the user is creating a new document from a template, retain the Document_New procedure.   

    Sunday, March 13, 2011 12:55 AM
  • Further thoughts on my previous solution.

    1.     In the EventClassModule module, it is better that the variable storing the reference to the Word.Application object should be Private.  (I copied the word "Public" from the Word Help topic on "Using Events with the Application Object";  but, in fact, you don't want any external code interfering with the mobjWORD variable.)  Therefore, change the declaration in the class using the Private keyword, as follows:

    '  Declare a WithEvents variable to store
    '  reference to the Word Application object:
    Private WithEvents mobjWORD As Word.Application
    

     

    2.     In the "ThisDocument" module, add the following event procedure to tidy up when the document is closed:

    Private Sub Document_Close()
    
      '  This is the Document_Close event procedure.
      '  This procedure runs whenever this document
      '  is closed.
      
      Set gobjEH = Nothing
    
    End Sub
    
    
    

     

    3.     Apologies for the lack of indentation in all the code samples.  I have tried editing my previous post to improve font size and indentation, but there is no control over these, especially when using the Insert Code Block button.  I suggest you use the usual VBA indentation when you copy the samples.

     

    Sunday, March 13, 2011 8:11 AM
  • Hi;

    Correct me if I'm wrong, what I think you are doing here (and it is quite clever) is:

    1. Creating a hyperlink that goes to itself by linking to a bookmark where the bookmark is the hyperlink text.

    2. On a selection change event, seeing if the selection moved to the link.

    My only concern here is if they move to the link using the cursor keys instead of the mouse. Is there any way to test for that?

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, March 24, 2011 5:59 PM
  • Hi Dave,

    Would a field link link work for you? Insert > Quick Parts > Field

    In the Field dialog use the Field Names list find the name called MacroButton. Selecting it brings up the available macros including those in your attached template. Enter the text to display.

    After you've got that set, format the displayed text to your liking. The user clicking on it executes the assigned macro.

    Hope this helps

     


    Regards
    Thursday, March 24, 2011 8:10 PM
  • David,

    Yes, you are right.  Whether you choose to create the bookmark so that it spans all of the text of the hyperlink, or whether you create the bookmark so that it is at the insertion point at, say, the beginning of the hyperlink, is a matter that doesn't affect the general purpose of the solution. 

    The idea is that, when the user clicks the hyperlink, it jumps to the bookmark, which also happens to be at the hyperlink location.  After that, the code checks to see if the bookmark is selected: if it is selected, then your code needs to fire. 

    The point is, of course, that the user can make a selection anywhere in the document and that will fire the WindowSelectionChange event and you don't want your code to fire if the bookmark at the hyperlink is not selected.

    I assume from your reply that, if the user selects the bookmark range using the keyboard, then you would not want your code to fire.  Sorry I don't have time today to investigate this, but I shall take a look as soon as possible, probably this weekend.  

    My initial reaction would be to seek a solution whereby a Boolean flag is set to TRUE (if possible) whenever the hyperlink is clicked, so that the code could then check whether the bookmark range is selected AND if the flag is TRUE.  (The code would then have to set the flag to FALSE, of course.)  However, off the top of my head, I don't know if it's possible to set the flag in the way I've just suggested.  That doesn't mean there might not be another solution.  There usually is!

    Also, I think I've created a new event class that has a Document property, so you pass in a reference to the document that has the hyperlink — just in case the user has more than one document open and just in case the other document has a similar hyperlink/bookmark pair.  I shall look into this too and let you have revised code after the weekend. 

     

    Rich,

    My first reaction was to create a macrobutton, as you suggest, but David's original requirement was for a link that highlights when the mouse if moves over it.  I think that's why I went down the route I did.

     

    A General Observation

    As an Access programmer/developer, I'm astonished at how few events the Word object model supports.

     

    Regards
    Geoff

     

    • Marked as answer by DavidThi808 Tuesday, May 3, 2011 3:09 PM
    Friday, March 25, 2011 11:37 AM
  • Hi Rich;

    This could be great. Is there a way to have it call a method in my AddIn instead of a macro? I have no macros or templates, just the AddIn.

    thanks - dave


    Very funny video - What's your Metaphor?
    Friday, March 25, 2011 3:21 PM
  • A General Observation

    As an Access programmer/developer, I'm astonished at how few events the Word object model supports.

    YES! Understatement of the century. There's incredibly basic events missing from the object model.

    thanks - dave


    Very funny video - What's your Metaphor?
    Friday, March 25, 2011 3:23 PM
  • Hi Dave,

    I didn't see anything said about your add-in other than you weren't using VSTO, so I made an assumption because this is a document level customization and VBA was being discussed that either a template was involved or document level macros.

    Sorry


    Regards
    Friday, March 25, 2011 7:57 PM
  • Absolutely no need to apologize. Your solution has me almost there - I just need to figure out how to get an event calling a macro to call a method in my AddIn.

    thanks - dave


    Very funny video - What's your Metaphor?
    Friday, March 25, 2011 8:08 PM
  • Hi Dave,

    Maybe this link can help.

    http://msdn.microsoft.com/en-us/library/bb608621.aspx

    What are you writing your add-in with?


    Regards
    • Marked as answer by DavidThi808 Tuesday, May 3, 2011 3:09 PM
    Friday, March 25, 2011 8:40 PM
  • Rich - I think that may be the last piece of the puzzle.

    thanks - dave

    ps - Code is in C# and not VSTO.


    Very funny video - What's your Metaphor?
    Friday, March 25, 2011 8:49 PM