• Replicating Table with Control Button Code (VBA/Wo

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Replicating Table with Control Button Code (VBA/Wo

    Author
    Topic
    #433858

    I have created a table that enables the user to click on a command button, select a picture, and the VBA code resizes the file to fit the table cell. The table allows the user to insert six (6) pictures. The VBA code for the command buttons points to a common procedure.

    What I want the user to be able to do is insert a new page, copy and paste the table on the new page and have the command button code also replicated.

    Is there a way to accomplish this with Cut-and-Paste operations? Or do I have to rebuild the table each time the user wants a new page of pictures (ugh!)?

    Thanks,

    Viewing 0 reply threads
    Author
    Replies
    • #1021516

      Copying and pasting a table is no problem, but copying a control and pasting it may disconnect it from your code. Have you tried it manually to see whether the CommandButton will still work?

      To copy the table, you can use either the Selection object (if the insertion point is in the table) or the document’s Tables collection. Common methods for identifying a table are:

      Selection.Tables(1) ‘ first table in selection
      ActiveDocument.Content.Tables(ActiveDocument.Content.Tables.Count) ‘ last table in body

      (Note: I typed that in, not testing for any spelling error or objects/properties missing in the chain.)

      • #1021528

        Many thanks for your reply.

        >> Have you tried it manually to see whether the CommandButton will still work?
        No, the CommandButton doesn’t work because when Word performs the paste operation, it assigns new numbers to the pasted CommandButtons and doesn’t replicate the CommandButton VBA code. So,

        Private Sub CommandButton11_Click()

        End Sub

        when pasted becomes

        Private Sub CommandButton21_Click()

        End Sub

        If someone could recommend an elegant procedure that would replicate the table, CommandButtons, and CommandButton code, that’d be great. Otherwise, I’ll just create a Word template with enough tables for the specific purpose.

        Thanks,

        • #1021669

          Do you really need a seperate Commandbutton for each tablecell? If not, you can create a single commandbarbutton (on a standard or custom commandbar) that checks if the cursor is within a table and if so, inserts a picture at the cursor position:

          If Selection.Information(wdWithinTable) Then
          ‘ your code to insert a picture
          End If

          • #1021743

            What a great idea! cheers

            Thanks for thinking outside my box. I’ll do it.

          • #1023376

            Jan,

            This worked so well that I also incorporated automatic generation of the desired table with the cursor left in cell 1.

            Thanks again,

    Viewing 0 reply threads
    Reply To: Replicating Table with Control Button Code (VBA/Wo

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

    Your information: