Hello all–
I’m working with a Word form that includes tables with text-type ContentControls in them. I would like to set it up so that if the user exits the last ContentControl in the last row of a table by pressing the TAB key (and only by pressing the TAB key), it adds another row to the table and replicates the content controls in that row (without data inserted).
I’ve been able to get everything to work except for the trigger–right now, it triggers when the user exits the ContentControl by any method, including shift-TAB and mouse-clicking into another ContentControl. Is there any way to figure out what the last keypress was in Word? Or perhaps a way to see if a mouseclick occurred in the last 10 milliseconds? I might be able to use such occurrences to discriminate between different ways the Exit event gets triggered….
My code is below….
Thanks,
Christian
[INDENT]Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim tbl As Table
Dim cel As Cell
Dim rng As Range
Dim cc As ContentControl
‘Exit if contentcontrol is not in table.
If Not ContentControl.Range.Information(wdWithInTable) Then Exit Sub
Set tbl = ContentControl.Range.Tables(1)
Set rng = ContentControl.Range
‘I’ve flagged my ContentControls that are in the last row of a table as “LastCol”; this
‘checks for such a ContentControl.
If ContentControl.Tag = “LastCol” Then
‘Check if current row is last row of table; only add row if so.
If rng.Cells(1).RowIndex = tbl.Rows.Count Then
‘Add row to end of table.
tbl.Rows.Add
‘Go through each cell in current ContentControl’s table row and copy contents into
‘corresponding cell in newly added row.
For Each cel In rng.Rows(1).Cells
cel.Range.Copy
tbl.Cell(tbl.Rows.Count, cel.ColumnIndex).Range.Paste
Next cel
‘Go through each pasted ContentControl in new row and clear out data (for text-type
‘ContentControls).
For Each cc In tbl.Rows(tbl.Rows.Count).Range.ContentControls
If cc.Type = wdContentControlText Then
cc.Range.Text = “”
End If
Next cc
‘Move selection to first ContentControl in newly-added row.
tbl.Rows(tbl.Rows.Count).Range.ContentControls(1).Range.Select
End If
End If
End Sub[/INDENT]