如何根据excel中的表更新word表? [英] how to update word tables based on a table in excel?

查看:82
本文介绍了如何根据excel中的表更新word表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您好,

Hello,

推荐答案

您好,

您可以在Word中创建一个宏来自动化Excel以检索数据。您也可以使用ADO连接。请访问

You may create a macro in Word to automate Excel to retrieve the data. You could also use ADO connection. Please visit

使用ADO查询Excel电子表格

ExcelADO demonstrates how to use ADO to read and write data in Excel workbooks

获取Excel数据后,您可以循环遍历Word表格的单元格以检查匹配并写入新值。

After getting the Excel data, you could loop through the cells of Word table to check for matching and write new value.

>>重要提示:请注意,在Word表中我们看不到纯粹的汽车名称,例如我们看到奔驰L9000C或现代雅绅特2014.但是在excel中的参考表我们看到纯汽车名称(没有后缀和前缀)。

>>Important: Note that in the Word tables we don't see a pure car name, for example we see Benz L9000C or Hyundai Accent 2014. but in the reference table in excel we see pure car names (with no suffix and prefix).

您可以使用查找功能搜索Word表格以获取行号或行号,然后获取价格使用row.Cells(3).Range.Text来获取Word表格中的当前价格。下面是示例:

You could use Find function to search the Word table to get the row or row number and then get the price using row.Cells(3).Range.Text to get the current price in Word table. Here is the example:

Sub Demo()
Dim name As String
Dim price As String
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=D:\Book1.xlsx;" & _
        "Extended Properties=""Excel 12.0;HDR=Yes;"";"

objRecordset.Open "Select * FROM [Sheet1


",_
objConnection,adOpenStatic,adLockOptimistic,adCmdText

Do Until objRecordset.EOF
name = objRecordset.Fields.Item(" name")
price = objRecordset.Fields.Item(" price")
Dim tb As Table
设置tb = ActiveDocument.Tables(1)
Dim rng As Range
设置rng = tb.Range
rng.Find.Execute FindText:= name
如果是rng。 Find.found = True然后
如果rng.Information(wdWithInTable)那么
rng.Select
i = Selection.Cells(1).RowIndex
tb.Rows(i).Cells (3).Range.Text =价格
结束如果
结束如果
objRecordset.MoveNext
Loop
End Sub
", _ objConnection, adOpenStatic, adLockOptimistic, adCmdText Do Until objRecordset.EOF name = objRecordset.Fields.Item("name") price = objRecordset.Fields.Item("price") Dim tb As Table Set tb = ActiveDocument.Tables(1) Dim rng As Range Set rng = tb.Range rng.Find.Execute FindText:=name If rng.Find.found = True Then If rng.Information(wdWithInTable) Then rng.Select i = Selection.Cells(1).RowIndex tb.Rows(i).Cells(3).Range.Text = price End If End If objRecordset.MoveNext Loop End Sub

问候,

Celeste

Celeste


这篇关于如何根据excel中的表更新word表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆