PDA

View Full Version : Copy table Macro



shickles
07-21-2004, 06:12 AM
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.

Anne Troy
07-21-2004, 07:28 AM
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:

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

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...

smozgur
07-21-2004, 08:17 AM
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:


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

Anne Troy
07-21-2004, 08:25 AM
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.