vba Microsoft Word 2007 的复制表宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3488671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy Table macro for Microsoft word 2007
提问by Shilpa Silk
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.
我在 MS Office word 2007 文档中有一个简单的要求。我需要在宏背后的代码,它复制一个表(格式化的)并在我每次运行这个宏时粘贴它。
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. 我将复制一个格式化的表格(7-8 行和 5-6 列等)并将其作为按钮或快捷键存储在宏中。
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.
1.无论何时或在同一个word文档中的任何特定位置,我都会放置光标并单击宏按钮(运行我们的宏)。此宏应粘贴具有相同行数和列数以及样式的相同格式的表格。
2.I can run this macro several times but it should paste the same table everytime.
2.我可以多次运行这个宏,但每次都应该粘贴同一个表。
I hope code will be in VB.
我希望代码将在 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.
我知道如何创建宏、分配按钮、快捷键、安全性等。我只需要宏后面的 VB 代码(或任何代码),这可能是上述情况的解决方案。
Sorry for long post but I have made my requirement pretty clear.
很抱歉长篇幅,但我已经清楚地表达了我的要求。
Thanks in Advance... Cheers! Shilpa Silk
提前致谢...干杯!希尔帕丝绸
回答by Majid Fouladpour
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:
在开始录制之前,应满足以下条件:
- Your table should be at the beginning of the document after a paragraph mark
- Your cursor should be where you want to place the new table
- 您的表格应位于文档开头的段落标记之后
- 您的光标应该是您要放置新表的位置
Then turn recording on and complete the following steps:
然后打开录音并完成以下步骤:
- Type
_table_goes_here_
where the cursor is - Press Ctrl + Home to go to the beginning of the document (just before the main table
- Hold down
Shift
and pressdown arrow
key enough times until the whole table is selected, - Press Ctrl + C to copy the table
- Press Ctrl + F to bring up the Find dialog
- Type the placeholder text into Find what box (
_table_goes_here_
) and click Find next - When you have your placeholder text found and selected, press Esc key to dismiss the find dialog
- Press Ctrl+V to paste the copied table which will replace your placeholder text
- End macro recording.
- 键入
_table_goes_here_
光标所在 - 按 Ctrl + Home 转到文档的开头(就在主表之前
- 按住
Shift
,然后按down arrow
,直到选中整个表格关键足够的时间, - 按 Ctrl + C 复制表格
- 按 Ctrl + F 调出查找对话框
- 在“查找内容”框 (
_table_goes_here_
) 中键入占位符文本,然后单击“查找下一个” - 找到并选择占位符文本后,按 Esc 键关闭查找对话框
- 按 Ctrl+V 粘贴复制的表格,该表格将替换您的占位符文本
- 结束宏录制。
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 t16t/08t/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