Word 插件实现读取excel自动填写

日常工作中碰到需要将EXCEL的对应数据记录填写到word文档对应的位置,人工操作的方式是:

打开exel表--->查找对应报告号的行--->逐列复制excel表列单元格内容到WORD对应的位置(如下图标注所示)

这种方法耗时且容易出错。实际上如果使用VBA开发WORD插件,编写代替人手操作的自动程序可以实现自动填写(秒填的感觉很爽),可以一劳永逸解决这样的困扰。

实现的代码如下:

Sub ReadExcelData()

Dim xlApp As Object

Dim xlBook As Object

arr_excel = Array("SAMPLE CODE", "TRI PRODUIT", "TRI COMPOSANT", "TYPE", "COMPONENT", "Color", "Description", "FP MODEL", "FP MATERIAL", "FP Color", "DESCRIZIONE", _

"FP SUPPLIER", "USE", "COMPOSIZIONE", "FORNITORE", "POIDS G / M2", "INFLA", "SHIPPING DATE TO CHINA", "RECEPTION Date", "CELINE PACKAGE")

arr_E = Array("B", "", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "", "R", "S", "T", "U", "V", "W")

arr_Word = Array("CERTEST REFERENCE", "", "TRI PRODUIT", "TRI COMPOSANT", "TYPE", "COMPONENT", "Color", "Description", "FP MODEL", "FP MATERIAL", "FP Color", "Description PROJECT", _

"FP SUPLIER", "USE", "COMPOSITION", "", "SUPPLIER", "POIDS", "INFLA", "RECEPTION Date", "SHIPPING DATE TO CHINA", "Test PACKAGE")

rp = Left(ActiveDocument.Name, 11)

Set xlApp = CreateObject("Excel.Application")

filepath = "K:\XX\xx\xx" 'excel表坐在的路径

fn = Dir(filepath & "xxxx" & "*.xlsx")

Set xlBook = xlApp.Workbooks.Open(filepath & fn)

With xlBook.Sheets(1)

rws = .Cells(.rows.Count, "a").End(-4162).Row

For i = 2 To rws

If InStr(.Cells(i, 1).value, rp) > 0 Then

ReDim arr(22)

For j = 0 To 22

arr(j) = .Cells(i, j + 2)

Next

Exit For

End If

Next

End With

xlBook.Close False

Set xlBook = Nothing

Set xlApp = Nothing

'MsgBox UBound(arr)

For k = 0 To UBound(arr) - 1

If arr_Word(k) <> "" Then

Call infoFill(arr_Word(k), arr(k))

End If

Next

End Sub

Private Sub infoFill(kw, res)

Dim para As Paragraph

For Each para In ActiveDocument.Paragraphs

If InStr(UCase(para.Range.Text), UCase(kw)) > 0 Then

If res = "" Then

para.Next.Next.Range = "/"

Else

para.Next.Next.Range = res

End If

If InStr(UCase(para.Range.Text), "CERTEST REFERENCE") > 0 Then

para.Next.Next.Range = para.Next.Next.Range & Split(para.Next.Next.Range, ".")(0) & ".02"

End If

Exit Sub

End If

Next

将插件安装在WORD的NORML文件,实现点击按钮即可实现excel内容的自动填写.

给按钮添加图标,这样在WORD的操作界面就可以看到程序执行的插件。

这样插件就完成了,每次点击按钮就可以秒速完成填写。

相关推荐
何曾参静谧5 小时前
「Py」模块篇 之 PyAutoGUI库自动化图形用户界面库
运维·python·自动化
横冲直撞de13 小时前
word转markdown的方法(pandoc)
word
小白~小黑15 小时前
软件测试基础二十(接口测试 Postman)
python·自动化·postman
弗拉唐17 小时前
将Excel文件的两个表格经过验证后分别读取到Excel表和数据库
数据库·excel
Lizzy_Fly17 小时前
【Excel】身份证号最后一位“X”怎么计算
excel
深情废杨杨17 小时前
后端-实现excel的导出功能(超详细讲解)
java·spring boot·excel
智汇探长17 小时前
EasyExcel自定义设置Excel表格宽高
java·excel·easyexcel
Eiceblue17 小时前
通过Python 调整Excel行高、列宽
开发语言·vscode·python·pycharm·excel
crackbuy17 小时前
Excel筛选的操作教程
excel
笔墨登场说说17 小时前
Excel SUMIFS
excel