Word 2007 throws an exception for interop code that manages forms, but works fine in Word 2010

I have big problems with the code, which should add watermark photos (used as office supplies) to the document before saving it in PDF format. Inserting an image in all relevant headers is not a problem. But as soon as I try to stretch the image (form) to the entire width and height of the page, Word 2007 (SP3) throws an exception. The same code works fine in Word 2010 (SP1). It doesn’t matter if I use Office 12 or Office 14 firewalls (always used with "Embed Interop Types").

The exception is the following:

System.Runtime.InteropServices.COMException (0x800A122C): Falscher Zeichnungselement-Typ fΓΌr diesen Befehl.
   at Microsoft.Office.Interop.Word.Shape.set_RelativeHorizontalSize(WdRelativeHorizontalSize prop)
   at BEKO.PDB.AuxiliaryServices.Documents.WordCreationService.AddWatermarkToHeader(HeaderFooter header, String watermarkFilePath)

I don’t know exactly what the English error message is, but the translation is something like "Invalid drawing type (or possibly form type) for this command."

The strange thing is that this is not always an error on the same interop call. If I delete the line that sets the property RelativeHorizontalSize, it fails to set another property, for example WidthRelative(with the same exception). If I add a line that sets shape.LeftRelative(to the "do not use" constant), it even fails in a line that otherwise works like shape.Top(again with the same exception).

, , - , Word 2007. SeekView , , , / .

, . . . , (headerFooter.Exists) (!headerFooter.LinkToPrevious).

private static void AddWatermarkToHeader(HeaderFooter header, string watermarkFilePath) {
   header.Range.Editors.Add(WdEditorType.wdEditorEveryone);

   Shape shape = header.Shapes.AddPicture(
      FileName: watermarkFilePath,
      LinkToFile: false,
      SaveWithDocument: true
   );

   shape.WrapFormat.AllowOverlap = (int)MsoTriState.msoTrue;
   shape.WrapFormat.Type = WdWrapType.wdWrapNone;

   shape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
   shape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
   shape.Left = 0;
   shape.Top = 0;

   shape.RelativeHorizontalSize = WdRelativeHorizontalSize.wdRelativeHorizontalSizePage;
   shape.RelativeVerticalSize = WdRelativeVerticalSize.wdRelativeVerticalSizePage;
   shape.WidthRelative = 100;
   shape.HeightRelative = 100;

   shape.ZOrder(MsoZOrderCmd.msoSendBehindText);
}

, , Word 2007, Word 2010.

+5
2

, Word , ? , . (.. ?).

shape.Width = page.Width;
shape.Height = page.Height;
+1

Word 97 Word 2003 Word, WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage WdRelativeVerticalPosition.wdRelativeVerticalPositionPage , Word 100%. , Word 2007 . ( VBA), :

Points-returned-by-Information-wdHorizontalPositionSubroutines

Word 97 wdHorizontalPositionRelativeToPage

( ), 100% . (, , Word , . , Active Document / .)

0

All Articles