|
楼主 |
发表于 2019-11-6 13:03
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 梧叶沙沙 于 2019-11-6 22:27 编辑
案例一:Excel提取Word表格简历
以下为Word简历表中的具体结构,需要批量提取到Excel中。这里面就是对Word VBA中的table对象进行的读取操作。
- Sub 提取数据()
- On Error Resume Next
- N = 1
- Set doc = CreateObject("word.application")
- f = Dir(ThisWorkbook.Path & "\*.doc")
- Do While f <> ""
- N = N + 1
- Set wd = doc.documents.Open(ThisWorkbook.Path & "" & f)
- doc.Visible = True
- With doc.documents(1).Tables(1)
- Cells(N, 1) = l(.cell(1, 2).Range) '姓名
- Cells(N, 2) = l(.cell(1, 4).Range) '性别
- Cells(N, 3) = l(.cell(1, 6).Range) '年龄
- Cells(N, 4) = l(.cell(2, 2).Range) '籍贯
- Cells(N, 5) = l(.cell(2, 4).Range) '身份证号
- End With
- f = Dir
- wd.Close False
- Loop
- doc.Quit
- MsgBox "完成!"
- End Sub
- Function l(a)
- l = Left(a, Len(a) - 2)
- End Function
复制代码
案例二:Excel批量导出Word中的图片
批量导出各个Word文档中每个人的照片,以身份证号作为图片的名字。
- Sub 导出Word图片()
- Dim PathSht As String, wb As Workbook
- Application.ScreenUpdating = False
- For Each shp In ActiveSheet.Shapes '清除本表中的图片
- shp.Delete
- Next
- With Application.FileDialog(msoFileDialogFolderPicker)
- If .Show Then PathSht = .SelectedItems(1) Else Exit Sub
- End With
- PathSht = PathSht & IIf(Right(PathSht, 1) = "", "", "")
- myfile = PathSht & "保存图片"
- fol = Dir(myfile, vbDirectory)
- If fol = "" Then MkDir myfile '新建存储图片的路径
- myname = Dir(PathSht & "*.doc*")
- Call wd_pic(PathSht)
- MsgBox "完成!"
- Application.ScreenUpdating = True
- End Sub
- Sub wd_pic(p As String)
- Set wordapp = CreateObject("word.application")
- Set sht = ThisWorkbook.ActiveSheet
- f = Dir(p & "*.doc*") '结合Do While循环获取Word文档
- Do While f <> ""
- Set WordDOC = wordapp.Documents.Open(p & f) '逐个打开Word文件
- wordapp.Visible = True
- shenfen_num = l(WordDOC.Tables(1).cell(7, 2).Range) '获取身份证号
- For i = 1 To WordDOC.Shapes.Count '对文档中的图片进行遍历
- WordDOC.Shapes(i).Select '选中图片
- wordapp.Selection.Copy '复制图片。这里不能合并为一句,否则报错
- sht.PasteSpecial Format:="图片(增强型图元文件)", Link:=False, DisplayAsIcon:=False
- Set Excel_Shape = sht.Shapes(1) '因为当单个doc中存在图片量过多,均复制到xls中造成数据量过大,
- Excel_Shape.ScaleHeight 1, True, msoScaleFromMiddle
- Excel_Shape.ScaleWidth 1, True, msoScaleFromMiddle
- '这里采用了复制一个进入xls,再另存图片后,立即删除xls中的图片数据,所以遍历时,index永远是1
- Excel_Shape.Copy
- With sht.ChartObjects.Add(0, 0, Excel_Shape.Width, Excel_Shape.Height).Chart
- .Parent.Select '64位必须加这句,否则导出后是空白图片
- .Paste
- .Export p & "保存图片" & shenfen_num & ".bmp"
- .Parent.Delete '删除第二次复制产生的数据
- End With
- Excel_Shape.Delete '删除第一次复制产生的数据
- Next i
- WordDOC.Close '关闭当前Word文档
- f = Dir
- Loop
- wordapp.Quit
- End Sub
- Function l(a) '清除Word表格中的不可见符号
- l = WorksheetFunction.Clean(a)
- End Function
复制代码
案例三:Excel批量生成Word合同Excel数据批量写入Word,生成合同文书。数据的对应关系如下图截图中所示。
代码思路我用流程图画了出来:(jiangxiaoyun推荐这种案例用域对象的方法,稍后研究下)
- Sub 写入Word数据()
- Application.ScreenUpdating = False
- Set doc = CreateObject("word.application")
- doc.Visible = True
- kehu_row = ActiveSheet.Cells(Rows.Count, 3).End(3).Row '找到C列已使用的最大行号,客户名称所在列
- For i = 2 To kehu_row '开始对C列进行循环
- If Cells(i, 3) <> "" And Cells(i + 1, 3) = "" Then '当是最后一行的情况的时候
- r = Cells(i, 3).End(xlDown).Row - 1 '获取第三列此时的最大行号-1
- If r = Rows.Count - 1 And r <> Cells(i, 4).End(xlDown).Row - 1 Then '该客户有多个商品
- r = Cells(i, 4).End(xlDown).Row '第四列已使用的最大行号赋值给r
- ElseIf r = Rows.Count - 1 And r = Cells(i, 4).End(xlDown).Row - 1 Then '该客户只有一个商品
- r = i
- End If
- Set wd = doc.Documents.Open(ThisWorkbook.Path & "\合同模板.docx")
- With doc.Documents(1).Tables(1)
- .Rows(2).Select
- If r <> i Then doc.Selection.insertrowsbelow r - i '如果r<>i,也就是说,该客户不止一件商品,word表格插入行
- For rr = 2 To r - i + 2 '开始往word表格中写入数据
- .cell(rr, 1).Range = IIf(Cells(i + rr - 2, 5).Value = "", "", Cells(i + rr - 2, 5).Value)
- .cell(rr, 2).Range = IIf(Cells(i + rr - 2, 6).Value = "", "", Cells(i + rr - 2, 6).Value)
- .cell(rr, 3).Range = IIf(Cells(i + rr - 2, 7).Value = "", "", Cells(i + rr - 2, 7).Value)
- .cell(rr, 4).Range = IIf(Cells(i + rr - 2, 8).Value = "", "", Cells(i + rr - 2, 8).Value)
- .cell(rr, 5).Range = IIf(Cells(i + rr - 2, 9).Value = "", "", Cells(i + rr - 2, 9).Value)
- .cell(rr, 6).Range = IIf(Cells(i + rr - 2, 10).Value = "", "", Cells(i + rr - 2, 10).Value)
- .cell(rr, 7).Range = IIf(Cells(i + rr - 2, 11).Value = "", "", Cells(i + rr - 2, 11).Value)
- .cell(rr, 8).Range = IIf(Cells(i + rr - 2, 12).Value = "", "", Cells(i + rr - 2, 12).Value & "%")
- Next
- .cell(rr, 2).Range = WorksheetFunction.Sum(Range(Cells(i, 8), Cells(r, 8)))
- .cell(rr, 5).Range = WorksheetFunction.Sum(Range(Cells(i, 11), Cells(r, 11)))
- End With
- Set myrange = wd.Content
- With doc.Selection '查找替换数据
- .HomeKey Unit:=6
- .Find.Execute ("日期数据1")
- .Text = Cells(i, 1).Value
- .HomeKey Unit:=6
- .Find.Execute ("日期数据2")
- .Text = Cells(i, 1).Value
- .HomeKey Unit:=6
- .Find.Execute ("需方数据")
- .Text = Cells(i, 3).Value
- .HomeKey Unit:=6
- .Find.Execute ("总金额数据")
- .Text = Cells(i, 13).Value
- .HomeKey Unit:=6
- .Find.Execute ("甲方数据1")
- .Text = Cells(i, 3).Value
- .HomeKey Unit:=6
- .Find.Execute ("甲方数据2")
- .Text = Cells(i, 3).Value
- End With
- doc.ActiveWindow.ActivePane.View.SeekView = 9 '查找替换页眉数据
- doc.Selection.HomeKey Unit:=6
- If doc.Selection.Find.Execute("合同编号数据") Then
- doc.Selection.Text = Cells(i, 2).Value
- End If
- doc.Selection.Find.Execute Replace:=2
- doc.Selection.HomeKey Unit:=6
- fpath = ThisWorkbook.Path & "" & Cells(i, 2).Value & "静载合同.docx"
- wd.SaveAs fpath
- wd.Close False
- ElseIf Cells(i, 3) <> "" And Cells(i + 1, 3) <> "" Then '当是中间行的情况的时候
- Set wd = doc.Documents.Open(ThisWorkbook.Path & "\合同模板.docx")
- With doc.Documents(1).Tables(1)
- .cell(2, 1).Range = Cells(i, 5).Value
- End With
- Set myrange = wd.Content
- With doc.Selection
- .HomeKey Unit:=6
- .Find.Execute ("日期数据1")
- .Text = Cells(i, 1).Value
- .HomeKey Unit:=6
- .Find.Execute ("日期数据2")
- .Text = Cells(i, 1).Value
- .HomeKey Unit:=6
- .Find.Execute ("需方数据")
- .Text = Cells(i, 3).Value
- .HomeKey Unit:=6
- .Find.Execute ("总金额数据")
- .Text = Cells(i, 13).Value
- .HomeKey Unit:=6
- .Find.Execute ("甲方数据1")
- .Text = Cells(i, 3).Value
- .HomeKey Unit:=6
- .Find.Execute ("甲方数据2")
- .Text = Cells(i, 3).Value
- End With
- doc.ActiveWindow.ActivePane.View.SeekView = 9
- doc.Selection.HomeKey Unit:=6
- If doc.Selection.Find.Execute("合同编号数据") Then
- doc.Selection.Text = Cells(i, 2).Value
- End If
- doc.Selection.Find.Execute Replace:=2
- doc.Selection.HomeKey Unit:=6
- fpath = ThisWorkbook.Path & "" & Cells(i, 2).Value & "静载合同.docx"
- wd.SaveAs fpath
- wd.Close False
- Else
- End If
- Next
- doc.Quit
- Application.ScreenUpdating = True
- MsgBox "完成!"
- End Sub
复制代码
未完待续......
欢迎大家下载案例和我讨论。
|
评分
-
5
查看全部评分
-
|