简体   繁体   中英

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. 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. My VBA experience has been overwhelmingly within Excel. So this is a bit of a learning curve to program Word from Excel. 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. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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