none
Update fields in text box in the document header RRS feed

  • Question

  • Hi,

    I have problems with updating fields in text boxes in the header

    I use OpenXml SDK to manipulate a Word Document.

    After I made some changes, I use word interop to update the fields.

    This looks like this:

     

     

    foreach (Range myStoryRange in document.StoryRanges)
    {
        myStoryRange.Fields.Update();
    }
    When there are text boxes in the header, and there are fields inside the text boxes, they won’t be updated by this code.

    With this code, it works:

     

     

    foreach (Range myStoryRange in document.StoryRanges)
    {
        myStoryRange.Fields.Update();
        if (myStoryRange.StoryType == WdStoryType.wdEvenPagesHeaderStory ||
            myStoryRange.StoryType == WdStoryType.wdPrimaryHeaderStory ||
            myStoryRange.StoryType == WdStoryType.wdEvenPagesFooterStory ||
            myStoryRange.StoryType == WdStoryType.wdPrimaryFooterStory ||
            myStoryRange.StoryType == WdStoryType.wdFirstPageHeaderStory ||
            myStoryRange.StoryType == WdStoryType.wdFirstPageFooterStory)
    
            try
            {
                foreach (Shape shape in myStoryRange.ShapeRange)
                {
                    if (shape.TextFrame.HasText != 0)
                        shape.TextFrame.TextRange.Fields.Update();
                }
            }
            catch (Exception)
            {
            }
    }
     But with this code I got exceptions, if there is an image in the header.  I got COMException

    0x80010105 (RPC_E_SERVERFAULT))

    Word 2007 only throws an exception, Word 2010 crashes.

     

    My test document looks like this:

    (in the Header is a field, a text box with a field and an image)

     

     

    Peter

    • Edited by Fanski Wednesday, April 25, 2012 7:19 AM
    Wednesday, April 25, 2012 7:17 AM

Answers

  • Hi Peter

    Sorry it took so long to test this, things here have been BUSY...

    I confirm. The problem appears to lie in the PIAs. I tried removing the embedding of the Word objects in the project to see if that made any difference, but it did not.

    This appears to be a bug in the .NET PIAs for Word, since it works fine in VBA.

    There's no way to report this kind of problem on Connect, so you'd need to open a paid support incident. When it's confirmed as a bug the charges should be refunded.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Fanski Wednesday, May 16, 2012 9:52 AM
    Monday, May 14, 2012 7:54 AM
    Moderator

All replies

  • Hi Fanski

    I suspect the problem is that a "normal" graphic does not have a TextFrame. You don't indicate which line is triggering the exception, but if my guess is correct, it would be this one: if (shape.TextFrame.HasText != 0)

    If that is the case, then before this line you need to test shape.Type, which returns a value from the Enum msoShapeType. If you explicitly want to check text boxes, then test for msoTextBox.

    (Other types of Shapes, created using the Office drawing tools, can also have TextFrames, but I'm assuming you don't require those in this scenario.)


    Cindy Meister, VSTO/Word MVP

    Wednesday, April 25, 2012 8:13 AM
    Moderator
  • Hi Cindy,

    thank you for your answer.
    It is the line with the myStoryRange.ShapeRange property.
    As soon as I try to use this property there will be an exception (crash)

    The image I inserted by copy and paste.

    Best Regards
    Peter
    Wednesday, April 25, 2012 12:48 PM
  • Hi Peter

    You're pasting the image between generating the document using Open XML and running your code?

    Any chance you could post this document somewhere so that I can look at it and do some testing? Like on Skydrive? I find it odd that the ShapeRange property would have this effect...


    Cindy Meister, VSTO/Word MVP

    Wednesday, April 25, 2012 5:24 PM
    Moderator
  • Hi Cindy,

    here a test document on Skydrive

    It doesn’t matter if I insert the image in my code with the open xml sdk, or if I copy & past an image using word.

    Best Regards

    Peter

    • Edited by Fanski Thursday, April 26, 2012 10:02 AM
    Thursday, April 26, 2012 10:01 AM
  • Hi Peter

    I downloaded your file incorporated your code into a C# test add-in I have, ran it...

    And had no problems. It ran fine with no errors (Word 2007).

    I then put the code into a C# WinForms test app, opened the document in Word, ran the code... And again I had no problems.


    Cindy Meister, VSTO/Word MVP

    Sunday, April 29, 2012 7:15 AM
    Moderator
  • Hi Cindy,

    here is a VS 2010 Project.

    Using Word 2010 it crashes, with word 2007 there is an Exception.

    Best Regards

    Peter



    • Edited by Fanski Wednesday, May 2, 2012 4:12 PM
    Wednesday, May 2, 2012 3:39 PM
  • Hi Cindy

    Could you reproduce it?

    Best Regards

    Peter Stefanski

    Monday, May 7, 2012 8:06 AM
  • Hi Peter

    Sorry it took so long to test this, things here have been BUSY...

    I confirm. The problem appears to lie in the PIAs. I tried removing the embedding of the Word objects in the project to see if that made any difference, but it did not.

    This appears to be a bug in the .NET PIAs for Word, since it works fine in VBA.

    There's no way to report this kind of problem on Connect, so you'd need to open a paid support incident. When it's confirmed as a bug the charges should be refunded.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Fanski Wednesday, May 16, 2012 9:52 AM
    Monday, May 14, 2012 7:54 AM
    Moderator
  • Hi Cindy,

    thank you very much.

    Good to know that it’s a bug and not my fault :)

    Maybe I will report.

    Best Regards

    Peter Stefanski

     

    Wednesday, May 16, 2012 9:51 AM