简体   繁体   English

Excel VBA Word 文档表格中的内容居中

[英]Excel VBA Centering contents in a Word Document table

I am creating a macro in Excel that inserts a table in Word at a specific bookmark.我正在 Excel 中创建一个宏,该宏在 Word 中的特定书签处插入一个表格。 The table insertion process and the process that updates the table is working like I expect it to.表插入过程和更新表的过程就像我期望的那样工作。

Now, I'm trying to align the data in the table.现在,我正在尝试对齐表中的数据。 I'm using the following code to align the data in the table cells:我正在使用以下代码来对齐表格单元格中的数据:

objTbl.Cell(i, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
objTbl.Cell(i, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight

This works great and is exactly how I want the data to appear.这很好用,这正是我希望数据出现的方式。

The only issue I'm having is the data appears at the top of each cell and I would like it in the middle of each cell.我遇到的唯一问题是数据出现在每个单元格的顶部,我希望它出现在每个单元格的中间。 Is this possible?这可能吗? I tried to adjust the row height with the following code:我尝试使用以下代码调整行高:

WrdApp.ActiveDocument.Tables(1).Rows.Height = 15

I've tried both 15 and 20 and it didn't make a difference. 15和20我都试过了,没啥区别。 My VBA experience has been overwhelmingly within Excel.我的 VBA 经验绝大多数都在 Excel 中。 So this is a bit of a learning curve to program Word from Excel.所以这是一个从 Excel 编程 Word 的学习曲线。 Any suggestions would be appreciated.任何建议,将不胜感激。 Thanks in advance for your help.......在此先感谢您的帮助.......

Perhaps you should spend part of your 60 minutes of research in recording a macro.也许你应该花 60 分钟研究的一部分来录制宏。 Whilst you wouldn't want to use the recorded code it will point you to the objects and properties you need to use.虽然您不想使用记录的代码,但它会指向您需要使用的对象和属性。 Of course you do need to know how to use the UI to achieve what you want first.当然,您首先需要知道如何使用 UI 来实现您想要的。

Sub CellAlignment()
'
' CellAligment Macro
'
'
   Selection.SelectCell
   Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
   Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
End Sub

Having recorded the code you can then use the online help or the object explorer to get a pointer to more usable code, eg记录代码后,您可以使用在线帮助或 object 资源管理器获取指向更多可用代码的指针,例如

ActiveDocument.Tables(1).Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM