Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3052
  • Last Modified:

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

0
sstops
Asked:
sstops
  • 3
  • 2
1 Solution
 
irudykCommented:
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.
0
 
sstopsAuthor Commented:
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

0
 
irudykCommented:
Hmm, well given the error you indicated (which I was able to replicate), I found that if you select the shape first and the delete the selection, the image will be deleted and the error does not happen.  So try the following revised code (you might need to tweak it since my C# is a bit rusty)
            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;
 
            If (thisSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes.Count == 1)
            {
                thisSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes(1).Select;
                this.Application.ActiveDocument.Selection.Delete;
            }
 
            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;
            headerShapes.Add(shapeHeadbackground);
            headerInlineshapes.Add(iShapeHeadbackground);

Open in new window

0
 
irudykCommented:
Just curious to know if the above code modifications worked out for you?
0
 
sstopsAuthor Commented:
Thank you. Sorry for delayed answer today was the first day I could try.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now