Consulting

Results 1 to 8 of 8

Thread: use command button to copy a table

  1. #1
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    4
    Location

    use command button to copy a table

    I have no VBA experience in word, although I have done some Visual Basic programming for windows.

    I have a small table in a word doc which contains some pull-down lists and other fields for data entry and some headings.
    There is an activeX button below the table labeled New Item.
    When this button is pressed I want to add another table with no data entered, and have it added 2lines below the previous table and the button move to two lines below the new table.
    So, I want it inserted before the button but after the previous table.
    We do not know how many tables will be in the final doc, it is a live document for providing design feedback so there may be for example three items of feedback or there may be 30 items. each item uses a single copy of the table.

    secondary to that, I do not want the tables to split over page breaks, unless the table is larger than a full page but that would never be the case.

    I can upload the doc if needed, please advise.

    Any help with the code for doing this will be much appreciated.

    Brendon Parise

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Without knowing whether your document uses formfields or content controls, and what protection, if any, it might use, it's impossible to code a solution. Accordingly, you should attach a copy of your document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    4
    Location
    Quote Originally Posted by macropod View Post
    Without knowing whether your document uses formfields or content controls, and what protection, if any, it might use, it's impossible to code a solution. Accordingly, you should attach a copy of your document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
    Thanks very much for your reply, this is my first VBA and my first post so thanks for letting me know how to proceed.

    The doc is a simple word doc, with tables and content controls.
    if a form fields method is better then I would appreciate knowing that, but I have never done one of those and the 'form' would need to be expandable because of the added tables that may or may not be needed.

    I expect this process will take a couple of posts so thank you for your feedback in advance.
    The doc is attached.

    It is in early stages so formatting is not final but the content of the 'tables' is there.
    As mentioned, it will be used for peer review of CAD designs, so there may be no items or as many as 50 items per category, no way of knowing in advance.
    the categories I mention are mechanical, placement, constraints, routing and documentation. Each will have it's own 'add item' button after the last item table in each section.

    Hope it makes sense.

    Brendon
    Attached Files Attached Files

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You can get the results you want, without the need for the buttons, using a ContentControlOnExit macro in the 'ThisDocument' code module of the document or its template coded like:
    Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
    'The following code conditionally adds a new table, with initialized content controls, following the current table.
    Dim i As Long, j As Long, t As Long, Prot As Variant
    Const Pwd As String = "" 'Insert password (if any) here
    If CCtrl.Range.Information(wdWithInTable) = False Then Exit Sub
    With CCtrl
      i = .Range.Tables(1).Range.ContentControls.Count
      'Get our ContentControl's index # in the table
      j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count
      'Check that we're using the last content control
      If i <> j Then Exit Sub
      'Solicit user input
      If MsgBox("Add new table?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub
      With ActiveDocument
        ' Un-protect the document, if applicable
        Prot = .ProtectionType
        If .ProtectionType <> wdNoProtection Then
          Prot = .ProtectionType
          .Unprotect Password:=Pwd
        End If
      End With
      t = ActiveDocument.Range(0, .Range.Start).Tables.Count + 1
      With .Range.Tables(1).Rows.Last.Range
          .Characters.Last.Next.InsertBefore vbCr & vbCr
          .Characters.Last.Next.Next.FormattedText = .Tables(1).Range.FormattedText
      End With
      Call InitializeCCtrls(t)
      With ActiveDocument
        ' Re-protect the document, if applicable
        .Protect Type:=Prot, Password:=Pwd
      End With
    End With
    End Sub
    
    Sub InitializeCCtrls(t As Long)
    Dim CCtrl As ContentControl
    'Reset all content controls in the designated table
    With ActiveDocument.Tables(t).Range
        For Each CCtrl In .ContentControls
          With CCtrl
            Select Case .Type
              Case wdContentControlCheckBox: .Checked = False
              Case wdContentControlRichText, wdContentControlText, wdContentControlDate: .Range.Text = ""
            Case wdContentControlDropdownList
              .Type = wdContentControlText
              .Range.Text = ""
              .Type = wdContentControlDropdownList
            Case wdContentControlComboBox
              .Type = wdContentControlText
              .Range.Text = ""
              .Type = wdContentControlComboBox
            End Select
          End With
        Next
    End With
    End Sub
    This macro will prompt to add a new table anytime you exit the last content control in any table (assuming the table has at least one content control). If you give those content controls a title, you can test that so as to avoid the prompt etc. when exiting a table you never want to replicate.

    Preventing a table from breaking across page breaks is a matter of using 'keep with next' paragraph formatting in all rows except the last.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    4
    Location
    Paul, All,
    Firstly Paul, thank you for your very quick response and thorough code, much appreciated.
    However, I struggle to understand the navigation of controls and tables in word VBA, as mentioned I am new to this and will purchase a good office VBA book and learn it as I have a lot more of this to do. Do you have any recommendations for which book to get, using office 2010 and forward.
    That said, I am under pressure to have this doc ready to go ASAP.
    I have uploaded a current version of the doc for you to look at, I have not done the coding yet since I do not fully understand it.

    If you could show me code for the following then that would give me the kickoff I need by studying your code and understanding it, so I can then go ahead and create about another half dozen docs like this, this one being the simplest, I would appreciate the help.

    - the document is not protected so that code is not needed.
    - you will see in the Mechanical review section a second table with all cells highlighted yellow. Between the first table and the second one there is a single carriage return of text style Normal.
    between the second table and the New Mechanical Item button there is a single carriage return of text style No Spacing.
    - the yellow table is a copy of the original table above it, with the content controls copied as well, so the list box contents are copied.
    - it would be better to do that in code since copying the previous table would be copying a non blank table.
    - I guess it would be easy to copy the table with reinitialized content controls so I would need to know how to populate the list box controls in code.
    - all cells in the table are justified center in the Y axis, some are left in X axis and some are center in X axis, so I would need to know how to set that in code when creating the new table.
    - placing the new table would mean placing a carriage return of style normal immediately after the previous table, and then placing the new table, pushing the no spacing line and the
    New Mechanical Item button down.
    - a new table should not be split across pages, so if adding the new table does that then it would need to insert a page break at the end of the previous table.
    ( a completed item table may get longer than a page but that is ok.)
    ( if a table does not start on a new page, and then grows such that it crosses to the next page, then it would be nice if it automatically reformatted by placing a page break immediately before the table so it starts on a new page, or if a table has done that and then shrinks such that it would have fit on the previous page, then remove the page break before it - kind of dynamic formatting ) maybe figure out that one later.
    - the tables for each section are slightly different in that the Select Item list box content control has different entries in the list, so there is very slightly different code for each 'button' if you like.

    If you could show me how to add this new reinitialized table in the mechanical section then I can go ahead and do the others, I hope.

    one other thing, I have been asked whether it is possible to change the cell color for the very top Status item on the top right of the first page.
    Basically if there is a pending review item anywhere in the doc then the status is set to Open and the cell is highlighted RED, if there are no pending items then the status is Closed and the cell is highlighted GREEN. We do not want this to be automated in terms of looking for pending items, only a reviewer can set the status of the review manually, but we would like the cell color to change to reflect the status.
    Attached Files Attached Files

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Perhaps you should have started by adding the code to your original document - from which you could delete the buttons as they're not needed - and seeing how that works. I've created a template and added the code for the shading. See attached.
    Attached Files Attached Files
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    4
    Location
    OK Paul, thanks very much, it works well.
    Now I just need to go through the code and figure out why it works so I can use the same techniques as I proceed.

    Thanks very much for your help.
    I will need to add the button to initiate adding the tables because the table is filled out in the review process and the reviewer does not know if a new table is needed until there is a new item to document.
    reviewers also need to go back and forth in the doc as they work and having it prompt to add a table if the status of an item is changed will end up being annoying.
    I think it is better for the reviewer to explicitly add a table when it is needed.

    it would also show me how to identify a table to copy by knowing which button was pressed, and respond to the button which I will need in some of the other docs I need to do.

    Thanks for your help, I will study the code, learn how it works and move ahead from there, I have a lot more of this to do.

    your assistance is much appreciated.

    Brendon

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by bparise View Post
    I will need to add the button to initiate adding the tables because the table is filled out in the review process and the reviewer does not know if a new table is needed until there is a new item to document.
    reviewers also need to go back and forth in the doc as they work and having it prompt to add a table if the status of an item is changed will end up being annoying.
    I think it is better for the reviewer to explicitly add a table when it is needed.
    With the way the macro is coded, the user is offered the option to add a new table whenever the last "Select Status" content control for any of the "Mechanical Review", "Placement Review", "Constraints Review", "Routing Review", and "Documentation and Wrap up Review" groups is exited. It doesn't matter whether they've previously opted not to do so. I could have coded it so that the same option would be offered any time any of the "Select Status" content controls for any of those groups is exited (which would allow new tables to be inserted between any existing ones in a group), but I thought you wouldn't want that.

    The code can't be adapted to use with buttons without a fair bit of re-working.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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