如何在 Word 中居中所有表格,来自 Excel - VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16574145/
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
How to center all tables in Word, coming from Excel - VBA
提问by Luiz
I'm very new to VBA and my first code is to copy excel tables and paste it at word. It's working, but the problem is that all tables are pasted left-aligned and I want it centeraligned. Does anyone knows how to do it? Going to post my code here:
我对 VBA 很陌生,我的第一个代码是复制 excel 表格并将其粘贴到 word 中。它正在工作,但问题是所有表格都是左对齐粘贴的,我希望它居中对齐。有谁知道该怎么做?将在这里发布我的代码:
Sub excel2word()
Dim objWord As New Word.Application
'Copy the range Which you want to paste in a New Word Document
Set objWord = CreateObject("Word.Application")
For i = 2 To 200
With objWord
.Documents.Add
Sheets("Plan2").Select
Range("A" & i).Copy
.Selection.PasteAndFormat (wdFormatPlainText)
.Selection.TypeParagraph
Sheets("Teste").Select
Range(Cells((26 * (i - 1) + 1), 1).Address, Cells(((26 * (i - 1) + 7)), 3).Address).Copy
.Selection.PasteExcelTable True, False, False
.Visible = True
.Selection.TypeParagraph
Sheets("Teste").Select
Range(Cells((26 * (i - 1) + 8), 1).Address, Cells(((26 * (i - 1) + 16)), 3).Address).Copy
.Selection.PasteExcelTable True, False, False
.Visible = True
.Selection.TypeParagraph
Sheets("Teste").Select
Range(Cells((26 * (i - 1) + 17), 1).Address, Cells(((26 * (i - 1) + 25)), 3).Address).Copy
.Selection.PasteExcelTable True, False, False
.Visible = True
.Selection.TypeParagraph
End With
Next
End Sub
As you can see, this code copy paste 3 tables at one interaction, that is 600 tables in total! So, it gets very boring to center one by one.
如您所见,此代码在一次交互中复制粘贴 3 个表,总共 600 个表!所以,一个一个地居中会很无聊。
Any help is very appreciated!
非常感谢任何帮助!
回答by B1nd0
In case anyone still needs it:
如果有人仍然需要它:
ActiveDocument.Tables(1).Rows.Alignment = wdAlignRowCenter
回答by Christina
So, I'm going to try to explain how I would do the first part of this. The first part is outside of VBA. In Word, you can create or modify table styles. Since you say you are using existing styles, you can modify them so that the table is centered. Let's say you are using the Light Shading table style (this is the first Table Style that I see when I expand Table Styles from the ribbon). Do the following:
所以,我将尝试解释我将如何进行第一部分。第一部分在 VBA 之外。在 Word 中,您可以创建或修改表格样式。由于您说您使用的是现有样式,因此您可以修改它们以使表格居中。假设您正在使用 Light Shading 表格样式(这是我从功能区展开表格样式时看到的第一个表格样式)。请执行下列操作:
- In a Word document, create a basic table. You can just click Insert Table from the Insert tab and insert a table with a couple cells.
- Select the table. You should see the Table Tools tabs appear on the ribbon. Click Design.
- You should see some table styles in the center of the ribbon. Find the one you need (expanding the dropdown if necessary), and right-click.
- Select Modify Table Style from the context menu.
- The Modify Style dialog will pop up.
- Click Format and then Table Properties. Under the Table tab in the Table Properties dialog, look for Alignment. Choose Center and click OK. Click OK again to save the change.
- 在 Word 文档中,创建一个基本表格。您只需单击“插入”选项卡中的“插入表格”,然后插入带有几个单元格的表格。
- 选择表。您应该会看到功能区上出现“表格工具”选项卡。单击设计。
- 您应该会在功能区的中心看到一些表格样式。找到您需要的(如有必要,展开下拉列表),然后单击鼠标右键。
- 从上下文菜单中选择修改表格样式。
- 将弹出修改样式对话框。
- 单击格式,然后单击表属性。在“表格属性”对话框的“表格”选项卡下,查找“对齐”。选择中心并单击确定。再次单击“确定”以保存更改。
Now, when you apply that table style to a standard table the table will be centered.
现在,当您将该表格样式应用于标准表格时,表格将居中。
The second part, if necessary, is to apply that style programatically. I don't have time to write that part out right now, but I can edit this later. Perhaps this will be a starting point for you, though.
如有必要,第二部分是以编程方式应用该样式。我现在没有时间把那部分写出来,但我可以稍后再编辑。不过,也许这将是您的起点。