Detecting user action

B

Bo Hansson

How do I detect the event when a user puts the insertion point into a
specific table in a document?

/BosseH
 
J

Jay Freedman

Bo Hansson said:
How do I detect the event when a user puts the insertion point into a
specific table in a document?

/BosseH

Hi Bo,

You need an application event handler class. This isn't really as
complicated as it sounds. For the basics, see
http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm.

For this specific job, follow the instructions in the AppClassEvents
page for creating a global template and setting up the module and
class module. For step 6, use the following code in the
ThisApplication class module:

Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
If Sel.Information(wdWithInTable) Then
If ActiveDocument.Tables.Count > 1 Then
If Sel.Range.InRange(ActiveDocument.Tables(2).Range) Then
StatusBar = "In table 2"
End If
End If
End If
End Sub

Change the "ActiveDocument.Tables(2).Range" bit to detect the specific
table you have in mind. Replace the StatusBar statement with whatever
action you want to execute when the selection is in that table.

Be aware that this handler will execute every time the selection
moves, including when it moves from one place in the table to another
place in the same table. If your action should be executed only once,
on entering the table from outside, then you have to supply the
necessary logic to suppress the action when it isn't wanted.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Bo Hansson > écrivait :
In this message, < Bo Hansson > wrote:

|| How do I detect the event when a user puts the insertion point into a
|| specific table in a document?
||

Jay's answeer was excellent, but I tought I'd warn you about a potential
problem.

Be careful about using the table index in the code. If the user adds/removes
tables in the document, then this index will change.

Or did I miss something?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jay Freedman

Jean-Guy Marcil said:
Bonjour,

Dans son message, < Bo Hansson > écrivait :
In this message, < Bo Hansson > wrote:

|| How do I detect the event when a user puts the insertion point into a
|| specific table in a document?
||

Jay's answeer was excellent, but I tought I'd warn you about a potential
problem.

Be careful about using the table index in the code. If the user adds/removes
tables in the document, then this index will change.

Or did I miss something?

Jean-Guy, you're absolutely correct. That's what I meant when I said
'Change the "ActiveDocument.Tables(2).Range" bit to detect the
specific table you have in mind.' It's good to emphasize the point,
though. Since I don't know exactly how Bo defines 'a specific table',
I can't suggest a better way of determining whether the table
containing the selection is the 'right' one.
 
H

Howard Kaikow

One way might be to assign the table range to an object, then test for that
range instead of the table index.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Jay Freedman > écrivait :
In this message said:
Jean-Guy, you're absolutely correct. That's what I meant when I said
'Change the "ActiveDocument.Tables(2).Range" bit to detect the
specific table you have in mind.' It's good to emphasize the point,
though. Since I don't know exactly how Bo defines 'a specific table',
I can't suggest a better way of determining whether the table
containing the selection is the 'right' one.

Bookmarking the table and checking for to see if the cursor is in the
bookmark range?
Would that work?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
B

Bo Hansson

Thanks for the warning,

However, I believe that I have managed to identify the specific tables by
checking unique headings within them.

/BosseH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top