Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if a user clicks in a table or certain location in the doc.

Status
Not open for further replies.

easyit

Programmer
Aug 22, 2003
443
NL
Hi,

Does anybody know if it is possible to determine if a user clicks in a table or certain location in a Word document? (Word2007)
If a cursor entry is determined a form should popup.

Maarten

EasyIT

"Do you think that’s air you're breathing?
 
It is not possible to do any check by the click event itself - as there is no click event exposed to VBA. In other words, if you click at location A there is no event that identifies location A.

On the other hand you CAN - using you own procedure - identify if the Selection in anywhere you want. In other words, you can have an event (that you write and more importantly that YOU must execute somehow - a button, a hotkey or a subroutine from some other procedure) that tests the location.

Locations that have specific locations (even if dynamic) like bookmarks are easy. Tables are trickier because of the nature of tables in Word. If you know the sequence number - eg. the third table in the document - then you use that. If you have bookmarked the tables and thus given them a NAME, identifying tables becomes very easy. Examples:

Code:
Option Explicit

Sub CheckIt_Bookmark()
If Selection.Start > ActiveDocument.Bookmarks("Yadda").Range.Start And _
    Selection.Start < ActiveDocument.Bookmarks("Yadda").Range.End Then
        MsgBox "Selection is in the bookmark Yadda."
Else
    MsgBox "Selection is NOT in the bookmark Yadda."
End If
End Sub


Sub CheckIt_Table()
Dim TableNum As Long
TableNum = InputBox( _
    "Type in table number (in order in the document), and press Enter")
On Error GoTo Blah:
If Selection.Start > ActiveDocument.Tables(TableNum).Range.Start And _
    Selection.Start < ActiveDocument.Tables(TableNum).Range.End Then
        MsgBox "Selection is in the third table."
Else
    MsgBox "Selection is NOT in the third table."
End If
Blah:
If Err = 5941 Then
    MsgBox "Identified table does not exist."
End If
End Sub

Sub CheckIt_Table2()
Dim TableName As String
TableName = InputBox( _
    "Type in table Name and press Enter")
On Error GoTo Blah:
If Selection.Start > ActiveDocument.Bookmarks(TableName).Range.Tables(1).Range.Start And _
    Selection.Start < ActiveDocument.Bookmarks(TableName).Range.Tables(1).Range.End Then
        MsgBox "Selection is in table " & TableName & "."
Else
    MsgBox "Selection is NOT in table " & TableName & "."
End If
Blah:
If Err = 5941 Then
    MsgBox "Identified table does not exist."
End If
End Sub

If you are ever going to be doing serious VBA actions on tables in a Word document I strongly recommend making them name-able objects via bookmarks. That way you can move them, expand them, change them, access data from them, all by NAME. No matter where they are, you can perform actions on them directly.

 


Gerry!

Come up for breath out of the wilderness?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip & fumei,

Thank you both for your response. I'm not sure if I can work things out the way I want but this certainly points me in the direction.

Regards,

Maarten




EasyIT

"Do you think that’s air you're breathing?
 
A good place to start is to precisely - and I mean precisely - determine what you want to happen (and when...and how).

" if I can work things out the way I want"

What exactly do you want? If what you want is to have something happen (automatically) whenever the user clicks into table X (but Not happen if they click into table Y), then sorry this is not going to happen.

 
...yes, you're correct. What I want exactly is that when a user clicks in a specific table (anywhere within) that a form is triggered.

So I need the mouse-click event which seems not possible.

" if I can work things out the way I want" is the result of your responses and the urgency of this possibility. My client can live without it, stays still my own curiosity (which is patient...).

regards,

Maarten






EasyIT

"Do you think that’s air you're breathing?
 
As defined - an automatic execution of something i.e. your form - whenever a user clicks in a specific table, is not possible.

There is no click event to trigger this. You can execute something that will test and open your form, but not automatically.

Hi Skip. I am just dropping by. I am not really in the wilderness, simply off-line.

 

The event you need to use is the Application WindowSelectionChange Event, which will fire on any explicit cursor movement (but not on implicit ones such as when typing). It does have one or two peripheral effects but it will give you what you want.


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
True...except it would fire everytime there was a SelectionChange. Click and select a different word...it fires. Selection the next word...it fires. And remember it does NOT fire with a change in the cursor location. It fires with a Selection change.

 
Hi Gerry,

Glad to see you're still circulating!!

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

circulating in, circulating out. It is rather hit and miss. I am not online regularly.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top