Microsoft Word - Macro that Prompts User for Number of Tables to Be Copied and Pasted
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 jack about Visual Basic. Can anyone here help me out?
Re: Microsoft Word - Macro that Prompts User for Number of Tables to Be Copied and Pasted
try this.
Code:
Private Sub Document_Open
'declare variables
Dim intNumberOfTables As Integer
Dim MyCount As Integer
'get input from user
intNumberOfTables = InputBox("Please enter the number of tables", "Table Numbers")
'copy current table
'Selection.WholeStory [edit] This is incorrect and would copy the tables that you paste as well, so your table number would be exponential to the number you actually want.
Selection.Tables(1).Select 'this should be ok, you will need the index number of your table though.
Selection.Copy
'repeat paste for number input by user
For MyCount = 1 To intNumberOfTables 'changed from 0 start to 1
Selection.InsertBreak Type:=wdPageBreak
Selection.PasteAndFormat (wdPasteDefault)
Next
'confirm completion
msgbox "Process COmplete",vbOKOnly +vbInformation ,"Complete"
end sub