ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 414|回复: 0

【求教】想从excel中提取相应的字段到word模板中

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-6-22 21:52 | 显示全部楼层 |阅读模式
朋友给了一段这样的代码,请大神帮忙解释下呀,谢谢!!!

Sub obj()
Dim objWord As Object
Dim FileStart
Dim FileNew
Set objWord = CreateObject("Word.Application")
    FileSt = "C:\My_acts_template\_act_template_12.dotx"
    FileNew = "C:\My_acts_template\act template_2.docx"
   
    Set objDoc = objWord.Documents.Open(FileSt)
    objWord.Visible = True
         
   
    objDoc.Bookmarks("ACT_DATE").Range.InsertAfter (Cells(3, 3).Value)
    objDoc.Bookmarks("ACT_DATE_0").Range.InsertAfter (Cells(3, 3).Value)
   
    objDoc.Bookmarks("invoice_date_1").Range.InsertAfter (Cells(3, 10).Value)
    objDoc.Bookmarks("invoice_date_2").Range.InsertAfter (Cells(4, 10).Value)
    objDoc.Bookmarks("invoice_date_3").Range.InsertAfter (Cells(5, 10).Value)
    objDoc.Bookmarks("invoice_date_4").Range.InsertAfter (Cells(6, 10).Value)
    objDoc.Bookmarks("invoice_date_5").Range.InsertAfter (Cells(7, 10).Value)
    objDoc.Bookmarks("invoice_date_6").Range.InsertAfter (Cells(8, 10).Value)
    objDoc.Bookmarks("invoice_date_7").Range.InsertAfter (Cells(9, 10).Value)
    objDoc.Bookmarks("invoice_date_8").Range.InsertAfter (Cells(10, 10).Value)
   
    objDoc.Bookmarks("invoice_number_1").Range.InsertAfter (Cells(3, 11).Value)
    objDoc.Bookmarks("invoice_number_2").Range.InsertAfter (Cells(4, 11).Value)
    objDoc.Bookmarks("invoice_number_3").Range.InsertAfter (Cells(5, 11).Value)
    objDoc.Bookmarks("invoice_number_4").Range.InsertAfter (Cells(6, 11).Value)
    objDoc.Bookmarks("invoice_number_5").Range.InsertAfter (Cells(7, 11).Value)
    objDoc.Bookmarks("invoice_number_6").Range.InsertAfter (Cells(8, 11).Value)
    objDoc.Bookmarks("invoice_number_7").Range.InsertAfter (Cells(9, 11).Value)
    objDoc.Bookmarks("invoice_number_8").Range.InsertAfter (Cells(10, 11).Value)
   
    objDoc.Bookmarks("Invoice_amount_1").Range.InsertAfter (Cells(3, 14).Text)
    objDoc.Bookmarks("Invoice_amount_2").Range.InsertAfter (Cells(4, 14).Text)
    objDoc.Bookmarks("Invoice_amount_3").Range.InsertAfter (Cells(5, 14).Text)
    objDoc.Bookmarks("Invoice_amount_4").Range.InsertAfter (Cells(6, 14).Text)
    objDoc.Bookmarks("Invoice_amount_5").Range.InsertAfter (Cells(7, 14).Text)
    objDoc.Bookmarks("Invoice_amount_6").Range.InsertAfter (Cells(8, 14).Text)
    objDoc.Bookmarks("Invoice_amount_7").Range.InsertAfter (Cells(9, 14).Text)
    objDoc.Bookmarks("Invoice_amount_8").Range.InsertAfter (Cells(10, 14).Text)
   
    objDoc.Bookmarks("Invoice_Balance_1").Range.InsertAfter (Cells(3, 13).Text)
    objDoc.Bookmarks("Invoice_Balance_2").Range.InsertAfter (Cells(4, 13).Text)
    objDoc.Bookmarks("Invoice_Balance_3").Range.InsertAfter (Cells(5, 13).Text)
    objDoc.Bookmarks("Invoice_Balance_4").Range.InsertAfter (Cells(6, 13).Text)
    objDoc.Bookmarks("Invoice_Balance_5").Range.InsertAfter (Cells(7, 13).Text)
    objDoc.Bookmarks("Invoice_Balance_6").Range.InsertAfter (Cells(8, 13).Text)
    objDoc.Bookmarks("Invoice_Balance_7").Range.InsertAfter (Cells(9, 13).Text)
    objDoc.Bookmarks("Invoice_Balance_8").Range.InsertAfter (Cells(10, 13).Text)
   
    objDoc.Bookmarks("Amout_to_deduct_1").Range.InsertAfter (Cells(3, 12).Text)
    objDoc.Bookmarks("Amout_to_deduct_2").Range.InsertAfter (Cells(4, 12).Text)
    objDoc.Bookmarks("Amout_to_deduct_3").Range.InsertAfter (Cells(5, 12).Text)
    objDoc.Bookmarks("Amout_to_deduct_4").Range.InsertAfter (Cells(6, 12).Text)
    objDoc.Bookmarks("Amout_to_deduct_5").Range.InsertAfter (Cells(7, 12).Text)
    objDoc.Bookmarks("Amout_to_deduct_6").Range.InsertAfter (Cells(8, 12).Text)
    objDoc.Bookmarks("Amout_to_deduct_7").Range.InsertAfter (Cells(9, 12).Text)
    objDoc.Bookmarks("Amout_to_deduct_8").Range.InsertAfter (Cells(10, 12).Text)
   
    objDoc.Bookmarks("VAT_1").Range.InsertAfter (Cells(3, 17).Text)
    objDoc.Bookmarks("VAT_2").Range.InsertAfter (Cells(4, 17).Text)
    objDoc.Bookmarks("VAT_3").Range.InsertAfter (Cells(5, 17).Text)
    objDoc.Bookmarks("VAT_4").Range.InsertAfter (Cells(6, 17).Text)
    objDoc.Bookmarks("VAT_5").Range.InsertAfter (Cells(7, 17).Text)
    objDoc.Bookmarks("VAT_6").Range.InsertAfter (Cells(8, 17).Text)
    objDoc.Bookmarks("VAT_7").Range.InsertAfter (Cells(9, 17).Text)
    objDoc.Bookmarks("VAT_8").Range.InsertAfter (Cells(10, 17).Text)
        
    objDoc.Bookmarks("CONTRACT_0_1").Range.InsertAfter (Cells(3, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_2").Range.InsertAfter (Cells(4, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_3").Range.InsertAfter (Cells(5, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_4").Range.InsertAfter (Cells(6, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_5").Range.InsertAfter (Cells(7, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_6").Range.InsertAfter (Cells(8, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_7").Range.InsertAfter (Cells(9, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_8").Range.InsertAfter (Cells(10, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_9").Range.InsertAfter (Cells(11, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_10").Range.InsertAfter (Cells(12, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_11").Range.InsertAfter (Cells(13, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_12").Range.InsertAfter (Cells(14, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_13").Range.InsertAfter (Cells(15, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_14").Range.InsertAfter (Cells(16, 2).Value)
    objDoc.Bookmarks("CONTRACT_0_15").Range.InsertAfter (Cells(17, 2).Value)
   
   objDoc.Bookmarks("ACT_AMOUNT_0_1").Range.InsertAfter (Cells(3, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_0_2").Range.InsertAfter (Cells(4, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_0_3").Range.InsertAfter (Cells(5, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_0_4").Range.InsertAfter (Cells(6, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_0_5").Range.InsertAfter (Cells(7, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_0_6").Range.InsertAfter (Cells(8, 4).Text)

   
    objDoc.Bookmarks("SETOFF_AMOUNT_0").Range.InsertAfter (Cells(45, 4).Text)
    objDoc.Bookmarks("SETOFF_AMOUNT_1").Range.InsertAfter (Cells(45, 4).Text)
    objDoc.Bookmarks("SETOFF_AMOUNT_2").Range.InsertAfter (Cells(45, 4).Text)
    objDoc.Bookmarks("SETOFF_WITH_WORDS_ENG").Range.InsertAfter (Cells(45, 5).Value)
    objDoc.Bookmarks("SETOFF_WITH_WORDS_RU").Range.InsertAfter (Cells(45, 7).Value)
    objDoc.Bookmarks("SETOFF_Cents").Range.InsertAfter (Cells(45, 6).Value)
    objDoc.Bookmarks("SETOFF_Cents_1").Range.InsertAfter (Cells(45, 6).Value)
   
    objDoc.Bookmarks("Balance_AMOUNT").Range.InsertAfter (Cells(7, 13).Text)
    objDoc.Bookmarks("Balance_VAT_AMOUNT").Range.InsertAfter (Cells(7, 15).Text)
  
    objDoc.Bookmarks("INVOICE_AMOUNT_FOR_THE_NEXT_SETOFF_1").Range.InsertAfter (Cells(46, 4).Text)
    objDoc.Bookmarks("INVOICE_AMOUNT_FOR_THE_NEXT_SETOFF_2").Range.InsertAfter (Cells(46, 4).Text)
    objDoc.Bookmarks("INVOICE_Number_FOR_THE_NEXT_SETOFF_1").Range.InsertAfter (Cells(46, 2).Value)
    objDoc.Bookmarks("INVOICE_Number_FOR_THE_NEXT_SETOFF_2").Range.InsertAfter (Cells(46, 2).Value)
    objDoc.Bookmarks("INVOICE_date_FOR_THE_NEXT_SETOFF_1").Range.InsertAfter (Cells(46, 3).Value)
    objDoc.Bookmarks("INVOICE_date_FOR_THE_NEXT_SETOFF_2").Range.InsertAfter (Cells(46, 3).Value)
    objDoc.Bookmarks("FOR_THE_NEXT_SETOFF_WITH_WORDS_ENG").Range.InsertAfter (Cells(46, 5).Value)
    objDoc.Bookmarks("FOR_THE_NEXT_SETOFF_WITH_WORDS_RU").Range.InsertAfter (Cells(46, 7).Value)
    objDoc.Bookmarks("INVOICE_FOR_THE_NEXT_SETOFF_Cents_1").Range.InsertAfter (Cells(46, 6).Value)
    objDoc.Bookmarks("INVOICE_FOR_THE_NEXT_SETOFF_Cents_2").Range.InsertAfter (Cells(46, 6).Value)
      
      
    objDoc.Bookmarks("CONTRACT_1").Range.InsertAfter (Cells(3, 2).Value)
    objDoc.Bookmarks("CONTRACT_1_1").Range.InsertAfter (Cells(3, 2).Value)
    objDoc.Bookmarks("ACT_DATE_1").Range.InsertAfter (Cells(3, 3).Value)
    objDoc.Bookmarks("ACT_DATE_1_1").Range.InsertAfter (Cells(3, 3).Value)
    objDoc.Bookmarks("ACT_AMOUNT_1").Range.InsertAfter (Cells(3, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_1_1").Range.InsertAfter (Cells(3, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_WITH_WORDS_ENG_1").Range.InsertAfter (Cells(3, 5).Value)
    objDoc.Bookmarks("ACT_AMOUNT_WITH_WORDS_RU_1").Range.InsertAfter (Cells(3, 7).Value)
    objDoc.Bookmarks("Cents_1").Range.InsertAfter (Cells(3, 6).Value)
    objDoc.Bookmarks("Cents_1_1").Range.InsertAfter (Cells(3, 6).Value)
      
    objDoc.Bookmarks("CONTRACT_2").Range.InsertAfter (Cells(4, 2).Value)
    objDoc.Bookmarks("CONTRACT_2_2").Range.InsertAfter (Cells(4, 2).Value)
    objDoc.Bookmarks("ACT_DATE_2").Range.InsertAfter (Cells(4, 3).Value)
    objDoc.Bookmarks("ACT_DATE_2_2").Range.InsertAfter (Cells(4, 3).Value)
    objDoc.Bookmarks("ACT_AMOUNT_2").Range.InsertAfter (Cells(4, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_2_2").Range.InsertAfter (Cells(4, 4).Text)
    objDoc.Bookmarks("ACT_AMOUNT_WITH_WORDS_ENG_2").Range.InsertAfter (Cells(4, 5).Value)
    objDoc.Bookmarks("ACT_AMOUNT_WITH_WORDS_RU_2").Range.InsertAfter (Cells(4, 7).Value)
    objDoc.Bookmarks("Cents_2").Range.InsertAfter (Cells(4, 6).Value)
    objDoc.Bookmarks("Cents_2_2").Range.InsertAfter (Cells(4, 6).Value)

   
    objWord.ActiveDocument.SaveAs _
            Filename:=FileNew, _
            FileFormat:=wdFormatDocument, _
            Password:="", _
            AddToRecentFiles:=True, _
            WritePassword:="", _
            ReadOnlyRecommended:=False
   
objWord.Quit
End Sub

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

本版积分规则

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

GMT+8, 2024-11-23 15:35 , Processed in 0.030689 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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