PDA

View Full Version : VBA, MS Word Macro - Prompt # Tables, Copy + Paste # of Tables



peerapon
03-23-2005, 08:01 AM
I have a Microsoft Word document that contains a table with form fields.

I would like it set up so that when the user first opens up the document, Microsoft Word prompts the user "how many additional tables" they want in addition to the one that already exists in the document.

The macro would then select and copy, then paste the table "that many times" underneath.

Unfortunately I don't know much about Visual Basic. On the bright side, I do know how to make macros in Word. Can anyone here help me out?

Killian
03-23-2005, 09:05 AM
In the Word VBEditor (Alt+F11), find your way to the document_open event and insert this code Dim Response As Integer, i As Integer

Response = InputBox("Prompt text here", "How many extra tables?")

If Response > 0 Then
ThisDocument.Tables(1).Select
Selection.Copy
For i = 1 To Response
Selection.EndKey Unit:=wdStory
If Selection.Next(wdCharacter, -1) <> Chr(13) Then Selection.TypeParagraph
Selection.Paste
Next
End If you should get something close to the result you're after

sandam
03-23-2005, 09:19 AM
And if your document is a template then you can place the code in the AutoNew event procedure which would mean that the user isn't prompted everytime to insert tables, just when they use the doc for the first time, unless this is what you want to happen??



Andrew ;?