none
UpdateFieldsOnOpen not working RRS feed

  • Question

  • Hello.  I am trying to have a Word doc pop up a dialog box to update fields when being opened. 

    I am using this code:

    DocumentSettingsPart settingsPart = wordDoc.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First(); 
    
    
    
    // Create object to update fields on open 
    
    UpdateFieldsOnOpen updateFields = new UpdateFieldsOnOpen(); 
    
    
    
    updateFields.Val = new DocumentFormat.OpenXml.OnOffValue(true); 
    
    
    
    // Insert object into settings part. 
    
    settingsPart.Settings.PrependChild<UpdateFieldsOnOpen>(updateFields); 
    
    
    
    settingsPart.Settings.Save();
    
    

    But it just creates an invalid document.  Supposedly this code should work, but it does not. Is there something I am missing?

     I am receiving this validation error from the Open XML tool:

    The element has unexpected child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:updateFields'.

    Thanks,

    Greg

    Tuesday, February 15, 2011 4:03 PM

Answers

  • Hi Greg,

    Thank you for posting and we are glad to help with you.

    >>But it just creates an invalid document.  Supposedly this code should work, but it does not. Is there something I am missing?

    From this blog article: http://www.samuraiprogrammer.com/blog/2010/08/09/OpenXMLHowToRefreshAFieldWhenTheDocumentIsOpened.aspx, I think your code is right and should work well in Word 2010. However, the article mentions that in Word 2007, it is need to clear out the contents of the filed before the user opens the document. Is it a Word 2007 document? If so, the issue may be caused by it.

    I hope it can help you and feel free to follow up after you have tried the suggestions in the article.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Bruce Song Wednesday, February 16, 2011 5:51 AM typo
    • Marked as answer by Bruce Song Friday, March 4, 2011 3:43 AM
    Wednesday, February 16, 2011 3:47 AM

All replies

  • Hi Greg,

    Thank you for posting and we are glad to help with you.

    >>But it just creates an invalid document.  Supposedly this code should work, but it does not. Is there something I am missing?

    From this blog article: http://www.samuraiprogrammer.com/blog/2010/08/09/OpenXMLHowToRefreshAFieldWhenTheDocumentIsOpened.aspx, I think your code is right and should work well in Word 2010. However, the article mentions that in Word 2007, it is need to clear out the contents of the filed before the user opens the document. Is it a Word 2007 document? If so, the issue may be caused by it.

    I hope it can help you and feel free to follow up after you have tried the suggestions in the article.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Bruce Song Wednesday, February 16, 2011 5:51 AM typo
    • Marked as answer by Bruce Song Friday, March 4, 2011 3:43 AM
    Wednesday, February 16, 2011 3:47 AM
  •  Hi Greg,

    Have you figured out the solution to resolve your issue? Does the suggestion work for you or not? If you still show any concern on your issue, just feel free to follow up.

    Best Regards,

     


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 21, 2011 2:46 AM