views:

51

answers:

1

Hi

I have a simple requirement in MS Office word 2007 document. I needed code behind the macro which copies a Table (formatted one) and paste it everytime when I run this macro.

The scenario is as follows:- 1. I will copy a formatted table (with 7-8 rows and 5-6 columns, etc) and store it in a macro as button or shortcut key.

1.Whenever I want or at any particular place in the same word document I will place the cursor and click on macro button (run our macro). This macro should paste the same formatted table with same number of rows and columns and style.

2.I can run this macro several times but it should paste the same table everytime.

I hope code will be in VB.

I know how to create macro, assigning button, shortcut key, security, etc. I need only the VB Code (or any code) behind the macro which could be solution for above scenario.

Sorry for long post but I have made my requirement pretty clear.

Thanks in Advance... Cheers! Shilpa Silk

+2  A: 

Use macro recorder. Invoke the recorder, then complete the steps to copy and paste the table, then you can edit it to see the macro's actual instructions. But note that macro recorder does not save the contents of the clipboard, so the markup that creates the table will not be saved with the macro. To get it work, the table should exist before you run the macro.

Here is one possible method:

Before you start recording the following conditions should be met:

  1. Your table should be at the beginning of the document after a paragraph mark
  2. Your cursor should be where you want to place the new table

Then turn recording on and complete the following steps:

  1. Type _table_goes_here_ where the cursor is
  2. Press Ctrl + Home to go to the beginning of the document (just before the main table
  3. Hold down Shift and press down arrow key enough times until the whole table is selected,
  4. Press Ctrl + C to copy the table
  5. Press Ctrl + F to bring up the Find dialog
  6. Type the placeholder text into Find what box (_table_goes_here_) and click Find next
  7. When you have your placeholder text found and selected, press Esc key to dismiss the find dialog
  8. Press Ctrl+V to paste the copied table which will replace your placeholder text
  9. End macro recording.

Edit - Second approach
Another approach is to start macro recording and then create the table from scratch, that way you will not need a pre-existing table for the macro to work. When you have shaped and formatted the table end recording and you have captured all the required steps to place the exact same table wherever you want.

I just tested the second approach and it works just fine. Here is the code generated by the recorder for my small test:

Sub MakeTable()
'
' MakeTable Macro
' Macro recorded þ16þ/08þ/2010 by Majid Fouladpour
'
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= _
        4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    With Selection.Tables(1)
        .Style = "Table Columns 4"
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    Selection.TypeText Text:="Col one"
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="Col two"
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="Col three"
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="Col four"
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=3
    Selection.TypeText Text:="Item 1"
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.TypeText Text:="Item 2"
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.TypeText Text:="Item 3"
End Sub
Majid
Hi Majid!Thanks for answering the question but my requirement is pretty different. I can't place table in the begining of document rather I will have table in some particular page. Isn't it there a way that macro should remember the table and paste it wherever and whenever I need. Same macro can be used to paste the table in other documents. There (in new document) I can't tell anyone to copy-paste the table in the begining of document. Can't a macro store and save the table and the format in its memory or something like that? I can re-use it anytime in any word document.Thanks for ur help
Shilpa Silk
The macro will paste the table where you place the cursor, not at the beginning of the document. The table at the beginning of the document acts as a template only. You can also place it at the end of the document in which case instead of `Ctrl + Home` you'd need to do a `Ctrl + End` and then move backward to select the table while recording the macro. I do not know any way to save the table in the macro itself, but will edit my answer to add another approach.
Majid
You could store the table as an AutoText entry (or Building Blocks in Word 2007). That way it's saved along with the document, but doesn't appear on the page until you run code to insert it.
e100
@e100: The second approach achieves that goal. The table does not appear on the document until you run the macro.
Majid