• VBA Word 2010 convert document variable to text

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » VBA Word 2010 convert document variable to text

    Author
    Topic
    #491218

    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.

    Viewing 2 reply threads
    Author
    Replies
    • #1414878

      Have you inserted the document variables into the document using Content Controls or fields?

    • #1415485

      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.

    • #1416493

      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 for the part 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.

      • #1416534

        Presumably I could set [a] bookmark for the part 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
    Viewing 2 reply threads
    Reply To: VBA Word 2010 convert document variable to text

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: