Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Shape top-most

In my Word add-in, I have a Word Document object which contains a particular Section. In this Section, I append a 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 issue is that some Word document templates have images or other things that appear over top of my shape. Originally, I thought that setting the Z order would be enough to fix this:

shape.ZOrder(MsoZOrderCmd.msoBringToFront);

It did not. So my question is, how can I absolutely set the Z order of my Shape, or in other words, what else would I have to do to set in order to make my Shape such that it becomes the top-most thing you see in the document (meaning, that it appears above all of the other things)?

like image 254
Alexandru Avatar asked Sep 28 '16 19:09

Alexandru


2 Answers

I finally figured out why these methods weren't working:

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

The problem was that I added my Shape object within a HeaderFooter section, but the shape that was displaying over top of it was defined within the Document. Z-ordering is only relative to other shapes within the same section your object is in (whether your object is in the actual document, the header, footer, etc.).

So instead of this code to add the shape to a particular 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 Z-ordering to it, and it actually worked. It appeared above all of the other objects which were a 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 Word Macros, Second Edition states this perfectly clearly:

The ZOrder method sets the z-order of a Shape object relative to other objects. Note that the method does not set the absolute z-order.

Thus the absolute Z-order depends on other factors, such as where the Shape actually resides in this case.

like image 69
Alexandru Avatar answered Sep 18 '22 22:09

Alexandru


Doing this manually in Word, I select the option "Bring Forward In Front of Text." You should try:

shape.ZOrder(MsoZOrderCmd.msoBringInFrontOfText);

If this alone does not work because of other objects, try using one after the other:

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

The reason for doing this is that MS Word seems to treat Text and other objects as having different Z-Orders.

like image 40
vbnet3d Avatar answered Sep 17 '22 22:09

vbnet3d