繁体   English   中英

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

[英]Taking data from Excel and inserting into a pre existing Word table?

我知道之前已经问过这个问题,但我只能在传输数据时找到在Word中创建表的网站。

我有一个包含3个表的Word文档,并希望将客户端发送的Excel文件中的数据提取到Word表格中的某些单元格中。 EG我想在Excel中获取单元格D3的内容并将其放在Word第二个表格的单元格2,2中? 有没有办法引用哪个表来插入数据?

任何人都可以帮助解决这个问题,或者只是将我推向正确的方向,因为我对Macros等相对较新。当我们使用Word文档并将Excel文件发送给我们时,是否会使Word中的宏更有益?

谢谢

我已经设法修复它,通常它相对简单,但感谢@David Zemens推动正确的方向。 我不知道在之前搜索“ThisDocument”时我是如何错过“桌子”的。

 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

暂无
暂无

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

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