sstops
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.AccessViolationExc eption 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.I nterop.Wor d"
StackTrace:
at Microsoft.Office.Interop.W ord.Shape. Apply()
at SNJFaxAddIn.SNJFaxAddIn.In sertSNJSFi rstPageHea der() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SNJFaxAddIn\ SNJFaxAddI n\SNJFaxAd dIn.cs:lin e 340
at SNJFaxAddIn.SNJFaxAddIn.In sertSNJSSt ationary() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SNJFaxAddIn\ SNJFaxAddI n\SNJFaxAd dIn.cs:lin e 247
at SNJFaxAddIn.snjRibbon.btnS NJS_Click( Object sender, RibbonControlEventArgs e) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SNJFaxAddIn\ SNJFaxAddI n\snjRibbo n.cs:line 33
at Microsoft.Office.Tools.Rib bon.Ribbon PropertySt orage.Cont rolActionR aise(IRibb onControl control)
at Microsoft.Office.Tools.Rib bon.Ribbon PropertySt orage.Butt onClickCal lback(Ribb onComponen t component, Object[] args)
at Microsoft.Office.Tools.Rib bon.Ribbon Manager.In voke(Ribbo nComponent Callback callback, Object[] args)
at Microsoft.Office.Tools.Rib bon.Ribbon MethodInfo .Invoke(Ob ject obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Office.Tools.Rib bon.Ribbon Manager.Sy stem.Refle ction.IRef lect.Invok eMember(St ring 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.
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="Microsoft.Office.I
StackTrace:
at Microsoft.Office.Interop.W
at SNJFaxAddIn.SNJFaxAddIn.In
at SNJFaxAddIn.SNJFaxAddIn.In
at SNJFaxAddIn.snjRibbon.btnS
at Microsoft.Office.Tools.Rib
at Microsoft.Office.Tools.Rib
at Microsoft.Office.Tools.Rib
at Microsoft.Office.Tools.Rib
at Microsoft.Office.Tools.Rib
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);
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
Do you have any idea.
Thank you
Sascha
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just curious to know if the above code modifications worked out for you?
ASKER
Thank you. Sorry for delayed answer today was the first day I could try.
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.