wordpress hit counter
Welcome to OpenXML Developer Sign in | Join | Help

Update Custom Properties

Last post 12-13-2006, 7:38 PM by oasisman. 2 replies.
Sort Posts: Previous Next
  •  12-12-2006, 11:38 PM 962

    Update Custom Properties

    The problem is I've found the snippet to update a custom property and it works, but, the field in document.xml isn't updated and the only way is to open Word 2007 and right-click and select Update Field.

    How would one go about updating the actual fields in the word document?

    It can get really confusing, especially if the DOCPROPERTY is formatted to be partially bold as you can see below

    - <w:fldSimple w:instr="DOCPROPERTY Name \* MERGEFORMAT" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    - <w:r w:rsidR="00936A0C" w:rsidRPr="00CB0BEE">
      <w:t><<</w:t>
      </w:r>
    - <w:r w:rsidR="00936A0C" w:rsidRPr="00CB0BEE">
    - <w:rPr>
      <w:b />
      </w:rPr>
      <w:t>Name</w:t>
      </w:r>
    - <w:r w:rsidR="00936A0C" w:rsidRPr="00CB0BEE">
      <w:t>>></w:t>
      </w:r>
      </w:fldSimple>


    If the whole field is bold it is much simpler:

    - <w:fldSimple w:instr="DOCPROPERTY Name \* MERGEFORMAT" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    - <w:r w:rsidR="00936A0C" w:rsidRPr="006010D9">
    - <w:rPr>
      <w:b />
      </w:rPr>
      <w:t><<Name>></w:t>
      </w:r>
      </w:fldSimple>


    I've been able to get to the w:fldSimple node but that's about it:

    Dim nodeValue As String = String.Format(" DOCPROPERTY  {0}  \* MERGEFORMAT ", "Name")
    Dim nodeList As XmlNodeList = doc.SelectNodes(String.Format("//w:fldSimple[@w:instr='{0}']", nodeValue), nsManager)

    I still haven't managed to update it, can anyone help?
  •  12-13-2006, 3:19 AM 964 in reply to 962

    Re: Update Custom Properties

    Hi,

    I tried replicating what you are trying to do and I could successfully update the field property as well.

    Probably you might have missed  a step.

    Onec you get the node,

    1. update the property by replacing the attribute value,i.e.

    foreach (XmlNode node in nodeList)

    {

    newFormat=String.Format("PAGE \\* {0} \\* MERGEFORMAT", "Arabic")

    node.Attributes[0].Value = newFormat;

    ---

    ---

    }

    2. Update the corresponding run with data you want as field data.

       node.SelectSingleNode("w:r[2]//w:t", ns).InnerText = "1";

    3. save back the xml document in the package(as part,document.xml)

    The word documnet would contain

    <<1>>

     

  •  12-13-2006, 7:38 PM 969 in reply to 964

    Re: Update Custom Properties

    Thanks Vijayeta,

    I was so close in getting it right, I'm not that familiar with XPath expressions.

    Here is what I did in the end:


                        Dim nodeValue As String = String.Format(" DOCPROPERTY  {0}  \* MERGEFORMAT ", docpropertyName)
                        Dim nodeList As XmlNodeList = doc.SelectNodes(String.Format("//w:fldSimple[@w:instr='{0}']", nodeValue), nsManager)

                        If (nodeList IsNot Nothing) Then
                            If (nodeList.Count > 0) Then

                                Dim textNodeList As XmlNodeList
                                Dim nodeUpdated As Boolean

                                For Each node As XmlNode In nodeList
                                    textNodeList = node.SelectNodes("w:r//w:t", nsManager)

                                    nodeUpdated = False
                                    For Each textNode As XmlNode In textNodeList
                                        If (nodeUpdated) Then
                                            textNode.RemoveAll()
                                        Else
                                            textNode.InnerText = docpropertyValue
                                            nodeUpdated = True
                                        End If
                                    Next
                                Next
                            End If
                        End If


    The reason I do a RemoveAll is because if some fool half formated the field in the document there will be multiple <w:t> tags. If you update the field using Word 2007 it only uses the first <w:t> tag and removes the rest. This is what I think happens, if you make the first character of a field bold and than right-click update field, Word makes the whole field bold with the field data.

    Cheers.
View as RSS news feed in XML