我知道以前有人问过这个问题,但我只能找到在您传输数据时在 Word 中创建表格的网站。
我有一个包含 3 个表格的 Word 文档,并希望从客户端发送的 Excel 文件中提取数据到 Word 表格中的某些单元格中。
例如,我想将 Excel 中单元格 D3 的内容放入 Word 第二个表格中的单元格 2,2 中?有没有办法引用哪个表来插入数据?
任何人都可以帮助解决这个问题,或者只是将我推向正确的方向,因为我对宏等比较陌生。当我们使用 Word 文档并将 Excel 文件发送给我们时,在 Word 中制作宏是否更有益?
谢谢
最佳答案
我已经设法修复它,通常它相对简单,但感谢@David Zemens 朝着正确的方向插入。我不知道我以前在“ThisDocument”中搜索时是如何错过“Tables”的。
Sub GetData()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Dim ExcelFileName As String
ExcelFileName = "{Put the directory of Excel file here}"
Set exWb = objExcel.Workbooks.Open(ExcelFileName)
'Set the text of the cell from Excel to the cell in the specified table in Word (the second table in this instance)
ActiveDocument.Tables(2).Cell(2, 2).Range.Text = exWb.Sheets("Test").Cells(2, 1)
' Close Excel bits
exWb.Close
Set exWb = Nothing
End Sub
关于vba - 从 Excel 中获取数据并插入到预先存在的 Word 表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23805804/