Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1782|回复: 0

打印代码出错。。

[复制链接]
发表于 2015-7-9 15:44 | 显示全部楼层 |阅读模式
  1. Option Explicit

  2. Sub tt()
  3. On Error Resume Next

  4. Dim 最后行号, i, m, start1, end1 As Integer
  5. Dim Word对象 As New Word.Application ', Fso As New FileSystemObject
  6. Dim Str1, Str2, 当前路径, 导出路径, 导出文件名, 导出路径文件名, 邮编, 地址, 联系人, 联系人电话, 缴费号, 申请人 As String

  7. 最后行号 = Sheets("Sheet1").Range("C65536").End(xlUp).Row
  8. For i = 6 To 最后行号
  9.     start1 = i                                  '根据不同缴费单号分类
  10.     While Sheets("Sheet1").Cells(i, "AC") = Sheets("Sheet1").Cells(i + 1, "AC")
  11.         i = i + 1
  12.     Wend
  13.     end1 = i
  14.     With Sheets("Sheet1")
  15.         Sheets("Sheet2").Rows("2:65536").Clear
  16.         .Range(Cells(start1, "E"), Cells(end1, "G")).Copy Sheets("Sheet2").Range("B2")
  17.         .Range(Cells(start1, "A"), Cells(end1, "A")).Copy
  18.         Sheets("Sheet2").Range("A2").PasteSpecial Paste:=xlPasteValues
  19.         Sheets("Sheet2").Range("A2").PasteSpecial Paste:=xlPasteFormats
  20.         
  21.         .Range(Cells(start1, "AO"), Cells(end1, "AO")).Copy                            '总计
  22.         Sheets("Sheet2").Range("H2").PasteSpecial Paste:=xlPasteValues
  23.         Sheets("Sheet2").Range("H2").PasteSpecial Paste:=xlPasteFormats
  24.         
  25.         .Range(Cells(start1, "I"), Cells(end1, "J")).Copy                            '官费和服务费
  26.         Sheets("Sheet2").Range("E2").PasteSpecial Paste:=xlPasteValues
  27.         Sheets("Sheet2").Range("E2").PasteSpecial Paste:=xlPasteFormats
  28.         
  29.         .Range(Cells(start1, "N"), Cells(end1, "N")).Copy                            '缴费期限
  30.         Sheets("Sheet2").Range("G2").PasteSpecial Paste:=xlPasteValues
  31.         Sheets("Sheet2").Range("G2").PasteSpecial Paste:=xlPasteFormats
  32.          
  33.         .Range(Cells(start1, "P"), Cells(end1, "P")).Copy                           '专利名称
  34.         Sheets("Sheet2").Range("T2").PasteSpecial Paste:=xlPasteValues
  35.         Sheets("Sheet2").Range("T2").PasteSpecial Paste:=xlPasteFormats
  36.         
  37.         .Range(Cells(start1, "AD"), Cells(end1, "AL")).Copy Sheets("Sheet2").Range("I2")
  38.         .Range(Cells(start1, "AC"), Cells(end1, "AC")).Copy Sheets("Sheet2").Range("R2")
  39.         
  40.         .Cells(1, "Z") = Application.Sum(Sheets("Sheet2").Range("E2:E65536"))      '本期发生官费总金额
  41.         .Cells(1, "X") = Application.Sum(Sheets("Sheet2").Range("F2:F65536"))      '本期发生服务费总金额
  42.         .Cells(1, "V") = .Cells(1, "Z") + .Cells(1, "X")                          '本期应缴总额
  43.         .Range(Cells(1, "U"), Cells(1, "V")).Copy Sheets("Sheet2").Cells(end1 - start1 + 3, "A")
  44.     End With
  45.     Sheets("Sheet2").UsedRange.Borders.ColorIndex = 0   '已使用的范围,即有内容的表格添加黑色边框线
  46.         
  47.     Sheets("Sheet2").UsedRange.Copy
  48.     邮编 = Sheets("Sheet1").Cells(start1, "T")
  49.     地址 = Sheets("Sheet1").Cells(start1, "U")
  50.     联系人 = Sheets("Sheet1").Cells(start1, "V")
  51.     联系人电话 = Sheets("Sheet1").Cells(start1, "W")
  52.     申请人 = Sheets("Sheet1").Cells(start1, "C")
  53.          
  54.    当前路径 = "\\192.168.1.120\杭诚软件\缴费单"
  55.    导出路径 = 当前路径 & "\已生成" & Format(Now, "yyyy-mm-dd")
  56.    MkDir 导出路径
  57.     导出文件名 = Cells(i, "AC") & ".doc"
  58.    
  59.    导出路径文件名 = 导出路径 & "" & 导出文件名
  60.    'If Fso.FileExists(导出路径文件名) = True Then Fso.DeleteFile 导出路径文件名, True

  61.    FileCopy 当前路径 & "\缴纳授权费用模板实样.doc", 导出路径文件名
  62.    With Word对象
  63.       .Documents.Open 导出路径文件名
  64.       .Visible = True

  65.       .Selection.HomeKey Unit:=wdStory
  66.       .Selection.Find.Text = "[当前时间]"
  67.       .Selection.Find.Execute
  68.       .Selection = Format(Now, "yyyy-mm-dd")
  69.            
  70.       .Selection.HomeKey Unit:=wdStory
  71.       .Selection.Find.Text = "[缴费号]"
  72.       .Selection.Find.Execute
  73.       .Selection = Sheet2.Cells(2, "R")
  74.       
  75.       .Selection.HomeKey Unit:=wdStory
  76.       .Selection.Find.Text = "[收款账户]"
  77.       .Selection.Find.Execute
  78. '      .Selection = Cells(1, "J")
  79.       .Selection = Sheet2.Cells(2, "J")
  80.       
  81.       .Selection.HomeKey Unit:=wdStory
  82.       .Selection.Find.Text = "[银行账号]"
  83.       .Selection.Find.Execute
  84. '      .Selection = Cells(1, "L")
  85.       .Selection = Sheet2.Cells(2, "K")
  86.       
  87.       .Selection.HomeKey Unit:=wdStory
  88.       .Selection.Find.Text = "[开户银行]"
  89.       .Selection.Find.Execute
  90. '      .Selection = Cells(2, "K")
  91.       .Selection = Sheet2.Cells(2, "L")
  92.       
  93.       .Selection.HomeKey Unit:=wdStory
  94.       .Selection.Find.Text = "[公司地址]"
  95.       .Selection.Find.Execute
  96. '      .Selection = Cells(1, "P")
  97.       .Selection = Sheet2.Cells(2, "M")
  98.       
  99.       
  100.       .Selection.HomeKey Unit:=wdStory
  101.       .Selection.Find.Text = "[公司邮编]"
  102.       .Selection.Find.Execute
  103. '      .Selection = Cells(2, "J")
  104.       .Selection = Sheet2.Cells(2, "N")
  105.       
  106. '      .Selection.HomeKey Unit:=wdStory
  107. '      .Selection.Find.Text = "[公司电话]"
  108. '      .Selection.Find.Execute
  109. ''      .Selection = Cells(2, "L")
  110. '      .Selection = Sheet2.Cells(2, "O")
  111. '
  112. '      .Selection.HomeKey Unit:=wdStory
  113. '      .Selection.Find.Text = "[公司传真]"
  114. '      .Selection.Find.Execute
  115. ''      .Selection = Cells(2, "N")
  116. '      .Selection = Sheet2.Cells(2, "P")
  117.       
  118.       .Selection.HomeKey Unit:=wdStory
  119.       .Selection.Find.Text = "[收款户名]"
  120.       .Selection.Find.Execute
  121. '      .Selection = Cells(2, "N")
  122.       .Selection = Sheet2.Cells(2, "J")
  123.       
  124.       .Selection.HomeKey Unit:=wdStory
  125.       .Selection.Find.Text = "[邮编]"
  126.       .Selection.Find.Execute
  127.       .Selection = 邮编

  128.       .Selection.HomeKey Unit:=wdStory
  129.       .Selection.Find.Text = "[地址]"
  130.       .Selection.Find.Execute
  131.       .Selection = 地址

  132.       .Selection.HomeKey Unit:=wdStory
  133.       .Selection.Find.Text = "[联系人]"
  134.       .Selection.Find.Execute
  135.       .Selection = 联系人

  136.       .Selection.HomeKey Unit:=wdStory
  137.       .Selection.Find.Text = "[联系人电话]"
  138.       .Selection.Find.Execute
  139.       .Selection = 联系人电话

  140.       .Selection.HomeKey Unit:=wdStory
  141.       .Selection.Find.Text = "[申请人]"
  142.       .Selection.Find.Execute
  143.       .Selection = 申请人
  144.       
  145.       .Selection.HomeKey Unit:=wdStory
  146.       .Selection.Find.Text = "[缴费号]"
  147.       .Selection.Find.Execute
  148.       .Selection = 缴费号

  149.       .Selection.HomeKey Unit:=wdStory
  150.       .Selection.Find.Text = "[表格处]"
  151.       .Selection.Find.Execute
  152.       .Selection.PasteExcelTable False, False, False
  153.       
  154.      With .Selection.Find        '替换连续的手动换行符
  155.         .Text = "^l^l"
  156.         .Replacement.Text = "^p"
  157.         .MatchWildcards = False
  158.         .Execute Replace:=wdReplaceAll, Forward:=True, _
  159.         Wrap:=wdFindContinue
  160.      End With
  161.      
  162.     End With
  163.    
  164.    Word对象.Documents.Save
  165. Word对象.ActiveDocument.PrintOut
  166.    Word对象.Documents.Close

  167. Next i
  168.     Set Word对象 = Nothing
  169.     Word对象.Quit
  170. End Sub

  171. 这段代码是调用系统模板打印的代码 ,问题就是不知道怎么原因,打到7-8分的时候就会出现莫名其妙的错误,格式变了,不知道是什么问题 还请指点一下~~!!





复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-16 15:35 , Processed in 0.289672 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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