Bra Utbildning AB

 

<-- Back
VBA & Object models
Work with text
Bookmarks
Ranges
Fields
Form fields
Autotext
Doc var & Doc Prop
Built-in commands
Built-in dialogs
Automation
External databases

Work with document variables and document properties


Document variables

Update variable and create the variable if it does not exist
ActiveDocument.Variables("SemiHiddenInfo").Value = "any text or number"

Verify if a certain document variable exists

If DoesDocVarXist("TheVariablesName") = True Then
  
MsgBox "Sure - it exists!"
Else
   MsgBox "Nope - this was a new one!"
End If

'=============================================
'This fucnction is called from the procedure above
Private Function DoesDocVarXist(VarName As String) As Boolean
On Error GoTo ErrorHandler
   DoesDocVarXist = _
      (VarName = _
      ActiveDocument.Variables(VarName).Name)

'Regular exit here
TheExit:
Exit Function
'---------------------------
ErrorHandler:
Err.Clear
Resume TheExit
End Function

Document properties

Simple update of properties - not completely safe/does not always update
ActiveDocument.BuiltInDocumentProperties(wdPropertyAuthor) = "Your name here"

Update the document properties
With Dialogs(wdDialogFileSummaryInfo)
   .Title = "The title here"
   .Author = "Your name here"
   .Execute
End With

Verify if the document property exists. If it does find out if it is a built-in or a custom property. If neither add a new custom property to the document. The solution is based upon 3 procedure below (1 sub routine and 2 functions). There is no possibility to simply check the existence of a document variable or a document property the way you can with bookmark. This is the reason for the code on this page.

Sub ThePropertyCheckPoint()
Dim oDoc As Document
Dim strControl As String
Dim strValue As String

Set oDoc = ActiveDocument

strControl = "ClientBirthDay"
strValue = "June 1"

If DoesBuiltInPropXist(strControl) = True Then
      oDoc.BuiltInDocumentProperties(strControl) = strValue
Else
   If DoesCustomPropXist(strControl) = False Then
      oDoc.CustomDocumentProperties.Add _
         Name:=strControl, _
         LinkToContent:=False, _
         Value:="<empty>", _
         Type:=msoPropertyTypeString
   End If

   If Not strValue = "" Then
      oDoc.CustomDocumentProperties(strControl).Value = _
         strValue
   End If
End If
End Sub


'=============================================
Private Function DoesBuiltInPropXist(DocPropName As String) As Boolean
On Error GoTo ErrorHandler

   If StrComp(ActiveDocument.BuiltInDocumentProperties(DocPropName).Name, _
      DocPropName, vbTextCompare) = 0 Then
      DoesBuiltInPropXist = True
End If

'Regular exit
TheExit:
Exit Function
'------------------------
ErrorHandler:
Err.Clear
Resume TheExit
End Function

'=============================================
Private Function DoesCustomPropXist(DocPropName As String) As Boolean
On Error GoTo ErrorHandler
   DoesCustomPropXist = _
      (DocPropName = _
      ActiveDocument.CustomDocumentProperties(DocPropName).Name)

'Regular exit
TheExit:
Exit Function
'---------------------------
ErrorHandler:
Err.Clear
Resume TheExit
End Function