none
InsertXml and Document Variables RRS feed

  • Question

  • Hello,

    Using World 2010 I have exported all autotext out of the word template (about 600+) into an sql database. The content is exported as XML. The idea is that during a data binding process, the VBA code will get XML from the database and insert it into the document. This works fine.

    The problem is that the exported word content has document variables as in:

    { DOCVARIABLE "ThisIsATestVariable" \* MERGEFORMAT}

    These are exported as xml docVariable elements:

    <w:docVar w:name="ThisIsATestVariable" w:val="Hello World"/>

    When I insert the XML into the document during the binding process the document variables don't get inserted into the document (ActiveDocument.Variables.Count = 0). What I would like to do is include all the document variables and then modify the value of those variables after the insertion of the XML as in: ActiveDocument.Variables("ThisIsATestVariable").Value = "new value".

    Any help would be appreciated.

    Thanks in advance.

    Friday, September 21, 2012 2:27 AM

Answers

  • Hi Tom

    If this is the XML stored in the database (and I'm unsure, as you say you've picked it up from ActiveDocument.Content.XML), then there are no fields stored in the database. There must have been some problem when transferring the content from AutoText to the database? How was that done?


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Tom Rakewell Wednesday, September 26, 2012 4:54 AM
    Tuesday, September 25, 2012 8:19 AM
    Moderator
  • Hi Cindy,

    Thanks for all your help. Looking at the xml I also noticed that the fields were not present. I did a test exporting content as xml with fields to a text file then reinserting it into the document. The fields were indeed there when i did it that way and I also saw it in the exported xml. It looks like the function that was exporting to the sql database was running this naughty function:

    Public Sub UnlinkBinderFields()
        Dim docfld As Word.Field
    
        For Each docfld In m_Document.Fields
            If docfld.Type = wdFieldDocVariable Then
                docfld.Unlink
            End If
        Next docfld
    End Sub
    This was the reason why i wasn't seeing any fields. All working now, thanks again.

    Wednesday, September 26, 2012 4:53 AM

All replies

  • Hi Tom

    OK, I think I follow, but we have to differentiate the terminology a little bit to make sure we're talking about the same things :-)

    This - { DOCVARIABLE "ThisIsATestVariable" \* MERGEFORMAT} - is a field code; for a DocVariable field, to be exact. It dynamically reflects the content of a document variable stored in the document.

    For fairly obvious reasons, the document variable information cannot/will not be stored in an AutoText entry as it's not stored in that part of the document.

    So, yes, you'd have to create the document variables in the document, not just set their values. Unless, of course, these documents are all being generated from a common template, or set of templates, where you could create the document variables?

    You can certainly do that, but not as part of the InsertXML action (either before or after).

    Are you basically asking how would you find out the names of the document variables in the field codes inserted from the database?

    You would need to "read" the XML string you're getting from the database using an XML parser to extract the required information. VBA doesn't have this capability built-in, you'd need to set a reference to an external library. Since Word 2003, the MSXML parser (version 3.6, I believe) has been part of the Office installation and should be available through Tools/References in the VBA Editor.

    Do you have any experience with parsing XML?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 21, 2012 8:01 AM
    Moderator
  • Hi Cindy,

    Thanks for taking to the time to reply and being so detailed with your explanation.

    You are correct about the variable information not being stored with the autotext content. I actually insert the document variables and their values into the document before I insert the xml. I know beforehand what fields each autotext will use because they are linked to a datasource in the sql database.

    The problem is that the after the InsertXml function is executed, the fields in the resulting content doesn't take on the new values for the document variables I have set beforehand. In fact, the autotext content, after it is inserted, no longer contains the linked fields. Pressing ALT+F9 doesn't reveal the field code nor does it display with a grey highlighted background as other fields do (the shaded in grey option is set to always for fields in preferences). It seems that the InsertXML function either ignores the docVariable elements or perhaps there is another way to do it.

    I have have some experience with XML parsing but I would rather let word do the work if possible.

    Thanks,

    Saturday, September 22, 2012 11:52 AM
  • Hi Tom

    Could you share the raw XML for an entry that is relatively small, but still contains a field, that InsertXML is passing into the Word document, please?

    (And please pre-format it with indents and use the Code tool in the messge toolbar to paste it into your reply so as it will otherwise be practically impossible to read.)

    Also, is this raw XML exactly the same as what's stored in the database, or is it being transformed in someway before being passed into InsertXML?


    Cindy Meister, VSTO/Word MVP, my blog


    Sunday, September 23, 2012 10:31 AM
    Moderator
  • Hi Cindy,

    Below is the output I get from the following using ActiveDocument.Content.Xml. This raw text is stored in the database as is without any transformation.

    <?xml version="1.0" standalone="yes" ?>
    <?mso-application progid="Word.Document" ?>
    <w:worddocument xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
        xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml"
        xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
        xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
        xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
        w:macrospresent="no" w:embeddedobjpresent="no" w:ocxpresent="no" xml:space="preserve">
        <w:ignoresubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2" />
        <o:documentproperties>
            <o:version>14</o:version>
        </o:documentproperties>
        <w:fonts>
            <w:defaultfonts w:ascii="Times New Roman" w:fareast="Times New Roman" w:h-ansi="Times New Roman"
                w:cs="Times New Roman" />
            <w:font w:name="Times New Roman">
                <w:panose-1 w:val="02020603050405020304" />
                <w:charset w:val="00" />
                <w:family w:val="Roman" />
                <w:pitch w:val="variable" />
                <w:sig w:usb-0="E0002AFF" w:usb-1="C0007841" w:usb-2="00000009" w:usb-3="00000000"
                    w:csb-0="000001FF" w:csb-1="00000000" />
            </w:font>
            <w:font w:name="Times New Roman">
                <w:panose-1 w:val="02020603050405020304" />
                <w:charset w:val="00" />
                <w:family w:val="Roman" />
                <w:pitch w:val="variable" />
                <w:sig w:usb-0="E0002AFF" w:usb-1="C0007841" w:usb-2="00000009" w:usb-3="00000000"
                    w:csb-0="000001FF" w:csb-1="00000000" />
            </w:font>
        </w:fonts>
        <w:styles>
            <w:versionofbuiltinstylenames w:val="7" />
            <w:latentstyles w:deflockedstate="off" w:latentstylecount="267">
                <w:lsdexception w:name="Normal" />
                <w:lsdexception w:name="heading 1" />
                <w:lsdexception w:name="heading 2" />
                <w:lsdexception w:name="heading 3" />
                <w:lsdexception w:name="heading 4" />
                <w:lsdexception w:name="heading 5" />
                <w:lsdexception w:name="heading 6" />
                <w:lsdexception w:name="heading 7" />
                <w:lsdexception w:name="heading 8" />
                <w:lsdexception w:name="heading 9" />
             </w:latentstyles>
            <w:style w:type="paragraph" w:default="on" w:styleid="Normal">
                <w:name w:val="Normal" />
                <w:rpr>
                    <wx:font wx:val="Times New Roman" />
                    <w:sz w:val="24" />
                    <w:sz-cs w:val="24" />
                    <w:lang w:val="EN-AU" w:fareast="EN-AU" w:bidi="AR-SA" />
                </w:rpr>
            </w:style>
            <w:style w:type="character" w:default="on" w:styleid="DefaultParagraphFont">
                <w:name w:val="Default Paragraph Font" />
            </w:style>
            <w:style w:type="table" w:default="on" w:styleid="TableNormal">
                <w:name w:val="Normal Table" />
                <wx:uiname wx:val="Table Normal" />
                <w:rpr>
                    <wx:font wx:val="Times New Roman" />
                    <w:lang w:val="EN-AU" w:fareast="EN-AU" w:bidi="AR-SA" />
                </w:rpr>
                <w:tblpr>
                    <w:tblind w:w="0" w:type="dxa" />
                    <w:tblcellmar>
                        <w:top w:w="0" w:type="dxa" />
                        <w:left w:w="108" w:type="dxa" />
                        <w:bottom w:w="0" w:type="dxa" />
                        <w:right w:w="108" w:type="dxa" />
                    </w:tblcellmar>
                </w:tblpr>
            </w:style>
            <w:style w:type="list" w:default="on" w:styleid="NoList">
                <w:name w:val="No List" />
            </w:style>
        </w:styles>
        <w:shapedefaults>
            <o:shapedefaults v:ext="edit" spidmax="1026" />
            <o:shapelayout v:ext="edit">
                <o:idmap v:ext="edit" data="1" />
            </o:shapelayout>
        </w:shapedefaults>
        <w:docpr>
            <w:view w:val="print" />
            <w:zoom w:percent="130" />
            <w:stylepaneformatfilter w:val="3F01" />
            <w:defaulttabstop w:val="720" />
            <w:punctuationkerning />
            <w:characterspacingcontrol w:val="DontCompress" />
            <w:optimizeforbrowser />
            <w:allowpng />
            <w:validateagainstschema />
            <w:saveinvalidxml w:val="off" />
            <w:ignoremixedcontent w:val="off" />
            <w:alwaysshowplaceholdertext w:val="off" />
            <w:compat>
                <w:breakwrappedtables />
                <w:snaptogridincell />
                <w:wraptextwithpunct />
                <w:useasianbreakrules />
                <w:dontgrowautofit />
            </w:compat>
            <w:docvars>
                <w:docvar w:name="ThisIsATestVariable" w:val="Hello World" />
            </w:docvars>
        </w:docpr>
        <w:body>
            <wx:sect>
                <w:p wsp:rsidr="0072654C" wsp:rsidrdefault="0072654C" wsp:rsidp="0072654C">
                    <w:ppr>
                        <w:rpr>
                            <w:u w:val="single" />
                        </w:rpr>
                    </w:ppr>
                    <aml:annotation aml:id="0" w:type="Word.Bookmark.Start" w:name="_GoBack" />
                    <aml:annotation aml:id="0" w:type="Word.Bookmark.End" />
                    <w:r>
                        <w:rpr>
                            <w:u w:val="single" />
                        </w:rpr>
                        <w:t>Here is the field: </w:t>
                    </w:r>
                </w:p>
                <w:p wsp:rsidr="0072654C" wsp:rsidrpr="009F7254" wsp:rsidrdefault="0072654C" wsp:rsidp="0072654C">
                    <w:ppr>
                        <w:rpr>
                            <w:u w:val="single" />
                        </w:rpr>
                    </w:ppr>
                    <w:r>
                        <w:rpr>
                            <w:u w:val="single" />
                        </w:rpr>
                        <w:t>1</w:t>
                    </w:r>
                </w:p>
                <w:p wsp:rsidr="00907B41" wsp:rsidrdefault="00907B41" />
                <w:sectpr wsp:rsidr="00907B41">
                    <w:pgsz w:w="11906" w:h="16838" />
                    <w:pgmar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="708"
                        w:footer="708" w:gutter="0" />
                    <w:cols w:space="708" />
                    <w:docgrid w:line-pitch="360" />
                </w:sectpr>
            </wx:sect>
        </w:body>
    </w:worddocument>
    

    Monday, September 24, 2012 3:44 AM
  • Hi Tom

    If this is the XML stored in the database (and I'm unsure, as you say you've picked it up from ActiveDocument.Content.XML), then there are no fields stored in the database. There must have been some problem when transferring the content from AutoText to the database? How was that done?


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Tom Rakewell Wednesday, September 26, 2012 4:54 AM
    Tuesday, September 25, 2012 8:19 AM
    Moderator
  • Hi Cindy,

    Thanks for all your help. Looking at the xml I also noticed that the fields were not present. I did a test exporting content as xml with fields to a text file then reinserting it into the document. The fields were indeed there when i did it that way and I also saw it in the exported xml. It looks like the function that was exporting to the sql database was running this naughty function:

    Public Sub UnlinkBinderFields()
        Dim docfld As Word.Field
    
        For Each docfld In m_Document.Fields
            If docfld.Type = wdFieldDocVariable Then
                docfld.Unlink
            End If
        Next docfld
    End Sub
    This was the reason why i wasn't seeing any fields. All working now, thanks again.

    Wednesday, September 26, 2012 4:53 AM
  • Hi Tom

    Glad you were able to track it down :-) Yes, that's a very naughty function!


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 26, 2012 8:35 AM
    Moderator