Making a Shape top-most
Asked Answered
J

2

7

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)?

Joyajoyan answered 28/9, 2016 at 19:42 Comment(0)
J
8

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.

Joyajoyan answered 5/10, 2016 at 18:59 Comment(0)
L
4

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.

Laundes answered 4/10, 2016 at 20:40 Comment(9)
I will say this as well, even in canonical books such as the following, the writer explicitly states how 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.: safaribooksonline.com/library/view/writing-word-macros/… Your answer does not work, nor is it a canonical answer.Joyajoyan
@Joyajoyan Perhaps your link actually answers your question, because Z-order is meaningless without multiple objects to be part of the Z-order. Unless your shape is the last one to receive a Z-order command, there does not seem to be any way built in to the interop to override this. No doubt this is by design. Good luck finding a canonical answer, since your own links show something different than what you want.Laundes
This statement is purely your interpretation of the snippet I linked: Unless your shape is the last one to receive a Z-order command, there does not seem to be any way built in to the interop to override this. Nowhere does it say this in the snippet. It does hint that it won't set the absolute Z-order, and it does say it works relative to other objects. There is a reason why it hints this, and I want to know what that reason it, but trust me its not your interpolation of what it could be. Definitely templates introduce objects that appear over top of mine. Respect the question please.Joyajoyan
@Joyajoyan It should be self-evident that sets the z-order of a Shape object relative to other objects means that the last z-order set has precedence. The link is not hinting that you can do it any other way - rather, it says you cannot set z-order any other way. Note what follows the z-order can be set only in the following ways: I was trying to be a help - but you do not seem too interested in help.Laundes
It does mean the last Z-order set has precedence, but obviously, we just tried setting the last Z-order using your suggestions of shape.ZOrder(MsoZOrderCmd.msoBringInFrontOfText); and shape.ZOrder(MsoZOrderCmd.msoBringToFront); and we have found that it does not do anything. So, I think whats missing is something to do with the relativity of shapes here. For example, some shapes might be in different sections, perhaps. So how would you set the Z ordering for them?Joyajoyan
You're right in that I don't seem too interested in your help at this point because you seem to think your assumptions are all correct...but, to be fair...I do appreciate your help. I appreciate it when anyone tries to help out. Once people become too stubborn is when I draw the line, though. Further, I will add that you can see this works in Office within Word itself, so presumably there is a way to do it.Joyajoyan
@Joyajoyan I am just reading the link that you provided. Sorry that you seem to understand it differently.Laundes
My shape is the last to receive the Z ordering commands in your answer, and yet its not top-most. So, what do you want from me? Your answer does not work!Joyajoyan
Read my post...I found the cause. If you don't believe me, you can try for yourself. Z-ordering is, as I guessed, relative to the section you are in.Joyajoyan

© 2022 - 2024 — McMap. All rights reserved.