PDA

View Full Version : Solved: Copy table upon clicking button



will1128
08-30-2010, 12:58 PM
I have a table in a form. When the user presses a button I want the table to continually appear each time the user presses the button.

1. How do I create a table in VBA and fill in the cells?
2. How do I make sure the table is below the previous table?

I'm using the table to help with formatting.
I've attached the Word document in Word 2003.

geekgirlau
08-31-2010, 08:10 PM
A really simple method would be to save your table as an AutoText entry. All your macro needs to do is position the cursor and insert the nominated AutoText.

This also means that if you need to modify the format or content of the table at any point in the future, you don't need to change the code at all.

will1128
09-01-2010, 06:31 AM
I searched AutoText in the VBA Help of MS Word 2003. I see InsertAutoTextMethod and AutoTextEntries Property.

Is it correct to assume I need to assign my table to AutoTextEntries? Not to mention how do I assign a table to an AutoText Entry?

---- Solved this by using Alt + F3 and saved it ot SchTbl as an AutoText...

.... how can a button call InsertAutoText?

Also, what worries me is Tables(i) is done by an index. Each time I click my button I will have Tables(i+1), how will I be able to tell which Tables(i) I'm at?

Thanks!:dunno

will1128
09-01-2010, 08:27 AM
Using the following code I was able to use Insert to Insert an AutoText table:

Public Sub AddScheduleButton_Click()
Set myRange = ActiveDocument.Range(Start:=0, End:=0)
ActiveDocument.Content.Select
ActiveDocument.AttachedTemplate.AutoTextEntries("SchTbl").Insert _
Where:=myRange, RichText:=True
End Sub

Two questions now are:

1. How do I position the cursor after each table?
2. Why do I lose my ComboBox values?

fumei
09-01-2010, 09:45 AM
First, is this only to be used by you, on your computer? If not (it will be used by others on different computers) then you have to be careful about using AutoText. AutoText are generally stored in Normal.dot, thus are local to THAT computer.

Second, positioning the cursor after each table can be done with bookmarks. Put a bookmark after the first instance of the table; insert the new table using that bookmark location, delete the bookmark, put the bookmark after the new table. Thus marking the new location for a new table.

will1128
09-01-2010, 09:59 AM
It's going to be used by others so I was afraid the AutoText may or may not work. Too bad...drawing the table as AutoText is a simple solution. : pray2:

Any other suggestions? Table is pretty complicated. See attachment.

For bookmarks, what if no schedule is created yet and I have no table to use as a bookmark?

I'm new to Word and VBA so I appreciate the help.

fumei
09-01-2010, 11:31 AM
I am confused as to which file, from which thread, I have adjusted, but in any case...from your code:
Private Sub Document_Open()
'populate all the combo boxes
Language.List = Array(" ", "English", "Japanese", "Chinese")
TimeZone.List = Array(" ", "PST", "MST", "CST", "EST")
Contract.List = Array(" ", "T&E", "Warranty", "Comprehensive")
MQFS.List = Array(" ", "Plymouth", "Charlotte", "San Antonio")
End Sub
'populate all the combo boxes

Oh no it does not. It populates nothing.

Regarding inserting new tables, the tables can be done using ranges and bookmarks. To repeat (from another thread?) the comboboxes will NOT be populated.

Public Sub AddScheduleButton_Click()
Dim r As Range
Dim r2 As Range
Set r = ActiveDocument.Bookmarks("Schedule").Range
r.Copy
ActiveDocument.Bookmarks("NewSchedule").Range.Paste
ActiveDocument.Bookmarks("NewSchedule").Select

Set r = Nothing
Set r2 = Selection.Tables(1).Range
With r2
.Collapse 0
.Move Unit:=wdCharacter, Count:=1
.Collapse 0
End With

ActiveDocument.Bookmarks("NewSchedule").Delete
ActiveDocument.Bookmarks.Add Name:="NewSchedule", _
Range:=r2

End Sub


1. declare objects

2. set range object for the Schedule table previously bookmarked as "Schedule". In other words, you have to bookmark first in order for this to work. Obviously for my file I have done this.

3. copy the bookmark (i.e. the Schedule table)

4. paste that at the bookmark "NewSchedule", previously bookmarked. This is at the second paragraph below the first Schedule table.

5. select the "NewSchedule" bookmark. This is at the first character inside the new table.

6. make a range object of the table at the Selection (i.e. the new table)

7. collapse the range, and move it forward so it is now OUTSIDE (after) the new table

8. delete the old "NewSchedule" bookmark (again, at the first character inside the new table)

9. make a NEW "NewSchedule" at the range object (i.e. after the new table)

Thus, clicking the button again will make a new table AFTER the last new table.

fumei
09-01-2010, 11:34 AM
"For bookmarks, what if no schedule is created yet and I have no table to use as a bookmark?"

Are you saying you are going to starting with NO table there at all? Just a button and nothing after it? If so, then this has been a utter waste of my time. In that case you are not "copying a table" now, are you?

will1128
09-01-2010, 01:56 PM
I appreciate your help and it wasn't an utter waste of your time as I was able to learn something from it. Perhaps you've learned something too.

I guess I would be inserting the table if that would be more accurate, but I did manage to find a way to add the same table each time a button is added.

Thank you.

geekgirlau
09-01-2010, 03:47 PM
You can still use the AutoText option provided all users have access to the template in which they're stored. Create the AutoText entries in a new template, and store that template in the Workgroup Templates folder (Tools | Options | File Locations).