vba - 从 Excel 中获取数据并插入到预先存在的 Word 表中?

标签 vba excel ms-word

我知道以前有人问过这个问题,但我只能找到在您传输数据时在 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/

相关文章:

excel - 确保您的 VBA 代码兼容 64 位

excel - Power Query 合并列

arrays - 如果 AC-AF 列包含空白,则剪切并粘贴行

java - 问题编码 java->xls

javascript - 将 html 表格转换为 word 文档或可编辑的 pdf

java - 如何使用java应用程序打印word和excel文档?

excel - 由于动态组合框,清除用户表单子(monad)给出 1004 错误

vba - Access 自定义组

arrays - Redim VBA 中的可变维数

python - 如何从 DOC(而不是 DOCX)获取 XML?