View Single Post
 
Old 03-30-2015, 02:41 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,143
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You could test the macro against the name of the document (or save the document as DOCX format and it will not have a macro).

In the case of the former, let's assume a docvariable field that displays the docvariable "varDate", which to demonstrate has a value that is the current time.

The following macro saved in the ThisDocument module will only run if the document containing the macro is called 'docname.docm'

Code:
Option Explicit

Private Sub Document_Open()
Dim oFld As Field
    If ThisDocument.Name = "docname.docm" Then
        For Each oFld In ThisDocument.Fields
            If oFld.Type = wdFieldDocVariable Then
                If InStr(1, oFld.Code, "varDate") > 0 Then
                    oFld.Locked = False
                    ThisDocument.Variables("varDate").Value = Format(Time, "hh:mm:ss")
                    oFld.Update
                    oFld.Locked = True
                    Exit For
                End If
            End If
        Next oFld
    End If
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote