从Excel VBA到Word文档的居中表格,可以通过以下步骤实现:
Dim wordApp As Object
Dim wordDoc As Object
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True
Set wordDoc = wordApp.Documents.Add
' 假设表格数据在Sheet1中的A1:D10范围内
Dim excelRange As Range
Dim wordTable As Object
Set excelRange = ThisWorkbook.Sheets("Sheet1").Range("A1:D10")
Set wordTable = wordDoc.Tables.Add(wordDoc.Range, excelRange.Rows.Count, excelRange.Columns.Count)
For i = 1 To excelRange.Rows.Count
For j = 1 To excelRange.Columns.Count
wordTable.Cell(i, j).Range.Text = excelRange.Cells(i, j).Value
Next j
Next i
wordTable.Rows.Alignment = wdAlignRowCenter
wordTable.Rows.VerticalAlignment = wdCellAlignVerticalCenter
wordDoc.SaveAs "路径\文件名.docx"
wordDoc.Close
Set wordDoc = Nothing
wordApp.Quit
Set wordApp = Nothing
这样,你就可以从Excel VBA中将表格数据导出到Word文档,并设置表格居中对齐了。
注意:以上代码中的路径\文件名需要根据实际情况进行修改。另外,需要确保你的计算机上已经安装了Microsoft Word应用程序。
领取专属 10元无门槛券
手把手带您无忧上云