ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: QW2012

excel数据与word模板导入

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-11-15 13:04 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 根据模板生成文档()
Dim i%, j%, k%, x%, y%, ss$, s$
Dim arr, temp, krr, trr, tm, t, rArr, tArr
Dim myPath$, OmyFile$, Nmyfile$, myPic$
Dim wdApp, d, wdD
Set wdApp = CreateObject("Word.Application")
Set d = CreateObject("scripting.dictionary")
myPath = ThisWorkbook.Path & "\"
arr = Range("A1:W" & Cells(Rows.Count, 4).End(xlUp).Row)

For i = 2 To UBound(arr)
    If arr(i, 1) <> "" Then
        If ss <> "" Then ss = ss & "-"
        ss = ss & i
    End If
Next
rArr = Split(ss, "-")
ReDim tArr(0 To UBound(rArr))
For j = 0 To UBound(rArr) - 1
    tArr(j) = rArr(j + 1) - 1
Next
tArr(UBound(tArr)) = UBound(arr)

For k = 0 To UBound(rArr)
    s = arr(rArr(k), 1) & "-" & arr(rArr(k), 2) & "-" & arr(rArr(k), 3)
    temp = Range("D" & rArr(k) & ":W" & tArr(k)).Value
    d(s) = temp
Next

krr = d.Keys
OmyFile = "成绩报告单模板.docx"
For j = 0 To UBound(krr)
    t = Split(krr(j), "-")
    Nmyfile = t(1) & "_" & t(0) & "学生毕业个人成绩明细清单" & ".docx"
    FileCopy myPath & OmyFile, myPath & "生成学生成绩单\" & Nmyfile
    tm = d(krr(j))
    Set wdD = wdApp.Documents.Open(myPath & "生成学生成绩单\" & Nmyfile)
    With wdD.Tables(1)
        .Cell(2, 2).Range.Text = t(2)
        .Cell(2, 4).Range.Text = t(1)
        .Cell(3, 4).Range.Text = t(0)
        myPic = myPath & "学生照片\" & t(0) & ".jpg"
        If Dir(myPic) <> "" Then
            With .Cell(5, 1).Range
                .InlineShapes.AddPicture Filename:=myPic, _
                LinkToFile:=False, SaveWithDocument:=True
               .InlineShapes(1).Height = 250
               .InlineShapes(1).Width = 550
            End With
        End If
        If UBound(tm) > 1 Then
            .Cell(8, 20).Select
            wdApp.Selection.InsertRowsBelow UBound(tm) - 1
        End If
        For x = 1 To UBound(tm)
            .Cell(7 + x, 1).Range.Text = x
            .Cell(7 + x, 2).Range.Text = tm(x, 1)
            .Cell(7 + x, 3).Range.Text = tm(x, 2)
            For y = 4 To 18
                .Cell(7 + x, y).Range.Text = tm(x, y + 2)
            Next
        Next
    End With
    wdD.Save
    wdD.Close
Next
Set wdD = Nothing
wdApp.Quit
Set d = Nothing
End Sub

TA的精华主题

TA的得分主题

发表于 2019-11-15 13:19 | 显示全部楼层
这是附件,可以实现目的。

自动生成成绩单.zip

1.34 MB, 下载次数: 19

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-19 19:48 , Processed in 0.037372 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表