Creating the best shape

In my Word add-in, I have a Word Document object that contains a specific Section . In this Section I add Shape :

 var shape = section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "Example text...", "Calibri", 72, MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0, section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range) as Shape; 

My problem is that some Word document templates have images or other things that appear on top of my figure. Initially, I thought that setting the order of Z would be enough to fix this:

 shape.ZOrder(MsoZOrderCmd.msoBringToFront); 

This is not true. So my question is: how can I completely set the Z order of my Shape , or, in other words, what else do I need to do to set, to make my Shape so that it becomes the topmost part you see in the document (which means that it appears primarily from other things)?

+7
c # ms-word office-addins word-addins
source share
2 answers

I finally understood why these methods do not work:

 shape.ZOrder(MsoZOrderCmd.msoBringInFrontOfText); shape.ZOrder(MsoZOrderCmd.msoBringToFront); 

The problem was that I added a Shape object to the HeaderFooter section, but the shape displayed on top of it was defined in the Document . Z-ordering only applies to other forms within the same section where your object is located (regardless of whether your object is in the actual document, header, footer, etc.).

So instead of this code, add the form to a specific section:

 var shape = section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "Example text...", "Calibri", 72, MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0, section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range) as Shape; shape.ZOrder(MsoZOrderCmd.msoBringInFrontOfText); shape.ZOrder(MsoZOrderCmd.msoBringToFront); 

I used this code to add it to my document directly, and then apply the Z-order to it, and it really worked. It appeared above all the other objects that were part of my template:

 var shape = document.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "Example text...", "Calibri", 72, MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0) as Shape; shape.ZOrder(MsoZOrderCmd.msoBringToFront); 

Writing macros in Word, the second edition states this very clearly:

The ZOrder method sets the z-order of the Shape object relative to another object. Note that the method does not establish an absolute z-order.

Thus, the absolute Z-order depends on other factors, for example, where the Shape is in this case.

+8
source share

When doing this manually in Word, I select the option "Forward before text." You must try:

shape.ZOrder(MsoZOrderCmd.msoBringInFrontOfText);

If this one does not work due to other objects, try using one after the other:

 shape.ZOrder(MsoZOrderCmd.msoBringInFrontOfText); shape.ZOrder(MsoZOrderCmd.msoBringToFront); 

The reason for this is that MS Word, apparently, treats the text and other objects as having different Z-orders.

+4
source share

All Articles