Word 使用宏根据文件名实现文件版本号自动更新_rev01
来源:互联网 发布:城市与文明游戏 知乎 编辑:程序博客网 时间:2024/04/30 11:41
因工作需要,需要经常更新文档内容。每次更新后文件版本号等信息都需要一一更新,过于繁琐且容易遗漏。因为更新部分有统一的格式,考虑利用Word文件的自定义属性和域功能实现自动更新这些常规内容。
主要包括:文档名,版本号(在文件名中有统一命名规则),文档更新日期,更新人员,核查人员等。通过文件自定义属性/域/宏结合使用,可以很好的实现文件版本号自动更新的问题。
Modules代码:
Option ExplicitPublic posIssueNo As ByteSub Update()'' NS_New Macro' Macro created 03/08/2012 by Vico Song' '''''''''''''''''''''''''''''''''''''''''''' ''' Define variables '''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''' Dim vProperties, vDefaultPropertyValue, vAuthors, vCheckers, v As Variant Dim i, lenDocName As Integer Dim b As Boolean Dim s As String '''''''''''''''''''''''''''''''''''''''''''' ''' Initial variables ''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''' vProperties = Array("_DocuName", _ "_IssueNumber", _ "_Prepared/Modified", _ "_Checked/Released", _ "_UpdateDate") vDefaultPropertyValue = Array("DD***xXXXXExx", _ "xx", _ "_AUTHOR_", _ "_CHECKER_", _ Date) ''' Add author to the list below vAuthors = Array("Luke Wang", _ "Vico Song") ''' add checker to the list below vCheckers = Array("Jason Li", _ "Kino Zhang", _ "Larry Yu", _ "Luke Wang", _ "Sean Lee") ''' Check the document name lenDocName = Len(ActiveDocument.Name) If lenDocName < 21 Then MsgBox ("E01: The file name is not defined by NEW or COPE numbering system!") Exit Sub End If If UCase(Left(ActiveDocument.Name, 2)) = "DD" And _ UCase(Mid(ActiveDocument.Name, 9, 1)) = "E" And _ IsNumeric(Mid(ActiveDocument.Name, 10, 2)) And _ IsNumeric(Mid(ActiveDocument.Name, 13, 2)) Then posIssueNo = 13 ''' Numbering System NEW, file name like DDT10208E00_02-EN.doc ElseIf UCase(Left(ActiveDocument.Name, 2)) = "DD" And _ UCase(Mid(ActiveDocument.Name, 11, 1)) = "E" And _ IsNumeric(Mid(ActiveDocument.Name, 12, 2)) And _ IsNumeric(Mid(ActiveDocument.Name, 15, 2)) Then posIssueNo = 15 ''' Numbering System COPE, file name like DDDEP10402E13_21-EN.doc Else MsgBox ("E02: The file name is not defined by NEW or COPE numbering system!") Exit Sub End If '''''''''''''''''''''''''''''''''''''''''''' ''' Check and create document properties ''' '''''''''''''''''''''''''''''''''''''''''''' If UBound(vProperties) = UBound(vDefaultPropertyValue) Then For i = 0 To UBound(vProperties) Call CreateProperty(vProperties(i), vDefaultPropertyValue(i)) Next Else MsgBox ("Error!!!" & vbCrLf & vbCrLf & "Please check VBA code!" & vbCrLf & vbCrLf & _ "Tip: UBound(vProperties) != UBound(vDefaultPropertyValue)") Exit Sub End If '''''''''''''''''''''''''''''''''''''''''''' ''' Initial components of frm_docInput ''' '''''''''''''''''''''''''''''''''''''''''''' b = False s = ActiveDocument.CustomDocumentProperties("_Prepared/Modified") frm_docInput.cmb_Author.Clear For Each v In vAuthors frm_docInput.cmb_Author.AddItem (v) ''' set current default property value as combo box default value If v = s Then frm_docInput.cmb_Author.Value = v b = True End If Next ''' If current value of document property does not exist in the list, ''' add it into list and set it as combo box default value If Not b Then frm_docInput.cmb_Author.AddItem (s) frm_docInput.cmb_Author.Value = s End If b = False s = ActiveDocument.CustomDocumentProperties("_Checked/Released") frm_docInput.cmb_Checker.Clear For Each v In vCheckers frm_docInput.cmb_Checker.AddItem (v) ''' set current default property value as combo box default value If v = s Then frm_docInput.cmb_Checker.Value = v b = True End If Next ''' If current value of document property does not exist in the list, ''' add it into list and set it as combo box default value If Not b Then frm_docInput.cmb_Checker.AddItem (s) frm_docInput.cmb_Checker.Value = s End If s = ActiveDocument.CustomDocumentProperties("_UpdateDate") v = Date frm_docInput.cmb_Date.Clear If Not v = s Then frm_docInput.cmb_Date.AddItem (v) frm_docInput.cmb_Date.AddItem (s) frm_docInput.cmb_Date.Value = s frm_docInput.txt_docNo = Left(ActiveDocument.Name, posIssueNo - 2) frm_docInput.txt_issueNo = Mid(ActiveDocument.Name, posIssueNo, 2) frm_docInput.ShowEnd SubPublic Sub CreateProperty(ByVal sPropertyName As String, ByVal sDefaultValue As String)'' CreateProperty Macro' Macro created 03/08/2012 by Vico Song' Dim bExist As Boolean Dim p As DocumentProperty bExist = False ''' Check all existed properties, if current property already exist For Each p In ActiveDocument.CustomDocumentProperties If p.Name = sPropertyName Then bExist = True Next ''' If current property does not exist, create. If Not bExist Then ActiveDocument.CustomDocumentProperties.Add _ Name:=sPropertyName, LinkToContent:=False, Value:=sDefaultValue, _ Type:=msoPropertyTypeString End IfEnd Sub
窗体frm_docInput代码:
Option ExplicitPrivate Sub btn_Cancel_Click() Unload MeEnd SubPrivate Sub btn_OK_Click() ActiveDocument.CustomDocumentProperties("_DocuName") = frm_docInput.txt_docNo.Value ActiveDocument.CustomDocumentProperties("_IssueNumber") = frm_docInput.txt_issueNo.Value ActiveDocument.CustomDocumentProperties("_Prepared/Modified") = frm_docInput.cmb_Author.Value ActiveDocument.CustomDocumentProperties("_Checked/Released") = frm_docInput.cmb_Checker.Value ActiveDocument.CustomDocumentProperties("_UpdateDate") = frm_docInput.cmb_Date.Value ActiveDocument.StoryRanges(wdFirstPageFooterStory).Fields.Update ActiveDocument.StoryRanges(wdEvenPagesHeaderStory).Fields.Update ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Fields.Update Unload MeEnd Sub
窗体界面:
- Word 使用宏根据文件名实现文件版本号自动更新_rev01
- Word 使用宏根据文件名实现文件版本号自动更新_rev00
- Word 使用宏根据文件名实现文件版本号自动更新_rev02
- VS 2013 编译 自动更新文件版本号
- 根据服务器检测本地版本号并自动更新资源
- Shell实现自动更新目录名称与文件中版本号信息与时间戳
- 使用 poi 根据 word 模板生成 word 文件
- 使用 poi 根据 word 模板生成 word 文件
- 根据关键字(文件名)检索文件的Java实现
- 根据文件句柄,获取文件名
- 根据文件handle获得文件名
- 根据文件句柄,获取文件名
- 根据文件名获取文件id
- spark streaming 实现根据文件内容自定义文件名,并实现文件内容追加
- 命令行SVN根据版本号查看文件路径
- 利用php内置的svn函数实现的根据svn版本号导出相关文件的功能
- 在Qt工程文件中根据不同版本号使用不同源码
- 在Qt工程文件中根据不同版本号使用不同源码
- Java工程中调用Android库出现“Stub!”错误
- micro2440 按键驱动程序
- 项目管理体系
- 切换SVN WORKING COPY的地址
- PowerShell基本语法及常用命令
- Word 使用宏根据文件名实现文件版本号自动更新_rev01
- Powershell创建文件
- js实现替换关键字链接(关键字中包含关键字)
- 暂无
- 编程语言的演化过程
- python与ruby的不同哲学观
- mciSendString("play movie",buf,sizeof(buf),NULL)
- verilog模块间传递参数
- 黑马程序员——hashCode方法解决内存泄露