Excel精英培训网

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

[已解决]word VBA有没有精通的老师?为什么命名总是向上错一行呢?

[复制链接]
发表于 2016-8-2 14:26 | 显示全部楼层 |阅读模式
word VBA有没有精通的老师?为什么命名总是向上错一行呢?
最佳答案
2016-8-2 16:35
ActiveRecord 不要用 wdNextRecord

修改后的代码如下

  1. Sub testMailMerge()
  2.     Dim myMerge As MailMerge, i As Integer, myname As String

  3.     Application.ScreenUpdating = False

  4.     Set myMerge = ActiveDocument.MailMerge

  5.     With myMerge.DataSource

  6.         .ActiveRecord = wdFirstRecord
  7.         .Parent.Destination = wdSendToNewDocument

  8.         If .Parent.State = wdMainAndDataSource Then
  9.             .ActiveRecord = wdFirstRecord
  10.             For i = 1 To .RecordCount
  11.                 .ActiveRecord = i
  12.                 .FirstRecord = i
  13.                 .LastRecord = i
  14.                 myname = .DataFields(2).Value & "【" & .DataFields(3).Value & "】"

  15.                 If .DataFields(10).Value <> "" Then
  16.                     .Parent.Execute Pause:=False
  17.                     With ActiveDocument
  18.                         .Content.Characters.Last.Previous.Delete
  19.                         .SaveAs ThisDocument.Path & "\各公司协议" & myname & "业务约定书.doc"
  20.                         .Close
  21.                     End With
  22.                 End If
  23.             Next
  24.         End If
  25.     End With
  26.     Application.ScreenUpdating = True
  27. End Sub
复制代码
 楼主| 发表于 2016-8-2 14:28 | 显示全部楼层
代码在word中

协议生成模板.zip

115.65 KB, 下载次数: 18

回复

使用道具 举报

发表于 2016-8-2 16:35 | 显示全部楼层    本楼为最佳答案   
ActiveRecord 不要用 wdNextRecord

修改后的代码如下

  1. Sub testMailMerge()
  2.     Dim myMerge As MailMerge, i As Integer, myname As String

  3.     Application.ScreenUpdating = False

  4.     Set myMerge = ActiveDocument.MailMerge

  5.     With myMerge.DataSource

  6.         .ActiveRecord = wdFirstRecord
  7.         .Parent.Destination = wdSendToNewDocument

  8.         If .Parent.State = wdMainAndDataSource Then
  9.             .ActiveRecord = wdFirstRecord
  10.             For i = 1 To .RecordCount
  11.                 .ActiveRecord = i
  12.                 .FirstRecord = i
  13.                 .LastRecord = i
  14.                 myname = .DataFields(2).Value & "【" & .DataFields(3).Value & "】"

  15.                 If .DataFields(10).Value <> "" Then
  16.                     .Parent.Execute Pause:=False
  17.                     With ActiveDocument
  18.                         .Content.Characters.Last.Previous.Delete
  19.                         .SaveAs ThisDocument.Path & "\各公司协议" & myname & "业务约定书.doc"
  20.                         .Close
  21.                     End With
  22.                 End If
  23.             Next
  24.         End If
  25.     End With
  26.     Application.ScreenUpdating = True
  27. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-30 01:02 , Processed in 0.531798 second(s), 17 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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