Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    Jan 2001
    Location
    UK, Surrey
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA Word 2010 convert document variable to text

    I've set up some template (.dotm) letters using an autonew macro and userforms to collect data (entered by the user), which is then inserted in the document in the correct places.

    Sooner or later somebody's going to try to edit the data - they'll decide to change the addressee's name or something - which will no doubt cause problems.

    I can see two possible solutions. One would be, when people click on the text which is actually a document variable, to have the document pop up a form inviting them to change and re-save the variables. I don't know how to do this.

    The other solution might be simpler - I hope it's easy when you know how. This would be to convert all the document variables in the document to text before saving the document.

    I can't see how to do this - does anybody have any pointers, please?

    Peter.

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,503
    Thanks
    3
    Thanked 141 Times in 134 Posts
    Have you inserted the document variables into the document using Content Controls or fields?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    Star Lounger
    Join Date
    Jan 2001
    Location
    UK, Surrey
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I used fields. It seemed the easiest way...

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,503
    Thanks
    3
    Thanked 141 Times in 134 Posts
    In that case, you can simply unlink the fields. Depending on your other content this might be massive overkill
    ActiveDocument.Fields.Unlink

    If you need a more elegant solution (which leaves your TOC and xref fields alone for instance) then you can query each field in the document and unlink just the ones which trigger your test. To get you started, something like this would be a starting point.
    Code:
    Dim aFld As Field
    For Each aFld In ActiveDocument.Fields
      If aFld.Type = wdFieldDocVariable Then aFld.Unlink
    Next aFld
    This simple code would need extra complications:
    - to count backwards since you are removing fields while stepping through them
    - to also do the same for each document story (eg header and footers)

    Let us know if this approach would work for you and we can point you in the direction to solve the remaining issues.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #5
    Star Lounger
    Join Date
    Jan 2001
    Location
    UK, Surrey
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, Andrew. Sorry not to have replied sooner - I've had to park this project for a while - those look like a really useful approach.

    Presumably I could set [a] bookmark[s] for the part[s] of the letter I want to unlink the text from fields, select them, unlink the fields in them, and then remove the bookmarks...

    When I get a chance to revisit this I'll have a go at that.

    Peter.

  7. #6
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    195
    Thanks
    4
    Thanked 45 Times in 37 Posts
    Quote Originally Posted by penglish View Post
    Presumably I could set [a] bookmark[s] for the part[s] of the letter I want to unlink the text from fields, select them, unlink the fields in them, and then remove the bookmarks...

    When I get a chance to revisit this I'll have a go at that.
    When you get a round tuit remember that you don't have to select a part of the document in order to unlink its fields. You can just do something like this, where the part in quotes is a bookmark's name (which could be a string variable, in which case don't use quotes):

    Code:
    ActiveDocument.Bookmarks("FirstLocation").Range.Fields.Unlink
    ActiveDocument.Bookmarks("FirstLocation").Delete

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
  •