开发者

Copy Table macro for Microsoft word 2007

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 b开发者_如何学Ce 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜