• Word ContentControlOnExit with different behavior for tab exit v. mouse click exit

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Word ContentControlOnExit with different behavior for tab exit v. mouse click exit

    Author
    Topic
    #496014

    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]

    Viewing 1 reply thread
    Author
    Replies
    • #1464387

      I would recommend you use Greg Maxey’s method here http://gregmaxey.mvps.org/word_tip_pages/add_table_row_with_content.html

      Failing that, can you test where the cursor is after exiting the last row? eg Selection.Range.Paragraphs(1).Range.Text

      I note that tabbing out of the last CC in the last cell of a table can give mixed results eg if selection is of entire CC you get a different result than if the selection is collapsed. If you can capture the tab keypress then you may need to handle this distinction.

      • #1464413

        Hi Andrew,

        I had thought of using a dialog as a decision point for inserting a new row, but that really breaks up the flow if you have to say “yes” every time you want to add a row. My users will get annoyed. 🙂

        The idea of checking where the cursor is after exiting the last row was interesting, but, unfortunately, I think that the cursor doesn’t move until after the OnExit event completes. And when it does move, it goes to the next ContentControl after the table (regardless of whether a row was added–that’s why I had to include code to move the selection to the first ContentControl in the new row). If a user were to use a mouseclick to leave the last ContentControl in the last row of a table, they would almost certainly be moving to the same field that tabbing would get you to, so the destination wouldn’t be a way of discriminating between the two cases.

        I may have to check into using PeekMessage….I saw that in some forums somewhere.

        Thanks for the tips, though!

        Christian

    • #1464394

      For something similar, see: http://www.msofficeforums.com/word-vba/13955-macro-add-row-table-word-form.html

      However, Word’s VBA object model has no built-in mouse handling functions, so you wouldn’t be able to trap those without API calls. For some code to get you started, see: http://support.microsoft.com/kb/152969/ (and that’s just to get the mouse position)… Capturing mouse clicks is undoubtedly as complex. As for capturing shift-TAB, I imagine you could use the MSForms control Keypress events for that.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1464415

        Hi Paul,

        I think I might have looked at that example back when I wanted to make this a Word protected-mode fillable form–I had buttons that would add an extra row (or delete one) though. In the end, I decided not to use the protected mode since the tab order for the ContentControls go completely wonky when the document is protected. I’m not sure why MS “designed” it that way. 🙂

        One thing I just realized is that I could easily modify this script so that it stops adding extra rows if there is already a “blank” row at the end of the table–that way, tabbing will keep adding new rows until you leave the last row blank, and then it will exit the table instead of adding new rows. It leaves an empty row at the bottom of the table, but that isn’t a huge deal.

        Since this solution will work for me, I am going to mark this thread as “solved”, although not quite in the way that I wanted to solve it.

        Christian

    Viewing 1 reply thread
    Reply To: Word ContentControlOnExit with different behavior for tab exit v. mouse click exit

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: