Results 1 to 5 of 5

Thread: Toggling - Can You Do It?

Hybrid View

  1. #1
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Toggling - Can You Do It?

    OK, there is a way to do it. You need to paste the following into the General Declarations bit of your VBA editor:

    Code:
    Public WithEvents appWord As Word.Application
    
    Private Sub Document_Open()
    
    Set appWord = Application.Application
    
    End Sub
    
    Private Sub appWord_WindowBeforeDoubleClick(ByVal Sel As Selection, Cancel As Boolean)
    
    If Sel.Words(1) = "(+) " Then
        MsgBox "Selected (+) "
    End If
    
    End Sub

    What are we doing here?

    We are declaring appWord as a Word Application, and using WithEvents to access all the events of that Word application, which are not normally visible through code. When the document opens, we are setting the variable appWord to be the current Word application. Then, we are trapping the BeforeDoubleClick event to intercept the double-clicking and, if the currently selected word is "(+) " then we do some action. Note that we use the Sel selection that is passed, but beware that the double click actually changes the selection to a single insertion point during the double click, and then puts it back afterwards. Hence we can't check to see what the full current selection is, because it is just a character. We need to check the word that the insertion point is in, and if it is the desired one then we do something.


    Drawbacks: The variable appWord is only set when you open the document. If you start interfering with the VBA code by stopping it, you will reset the value of this variable to Null, and hence things will stop working until you re-open the file. You may wish to stick a button in temporarily to reassign this value (i.e. to click it whenever you tinker with the code, so as to get it going again). However, I have included it in this format so that you can see how it works.

    Paste this in, try to figure out how it works, and then post back with more questions.


    zaza
    Last edited by zaza; Jun 11th, 2007 at 04:31 PM.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

Posting Permissions

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



Click Here to Expand Forum to Full Width