Consulting

Results 1 to 2 of 2

Thread: Word 2016 Change Document Properties via Field, VBA, or Both…

  1. #1
    VBAX Regular
    Joined
    Feb 2017
    Posts
    10
    Location

    Word 2016 Change Document Properties via Field, VBA, or Both…

    I can insert DOCPROPERTY and DOCVARIABLE fields into a Word 2016 document through VBA, from within Word and periodically Excel (based upon data in an Excel file). These are for various Word templates in which the data will be stored in the built-in and custom document properties when created, but will need to be modified when the documents are created from the templates for document specific information not applicable to the templates.

    However, what I need to do is to insert the DocProperty fields and be able to update the document property from the field, for both the built-on and custom document properties … instead of manually modifying them through File-Info-Properties-Advanced Properties.

    ContentControls do not seem to be the answer as you cannot uses them for custom document properties, in my limited experience … unless there is another way to use them that I am not aware of.

    It seems that while you can place the field into Word, you cannot update the original document property with the new field value. A right-click on update only refreshes the original document property into the “read-only” field, that is, from what I have succeeded in so far.

    Is there a way to accomplish viewing the Document Properties and updating the original Document Properties from the field?

    Thank you.

  2. #2
    DOCPROPERTY and DOCVARIABLE fields simply reflect the stored values. It is not a two way process.

    Thinking laterally, you could use a text content control to display the value e.g. lets assume you have a content control with its name and property tag set as 'Name' and you have a docVariable called 'varName' containing a default name. By using the On enter and on exit events for the content control you can fill the control with the value from the variable and if you change the content, the variable will be updated with the change.

    A Custom (or built-in) DocProperty would work in a similar manner to the docvariable, as also shown for the custom property PropNum and the content control tagged "Number". The code goes in the ThisDocument module of the document. Note that not all built-in document properties are editable.

    You might find http://www.gmayor.com/BookmarkandVariableEditor.htm useful if only to create the initial variables/docproperties.

    Option Explicit
    
    Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
        Select Case ContentControl.Tag
            Case Is = "Name"
                ContentControl.Range.Text = ActiveDocument.Variables("varName").Value
            Case Is = "Number"
                ContentControl.Range.Text = ActiveDocument.CustomDocumentProperties("PropNum").Value
            Case Else
        End Select
    End Sub
    
    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
        Select Case ContentControl.Tag
            Case Is = "Name"
                If ContentControl.ShowingPlaceholderText = False Then
                    If Not ActiveDocument.Variables("varName").Value = ContentControl.Range.Text Then
                        ActiveDocument.Variables("varName").Value = ContentControl.Range.Text
                    End If
                End If
            Case Is = "Number"
                If ContentControl.ShowingPlaceholderText = False Then
                    If Not ActiveDocument.CustomDocumentProperties("PropNum").Value = ContentControl.Range.Text Then
                        ActiveDocument.CustomDocumentProperties("PropNum").Value = ContentControl.Range.Text
                    End If
                End If
            Case Else
        End Select
    End Sub
    In practice, you would probably want to write the variable value to the control using the document open event or in the case of a template, the document new event.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •