locked
Copying document content with DocVariable or DocProperty fields RRS feed

  • Question

  • Hi All,

    I have a couple of templates with a userform that stores user entered content in custom document properties.  The document has DocProperty fields that then display the information in the body of the document.  Naturally, if the user changed content, when the document is closed and then reopened the DocProperty fields update and revert to whatever was stored in them.  More importantly, if content is copied from the document to another (with the DocProperty field in tact), then the text "Invalid Document Property" is displayed.  That makes good sense because the property doesn't exist in the destination document.

    Needless to say, I need to modify these templates to meet the following conditions:
    * User entered content needs to become static content so that when it is copied it doesn't change.
    * When content is later modified by the user it can't revert to information stored.

    The two ways I have seen this done before is using either the DocVariable field or using bookmarks and having the form replace the bookmark with user entered content.

    Does anyone have recommendations about the best way to insert data entered by a user (via a userform) into a document?

    Thanks in advance.


    Stephen, RedGalaxy Darwin, Australia

    Friday, December 14, 2012 1:42 AM

Answers

  • Hi Steven,

    There was no misunderstanding on my part - none of what I posted has anything to do with formfields. Field locking/unlocking applies to DOCPROPERTY fields, REF fields, DATE fields and others. Since you're using a userform to populate the custom document properties, I imagine that somewhere in your code is a line like:
    ActiveDocument.Fields.Update
    If, before that line you insert a line like:
    ActiveDocument.Fields.Locked = False
    and, immediatly before the sub ends, a line like:
    ActiveDocument.Fields.Locked = True
    or, if you want to restrict the locking to the DOCPROPERTY fields, code like:
    Dim Fld As Field
    With ActiveDocument
      For Each Fld In .Fields
        If Fld.Type = wdFieldDocProperty Then Fld.Locked = True
      Next
    End With
    your users should be able to copy the DOCPROPERTY fields to another document without them being likely to update (just don't teach the users to use Ctrl-Shift-F11!).


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Friday, December 14, 2012 8:24 PM Adjusted lock/unlock sequencing
    • Marked as answer by Quist Zhang Monday, December 24, 2012 9:13 AM
    Friday, December 14, 2012 6:25 AM
  • Hi Stephen

    >> I haven't found a list anywhere of fields that do and do not auto update.<<

    I'm not aware of a definitive list, but here is some information that may still apply.

    http://support.microsoft.com/?kbid=89953
    http://support.microsoft.com/?kbid=832897
    http://support.microsoft.com/?kbid=330079

    There's also this:

    http://msdn.microsoft.com/en-us/library/office/ff837289.aspx


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Quist Zhang Monday, December 24, 2012 9:14 AM
    Friday, December 14, 2012 1:12 PM

All replies

  • Hi Stephen,

    Your specifications are to some extent mutually exclusive - if you convert a field to static text it ceases to be a field and can no longer be updated as one. The updating of the fields in any other document to which they are copied can be prevented by converting them to static text in the destination document. This can be done in code or via Ctrl-Shift-F9.

    An alternative is to lock the fields (which you can do in code or via Ctrl-F11) after updating them, then unlock them again (which you can do in code or via Ctrl-Shift- F11) before the next update.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, December 14, 2012 5:30 AM
  • Hi Paul,

    I think there is a little misunderstanding.  The document I have doesn't use Text Form Fields that are found in the Toolbox or on the Developer Tab in 2007/2010.  It uses fields (as in Insert, Field... or Insert, Quick Part, Field...).

    In that case, the fields are never converted to plain text.  As such, when the user copies content from the document to a new document, the fields are pasted in tact.

    I have been doing some more testing and discovered that some fields automatically update when the document is opened (such as the "DocProperty" field) while others do not (such as the "DocVariable" field).  I haven't found a list anywhere of fields that do and do not auto update.  But it seems if text is copied with a DocVariable field, it pastes along with its last known value quite successfully.  But if the document is updated using CTRL-A, F9 then content disappears.

    While the CTRL-SHIFT-F9 shortcut is useful in this situation, I am trying to work out a way to make this possible for users who are not skilled enough with Word to know such keystrokes. :( Ergh! Users!


    Stephen, RedGalaxy Darwin, Australia

    Friday, December 14, 2012 5:44 AM
  • Hi Steven,

    There was no misunderstanding on my part - none of what I posted has anything to do with formfields. Field locking/unlocking applies to DOCPROPERTY fields, REF fields, DATE fields and others. Since you're using a userform to populate the custom document properties, I imagine that somewhere in your code is a line like:
    ActiveDocument.Fields.Update
    If, before that line you insert a line like:
    ActiveDocument.Fields.Locked = False
    and, immediatly before the sub ends, a line like:
    ActiveDocument.Fields.Locked = True
    or, if you want to restrict the locking to the DOCPROPERTY fields, code like:
    Dim Fld As Field
    With ActiveDocument
      For Each Fld In .Fields
        If Fld.Type = wdFieldDocProperty Then Fld.Locked = True
      Next
    End With
    your users should be able to copy the DOCPROPERTY fields to another document without them being likely to update (just don't teach the users to use Ctrl-Shift-F11!).


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Friday, December 14, 2012 8:24 PM Adjusted lock/unlock sequencing
    • Marked as answer by Quist Zhang Monday, December 24, 2012 9:13 AM
    Friday, December 14, 2012 6:25 AM
  • Hi Stephen

    >> I haven't found a list anywhere of fields that do and do not auto update.<<

    I'm not aware of a definitive list, but here is some information that may still apply.

    http://support.microsoft.com/?kbid=89953
    http://support.microsoft.com/?kbid=832897
    http://support.microsoft.com/?kbid=330079

    There's also this:

    http://msdn.microsoft.com/en-us/library/office/ff837289.aspx


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Quist Zhang Monday, December 24, 2012 9:14 AM
    Friday, December 14, 2012 1:12 PM