Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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

    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

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,515
    Thanks
    3
    Thanked 143 Times in 136 Posts
    I would recommend you use Greg Maxey's method here http://gregmaxey.mvps.org/word_tip_p...h_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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,899
    Thanks
    0
    Thanked 188 Times in 172 Posts
    For something similar, see: http://www.msofficeforums.com/word-v...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
    [MS MVP - Word]

  5. #4
    New Lounger
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  6. #5
    New Lounger
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

Posting Permissions

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