Add an image to a Word Document / Size problem

Add-in Express™ Support Service
That's what is more important than anything else

Add an image to a Word Document / Size problem
 
Tournoux Thomas




Posts: 24
Joined: 2013-04-16
Hi,

not sure this is the correct place to ask this, but...
I have an addin for word, who can add an image in a word document.
It works fine whith Word 2013 and a .doc document, but with a .docx, the image isn't resize like in the .doc
The image must be in the background of the document, and full size.
the code is
Microsoft.Office.Interop.Word.Document myWordDocument = Globals.ThisAddIn.Application.ActiveDocument ;
myWordDocument.Activate();
Microsoft.Office.Interop.Word.Range myRange = Globals.ThisAddIn.Application.Selection.Range.GoTo(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage, Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage, 1);
Microsoft.Office.Interop.Word.Shape myShape = myWordDocument.Shapes.AddPicture(pImageEntete, false, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, myRange);
myShape.ScaleHeight(1, Microsoft.Office.Core.MsoTriState.msoTrue);
myShape.ScaleWidth(1, Microsoft.Office.Core.MsoTriState.msoTrue);
myShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapTight;
myShape.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
myShape.Left = 0;
myShape.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
myShape.Top = 0;
myShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
myShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
myShape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBackward);


Any help ?

Regards.
Posted 25 Apr, 2013 08:35:02 Top
Andrei Smolin


Add-in Express team


Posts: 16991
Joined: 2006-05-11
Hello Thomas,

This works fine for me:

Word.Document myWordDocument = WordApp.ActiveDocument;
myWordDocument.Activate();
object missing = Type.Missing;
Word.Range myRange = WordApp.Selection.Range.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToItem.wdGoToPage, 2);
Word.Shape myShape = myWordDocument.Shapes.AddPicture(@"d:2-06-08_125730.png", false, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, myRange);
myShape.WrapFormat.Type = Word.WdWrapType.wdWrapTight;
myShape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
myShape.Left = 0;
myShape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
myShape.Top = 0;
myShape.ScaleHeight((float)0.5, Office.MsoTriState.msoTrue);
myShape.ScaleWidth((float)0.5, Office.MsoTriState.msoTrue);
myShape.LockAspectRatio = Office.MsoTriState.msoTrue;
myShape.WrapFormat.Type = Word.WdWrapType.wdWrapBehind;
myShape.ZOrder(Office.MsoZOrderCmd.msoSendBackward);


Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 26 Apr, 2013 01:27:58 Top
Tournoux Thomas




Posts: 24
Joined: 2013-04-16
Hi, it still doesn't work with your code, but now, work fine with .doc and .docx with this :

Microsoft.Office.Interop.Word.Document myWordDocument = Globals.ThisAddIn.Application.ActiveDocument ; 
myWordDocument.Activate(); 
Microsoft.Office.Interop.Word.Range myRange = Globals.ThisAddIn.Application.Selection.Range.GoTo(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage, Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage, 1); 
Microsoft.Office.Interop.Word.Shape myShape = myWordDocument.Shapes.AddPicture(pImageEntete, false, true, 0, 0, myWordDocument.Application.CentimetersToPoints((float)21), myWordDocument.Application.CentimetersToPoints((float)29.7), myRange); 
//myShape.ScaleHeight(1, Microsoft.Office.Core.MsoTriState.msoTrue); 
//myShape.ScaleWidth(1, Microsoft.Office.Core.MsoTriState.msoTrue); 
myShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapTight; 
myShape.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage; 
myShape.Left = 0; 
myShape.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage; 
myShape.Top = 0; 
//myShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue; 
myShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind; 
myShape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBackward); 



I think some properties override some other, 'cause on the right clic on the image --> "size & position", some properties aren't save like I "save" them with the code.

Regards.
Posted 26 Apr, 2013 03:53:40 Top
Andrei Smolin


Add-in Express team


Posts: 16991
Joined: 2006-05-11
Yes, this is possible. I've run into this sort of issues with Word (not with Word shapes, however).

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 26 Apr, 2013 04:46:45 Top