ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

原创:实现word自定义新域及一键更新域

[复制链接]

TA的精华主题

TA的得分主题

发表于 2008-9-26 14:13 | 显示全部楼层 |阅读模式
Public Sub UpdateFields()
On Error Resume Next
ActiveWindow.View.ShowFieldCodes = False
fic = ActiveDocument.Fields.Count
For counts = 0 To fic - 1
coun = fic - counts
typ = ActiveDocument.Fields(coun).Type
If typ = 64 Then
zfc = ActiveDocument.Fields(coun).Code.Text
cfs = ActiveDocument.Fields(coun).Code.Fields.Count
If cfs > 0 Then
        ksb = Mid(zfc, ActiveDocument.Fields(coun).Code.Fields(1).Code.Start - ActiveDocument.Fields(coun).Code.Start, 1)
        jis = Mid(zfc, ActiveDocument.Fields(coun).Code.Fields(1).Code.End - ActiveDocument.Fields(coun).Code.Start + 1, 1)
        zfc = Replace(zfc, ksb, "{")
        zfc = Replace(zfc, jis, "}")
      
End If
For rs = 1 To cfs
zfc = Replace(zfc, "{" & ActiveDocument.Fields(coun).Code.Fields(rs).Code.Text & "}", ActiveDocument.Fields(coun).Code.Fields(rs).Result.Text)
Next rs
hanscc = Split(zfc, "\$")
If UBound(hanscc) > 0 Then
    hanscc(1) = Trim(hanscc(1))
    For s = 1 To 20
    hanscc(1) = Replace(hanscc(1), "  ", " ")
    Next s
    hanscc(1) = Trim(Replace(hanscc(1), "\* MERGEFORMAT", ""))
    hanscs = Split(Mid(hanscc(1), 2, Len(hanscc(1)) - 2), ",")
    hansm = Trim(hanscs(0))
    hanscs(0) = ""
    hansc = Mid(Join(hanscs, ","), 2)
    
    If Right(hansm, 2) = "()" Then
       If UBound(hanscs) > 0 Then
         susc = Application.Run(Left(hansm, Len(hansm) - 2), hansc)
        Else
         susc = Application.Run(Left(hansm, Len(hansm) - 2))
        End If
        
     wdbl = Trim(Replace(UCase(hanscc(0)), "DOCVARIABLE", ""))
    Num = 0
    For Each aVar In ActiveDocument.Variables
         If aVar.Name = wdbl Then
                  Num = aVar.Index
                  Exit For
         End If
    Next aVar
    If Num = 0 Then
          ActiveDocument.Variables.Add Name:=wdbl, Value:=susc
    Else
         ActiveDocument.Variables(Num).Value = susc
    End If
    End If
    End If
End If
Next counts
ActiveDocument.Fields.Update

End Sub
Public Function choose(zf)
chooses = Split(zf, ",")
wz = Val(chooses(0))
cou = UBound(chooses)
If wz > cou Or wz = 0 Then
choose = " "
Else
choose = chooses(wz)
End If
End Function
Sub MailMergeNextRecord()
'
' MailMessageNext 宏
' 定位至下一个邮件
'
If ActiveDocument.MailMerge.DataSource.RecordCount > -1 Then
If ActiveDocument.MailMerge.DataSource.ActiveRecord < ActiveDocument.MailMerge.DataSource.RecordCount Then
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Else
MsgBox "已经到达末记录!"
End If
Else
MsgBox "当前文档没有附加邮件合并数据源!"
End If
UpdateFields
End Sub
Sub MailMergePrevRecord()
'
' MailMergePrevRecord 宏
' 显示当前邮件合并数据源的上一个记录
'
If ActiveDocument.MailMerge.DataSource.RecordCount > -1 Then
If ActiveDocument.MailMerge.DataSource.ActiveRecord <> ActiveDocument.MailMerge.DataSource.FirstRecord Then
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdPreviousRecord
Else
MsgBox "已经到达首记录!"
End If
Else
MsgBox "当前文档没有附加邮件合并数据源!"
End If
UpdateFields
End Sub
Sub MailMergeFirstRecord()
'
' MailMergeFirstRecord 宏
' 显示当前邮件合并数据源中的首记录
'
If ActiveDocument.MailMerge.DataSource.RecordCount > -1 Then
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
Else
MsgBox "当前文档没有附加邮件合并数据源!"
End If
UpdateFields

End Sub
Sub MailMergeLastRecord()
'
' MailMergeLastRecord 宏
' 显示当前邮件合并数据源中的末记录
'
If ActiveDocument.MailMerge.DataSource.RecordCount > -1 Then
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
Else
MsgBox "当前文档没有附加邮件合并数据源!"
End If
UpdateFields

End Sub
如何使用?
例如:在word文档中,插入文档变量域,文档变量任意指定,切换域代码,在代码的文档变量后,\* MERGEFORMAT之前添加\$"choose(),1,456,789,1236",choose(),表示选择函数,1表示选择列表的第几个选项,456,789,1236表示选择列表。将上述宏代码复制到norml模板下的宏模块下面,就可以了。更新任意域,就可以完成整篇文档域的更新。
[此贴子已经被作者于2008-10-13 8:44:18编辑过]

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-9-26 14:21 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Public Sub new_command()
menucap = "更新域,邮件首记录,邮件上一个,邮件下一个,邮件末记录"
menuact = "update_fields,First_record,Previous_record,next_record,Last_record"
For i = 1 To CommandBars("Tools").Controls.Count
If Not CommandBars("Tools").Controls(i).BuiltIn Then
    CommandBars("Tools").Controls(i).Delete
End If
Next i
menuca = Split(menucap, ",")
menuac = Split(menuact, ",")
For i = 0 To UBound(menuca)
Set newItem = CommandBars("Tools").Controls.Add(Type:=msoControlButton)
With newItem
    .BeginGroup = False
    .Caption = menuca(i)
    .FaceId = 0
    .OnAction = menuac(i)
End With
Next i
End Sub
Public Function update_fields()
fic = ActiveDocument.Fields.Count
For coun = 1 To fic
typ = ActiveDocument.Fields(coun).Type
If typ = 81 Then
sh = ActiveDocument.Fields(coun).Code.Fields.Count
zfc = ActiveDocument.Fields(coun).Code.Text
    If sh > 0 Then
        wb = ""
        ksb = Mid(zfc, ActiveDocument.Fields(coun).Code.Fields(1).Code.Start - ActiveDocument.Fields(coun).Code.Start, 1)
        jis = Mid(zfc, ActiveDocument.Fields(coun).Code.Fields(1).Code.End - ActiveDocument.Fields(coun).Code.Start + 1, 1)
        For st = 1 To sh
             wb = Replace(zfc, ActiveDocument.Fields(coun).Code.Fields(st).Code.Text, ActiveDocument.Fields(coun).Code.Fields(st).Result)
          Next st
          wb = Replace(wb, ksb, "")
          wb = Replace(wb, jis, "")
      Else
        wb = ActiveDocument.Fields(coun).Code.Text
    End If
    
texs = Split(Trim(Replace(UCase(wb), "ADDIN", "")), ",")
hansm = texs(0)
texs(0) = ""
hansc = Mid(Join(texs, ","), 2)
su = Application.Run(hansm, hansc)
    If coun + sh < fic Then
          typ = ActiveDocument.Fields(coun + sh + 1).Type
          stas = ActiveDocument.Fields(coun + sh + 1).Code.Start
    Else
          typ = 0
          stas = 0
    End If
           If (typ = 35 Or typ = -1) And stas = ActiveDocument.Fields(coun).Code.End + 2 Then
                      ActiveDocument.Fields(coun + sh + 1).Code.Text = " QUOTE " & """" & su & """" & " \* MERGEFORMAT"
                Else
                    Set Range1 = ActiveDocument.Range(ActiveDocument.Fields(coun).Code.End + 1, ActiveDocument.Fields(coun).Code.End + 1)
                    Set myField = ActiveDocument.Fields.Add(Range:=Range1, Type:=wdFieldEmpty, Text:=" QUOTE " & """" & su & """")
                    fic = fic + 1
                    
            End If
  
End If
Next coun
ActiveDocument.Fields.Update
End Function
Public Function choose(zf)
chooses = Split(zf, ",")
wz = Val(chooses(0))
cou = UBound(chooses)
If wz > cou Or wz = 0 Then
choose = ""
Else
choose = chooses(wz)
End If
End Function
Public Function next_record()
If ActiveDocument.MailMerge.DataSource.RecordCount > -1 Then
If ActiveDocument.MailMerge.DataSource.ActiveRecord < ActiveDocument.MailMerge.DataSource.RecordCount Then
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Else
MsgBox "已经到达末记录!"
End If
Else
MsgBox "当前文档没有附加邮件合并数据源!"
End If
update_fields
End Function
Public Function Previous_record()
If ActiveDocument.MailMerge.DataSource.RecordCount > -1 Then
If ActiveDocument.MailMerge.DataSource.ActiveRecord <> ActiveDocument.MailMerge.DataSource.FirstRecord Then
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdPreviousRecord
Else
MsgBox "已经到达首记录!"
End If
Else
MsgBox "当前文档没有附加邮件合并数据源!"
End If
update_fields
End Function
Public Function First_record()
If ActiveDocument.MailMerge.DataSource.RecordCount > -1 Then
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
Else
MsgBox "当前文档没有附加邮件合并数据源!"
End If
update_fields
End Function
Public Function Last_record()
If ActiveDocument.MailMerge.DataSource.RecordCount > -1 Then
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
Else
MsgBox "当前文档没有附加邮件合并数据源!"
End If
update_fields
End Function

TA的精华主题

TA的得分主题

发表于 2012-4-28 10:59 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2013-7-30 14:02 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
这里是自己建立了一个新域word中没有的域?

TA的精华主题

TA的得分主题

发表于 2013-7-31 09:30 | 显示全部楼层
我也被标题骗了,不是新的,word中没有的域

TA的精华主题

TA的得分主题

发表于 2013-7-31 10:45 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2018-1-13 23:38 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2025-1-12 10:08 , Processed in 0.022721 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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