Link to home
Start Free TrialLog in
Avatar of sstops
sstopsFlag for Germany

asked on

VSTO / Word 2007 Adding / Removing Shape from Header causes error

Hello, I have written a small Word add-in in C# that adds two buttons in a new ribbon. Depending on the button it adds a different image to the header and footer of the current document. That works just fine if I run it for the first time. If I re-run the procedure, for example to change from one set of images to another it causes an exception: "System.AccessViolationException was unhandled by user code
  Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
  Source="Microsoft.Office.Interop.Word"
  StackTrace:
       at Microsoft.Office.Interop.Word.Shape.Apply()
       at SNJFaxAddIn.SNJFaxAddIn.InsertSNJSFirstPageHeader() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SNJFaxAddIn\SNJFaxAddIn\SNJFaxAddIn.cs:line 340
       at SNJFaxAddIn.SNJFaxAddIn.InsertSNJSStationary() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SNJFaxAddIn\SNJFaxAddIn\SNJFaxAddIn.cs:line 247
       at SNJFaxAddIn.snjRibbon.btnSNJS_Click(Object sender, RibbonControlEventArgs e) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SNJFaxAddIn\SNJFaxAddIn\snjRibbon.cs:line 33
       at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ControlActionRaise(IRibbonControl control)
       at Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ButtonClickCallback(RibbonComponent component, Object[] args)
       at Microsoft.Office.Tools.Ribbon.RibbonManager.Invoke(RibbonComponentCallback callback, Object[] args)
       at Microsoft.Office.Tools.Ribbon.RibbonMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at Microsoft.Office.Tools.Ribbon.RibbonManager.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
  InnerException:

Originally I wrote a Word-VBA Macros but it caused the same problems.
I tried using both shape and inlineshape objects but it changed nothing.
I also added code to remove the images from the previous execution before adding new ones, but this didn't help either. I added the core code fragments below.

I trying to find a solution for days and found some information on the web but no one seems to offer a solution.
object oMissing = Type.Missing;
            object LinkedToFile = false;
            object SaveWithDocument = true;
            object left;
            object top;
            float width;
            object height;
            object range;
            Word.Section thisSection = this.Application.ActiveDocument.Sections.First;
 
 
            this.Application.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = -1;
 
            oMissing = Type.Missing;
            left = this.Application.ActiveDocument.PageSetup.LeftMargin * -1;
            top = this.Application.ActiveDocument.PageSetup.TopMargin * -1;
            width = this.Application.ActiveDocument.PageSetup.PageWidth;
 
            range = thisSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
            Word.InlineShape iShapeHeadbackground;
            try
            {
                iShapeHeadbackground =
                    this.Application.Selection.InlineShapes.AddPicture(
                    imagePath + "abc.jpg", ref LinkedToFile, ref SaveWithDocument,
                     ref range);
            }
            catch
            {
            }
 
            Word.Shape shapeHeadbackground = iShapeHeadbackground.ConvertToShape();
            shapeHeadbackground.Left = (float)left;
            shapeHeadbackground.Top = (float)top;
            shapeHeadbackground.Name = "HeaderFirst";
            shapeHeadbackground.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBehindText);
            shapeHeadbackground.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
            shapeHeadbackground.Width = width;
            shapeHeadbackground.Apply();  // here it crashed
            headerShapes.Add(shapeHeadbackground);
            headerInlineshapes.Add(iShapeHeadbackground);

Open in new window

Avatar of irudyk
irudyk
Flag of Canada image

Have you tried running the code without the line:
     shapeHeadbackground.Apply();
From the Help in Word:

Shape.Apply Method
Applies to the specified shape formatting that has been copied using the PickUp method.  If formatting for the Shape object has not previously been copied using the PickUp method, using the Apply method generates an error.
Since you are not inserting an image and not copying the formatting of an existing image using the Pickup method, the use of Apply would seem unnecessary.
Avatar of sstops

ASKER

Thank you, that works. But the old image remain on although I used the same name. I also tried to enumerate through the collections and delete them but this did not work either.

Do you have any idea.

Thank you
Sascha

ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just curious to know if the above code modifications worked out for you?
Avatar of sstops

ASKER

Thank you. Sorry for delayed answer today was the first day I could try.