7

I am developing VSTO application add-in for Word and want to make shape be always on first page on fixed position. Is there a way to do this without actively monitoring state of a shape?

Answers that state "it can't be done" with good why explanation, are also welcome.

Logman
  • 4,031
  • 1
  • 23
  • 35
  • Not really an answer but you could try enabling the header and footer info and setting the option to show something on the first page only using this ... https://support.office.com/en-gb/article/Delete-or-change-a-header-or-footer-on-a-single-page-a9b6c963-a3e1-4de1-9142-ca1be1dba7ff?ui=en-US&rs=en-GB&ad=GB&fromAR=1 ... having done that, look throug hthe markup generated in the raw doc and see what API calls you need to make to repeat that. It's a place to start looking at least, and hints that this should be possible! – War May 17 '17 at 08:50
  • @War problem with this approach is when you want to select object, as shape is not selectable until you go to header view. Also it provide multiple problems if header is already inside document. But it's a valid answer to my question. And maybe my only option in this situation. – Logman May 17 '17 at 16:37

2 Answers2

4

If you put your shape to header and check DifferentFirstPageHeaderFooter, page break do not have effect as you need, but the shape will be on background and Page Layout > Breaks > Next Page duplicate the shape to the next page.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        AddFixedShapeOnFistPage(Application.Documents.Add(System.Type.Missing), MsoAutoShapeType.msoShapeRectangle, 160, 160, 30, 30);
    }

    public void AddFixedShapeOnFistPage(Microsoft.Office.Interop.Word.Document wordDocument, Microsoft.Office.Core.MsoAutoShapeType shapeType,int left, int top, int width, int height)
    {
        int wordTrueConst = -1; //https://social.msdn.microsoft.com/Forums/office/en-US/e9f963a9-18e4-459a-a588-17824bd3906d/differentfirstpageheaderfooter-bool-or-int?forum=worddev
        wordDocument.Sections[1].PageSetup.DifferentFirstPageHeaderFooter = wordTrueConst;
        wordDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes.AddShape((int)shapeType, left, top, width, height);
    }

Shape will be on background

Denis P.
  • 302
  • 1
  • 2
  • 8
  • Please read my comment under [Somdip Dey answer](http://stackoverflow.com/a/44051459/2483065). Besides it's not really answer to my question, it has one magic number in it (shape Type). You can read more about it [here](http://stackoverflow.com/q/47882/2483065). – Logman May 18 '17 at 20:56
  • Yes this answers my question. One problem is that shape is not selectable on normal editing in this case but this was not part of my question. Second is that shape can be moved from original position but this I can handled by unselect shape if it's selected (it's not 100% correct but it's enough). 3rd problem is to correctly modify header if header already exists inside document. If no one give answer that better suits my needs I will give you my bounty and for now +1 for correct solution. Thx for your time on this topic. – Logman May 19 '17 at 17:57
  • 1 and 2 problem can be solved if you enter in head edit mode (just double click on header) – Denis P. May 20 '17 at 04:57
  • 3 problem you just add the shape and do not harm anything else in your header. – Denis P. May 20 '17 at 06:43
1

Yes, it is achievable. The code as follows:

using Word = Microsoft.Office.Interop.Word;

public void DrawShape()
{
try{
    var wordApp = new Word.Application();
    wordApp.Documents.Add(System.Type.Missing);
    Word.Document doc = wordApp.ActiveDocument;
    var shape = doc.Shapes.AddShape((int)Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 20, 20, 60, 20);
   }
   catch(Exception ex) { }
}

The aforementioned code draws a rectangle of width: 60, height: 20 at position (20, 20) in the top left corner position of the document's first page. Keep in mind, (0,0) is the beginning point from the top left corner of the first page of the Doc file.

Here, Shapes.AddShape should do the trick.

Shape AddShape(int Type, float Left, float Top, float Width, float Height, ref object Anchor = Type.Missing);

More on SHapes.AddShape(): https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shapes.addshape.aspx

And for different types of shapes refer to MsoAutoShapeType: https://msdn.microsoft.com/en-us/library/microsoft.office.core.msoautoshapetype.aspx

Somdip Dey
  • 3,346
  • 6
  • 28
  • 60
  • I don't want to be ungrateful but exactly how this answer resolve my problem? I already known how to insert a shape into a document but when I insert page break before shape anchor it will move to next page and this is my real problem. – Logman May 18 '17 at 20:43
  • i just used this code: var wordApp = new Word.Application(); wordApp.Visible = true; wordApp.Documents.Add(System.Type.Missing); Word.Document doc = wordApp.ActiveDocument; doc.Content.Text = "This is some content for the word document.\nI page break HERE "; /*insert a page break*/ doc.Words.Last.InsertBreak(Word.WdBreakType.wdPageBreak); var shape = doc.Shapes.AddShape(1, 20, 20, 40, 60); – Somdip Dey May 19 '17 at 08:30
  • and the shape still came in the first page at the right position. Am I missing something here? Any more specification regarding your requirement? – Somdip Dey May 19 '17 at 08:34
  • Quick answer: use Ctrl + Enter (break page) as a first char inside word editor. You are missing multiple things here like how word is anchoring shapes by default. When you add shape to document you need to provide paragraph where shape will be anchored (or use default one). Anchor is always added at the beginning of the paragraph. It means If you add some text, shape will be anchored before that text. But if you create new paragraph before paragraph with shape (for ex. by typing Enter or Ctrl + Enter at **beginning** of the paragraph) shape will move with paragraph to new page. – Logman May 19 '17 at 16:54