none
AddPicture in header works in Word 2010 but not Word 2007. RRS feed

  • Question

  • Hello All,

    I'm in a real pinch again, I keep hitting hurdle after hurdle.
    The following code works in Word 2010 but fails with "The index into the specified collection is out of bounds." on first run when the document is first opened.  But if I run it again after the initial call it works??!!  Please help I'm banging my head against the wall and I desperately need this to work.

     

     public static void ReplaceHeaderSeal(Word.Application app, string replacementPath)
      {
       foreach (Section section in app.ActiveDocument.Sections)
       {
        // Word 2007 does something strange with the layout and that is why we set Left and Top twice.
        Word.Shape replacement = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes.AddPicture(replacementPath,
         missing, true, 36, 36, 72, 72, missing);
    
        replacement.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
        replacement.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
        replacement.Left = 36;
        replacement.Top = 36;
       }
      }

    The document is .docx made in Word 2007.


     Thank you,

    Nick Metnik


    Friday, June 17, 2011 4:00 PM

All replies

  • Hi Nick

    Could you provide more detail about how the code is being called "when the document is first opened"? Is this being triggered by a DocumentOpen event, for example? Something else?

    Which line of code is giving the error?

    My guess at this point is that the code is being called before the document is fully opened and "ready to go", so the Headers aren't "there" yet. You might need to build in a delay (a loop with DoEvents, for example) to give Word time to completely load the document.


    Cindy Meister, VSTO/Word MVP
    Sunday, June 19, 2011 1:23 PM
    Moderator
  • Hello Cindy,

    Thank you for your help.

    1.  After I accept my custom windows form dialog with (Word running, the dialog is kicked off from a button on my custom ribbon tab)...

    2.  I open a document like so:  
    current = WordApp.Documents.Open(ref path, missing, no, no, missing, missing, missing, missing, missing, missing, missing, yes, missing, missing, missing, missing);

    3.  I prompt the user to save the document after I do some document manipulation:   
    current.SaveAs(savePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

    4.  I then call my code above to add the picture to the document.  Like I said though, 2010 has no problem doing it all.

    I think your guess is in alignment with mine regarding that the document object may not be completely initialized or something until the call is complete.  I threw a    System.Windows.Forms.Application.DoEvents(); before my code and I still get the error.

    The line it bombs on is: 
    Word.Shape replacement = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes.AddPicture(replacementPath, missing, true, 36, 36, 72, 72, missing);

    I'm going to pursue any event related to "Document Saved" if it exists and try to stuff my code there.

    Please let me know if you get any other ideas, I certainly appreciate any help I can get!

    Thank you Cindy,

    Nick Metnik

    Monday, June 20, 2011 12:33 AM
  • Nick,

    I'd split that code line to check if the probelm occurs in Headers[...] or in Shapes.AddPicture(...).


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Monday, June 20, 2011 1:07 PM
  • Hello Andrei,

    Thank you for your advice and for Add-in Express (I'm a happy customer BTW) :) 
    The header and shapes checked out fine, after further examination the AddPicture was causing the problem some how.
    The thread from here put me on the right path:  http://social.msdn.microsoft.com/forums/en-US/vsto/thread/0fd3039c-0fde-4b35-a366-abc414998c33/

    My final WORKING code looks like this:

    public static void ReplaceHeaderSeal(Word.Application app, string replacementPath)
      {
       foreach (Section section in app.ActiveDocument.Sections)
       {
        // Word 2007 does something strange with the layout and that is why we set Left and Top twice.
        HeaderFooter header = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage];
        if (app.Version == "12.0")
        {
         Range range = header.Range;
         Word.InlineShape inlineShape = app.ActiveDocument.InlineShapes.AddPicture(replacementPath, false, true, range);
         Word.Shape shape = inlineShape.ConvertToShape();
         shape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
         shape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
         shape.Left = 36;
         shape.Top = 36;
         shape.Width = 72;
         shape.Height = 72;
        }
        else
        {
         Word.Shape replacement = header.Shapes.AddPicture(replacementPath, missing, true, 36, 36, 72, 72, missing);     
         replacement.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
         replacement.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
         replacement.Left = 36;
         replacement.Top = 36;
        }
       }
      }
    

    I can't explain why it works, it just does?!

     

    Thanks again all for your responses, I really appreciate your help!

    Take care,

     

    Nick Metnik

     


    Monday, June 20, 2011 3:32 PM
  • Hello,
    I guess this isn't answered too early.
    The image gets replace but Word eventually locks up and I get the following error "Attempted to read or write protected memory" which stems from that call.
    It's really strange.
    Thank you,
    Nick
    Friday, June 24, 2011 7:24 PM