Consulting

Results 1 to 4 of 4

Thread: Copy table Macro

  1. #1
    VBAX Regular
    Joined
    Jul 2004
    Posts
    10
    Location

    Copy table Macro

    I am trying to insert a button in word that will copy a table and number it. I have attached a picture of the word document. I need to be able to add a 3rd or 4th goal. Any help would be great.

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    For the goal numbers, you need to insert an autonumber field.

    Here's how I would do this, but I'm not a coder. That's why I created this website, hee hee...

    I would create my table for Goal 1, including an autonumber field for the goal #.
    I would select it, and hit Insert-Autotext, New. Give it a name like insgoal.

    Then, use this macro to insert your autotext entry:

    [vba]Sub InsGoal()

    'Replace the Word "password" with your password
    ActiveDocument.Unprotect Password
    Selection.TypeText Text:="insgoal"
    Selection.Range.InsertAutoText
    ActiveDocument.Protect 2, True,Password

    End Sub[/vba]

    You may also want a macro that inserts your signature block in a textbox at the very end of the last page of the document...

    Please save your file to a new name before trying any solutions...
    ~Anne Troy

  3. #3
    BoardCoder VBAX Regular
    Joined
    May 2004
    Location
    Istanbul, Turkiye
    Posts
    73
    Great method, Dreamboat! Both AutoText and AutoNumber (Why I cannot think this fast and correct?? I was almost trying to create the same table in code! yes, I admit!)

    A small addition if we need to insert this new table (new goal) between last goal table and the last table (if the last frame is a table). We might need a bookmark to add the new goal table row.

    Actually there are lots of different method for this but I started with tables so attached is my approach (same with Dreamboat's)

    Basically I created a bookmark after first existing table (Goal 1) and used it to locate the selection before inserting the new one. Bookmark name is "goalhere" and password is "test" for the attached template. My addition is only the bookmark as locator.

    I hope it helps.
    Suat

    Edit : Modified code is:

    [vba]
    Sub InsertGoal()
    Dim strPwd As String
    strPwd = "test"
    ActiveDocument.Unprotect strPwd
    With Selection
    .GoTo What:=wdGoToBookmark, Name:="goalhere"
    .MoveLeft Unit:=wdCharacter, Count:=1
    .TypeText Text:="insgoal"
    .Range.InsertAutoText
    End With
    ActiveDocument.Protect wdAllowOnlyFormFields, True, strPwd
    End Sub
    [/vba]

  4. #4
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    I tested it on Word 2000, and it's terrific. Let us know how it works for you, shickles.

    Another note:

    Autotext can only be stored in templates. By default, an autotext entry is stored in normal.dot. Once you create it, you must use the Organizer to move it to YOUR template. One method to get to the organizer: Tools-Macro-Macros, click the Organizer button, and then click the Autotext tab.
    ~Anne Troy

Posting Permissions

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