none
How to add text vertically into a Word margin with C# (using namespace: Microsoft.Office.Interop.Word) RRS feed

  • Question

  • I need to add text vertically in a word document outside the margins.  How can I do this with Microsoft.Office.Interop.Word and C#?

    Leonard Swarczinski Software Developer Postal Center International

    Thursday, July 18, 2013 7:43 PM

Answers

  • Hi Leonard,

    According to your description, do you want to add text vertically into Page Header/Footer? I wrote a sample  for you.

    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AddTextToWord
    {
        class Program
        {
            static void Main(string[] args)
            {
                CreateNewDocument();
                Console.ReadLine();
            }
    
            private static void CreateNewDocument()
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Application oWord;
                Microsoft.Office.Interop.Word.Document oDoc;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = true;
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                String HeaderText = "Hello everyone!";
                WdParagraphAlignment wdAlign = WdParagraphAlignment.wdAlignParagraphCenter;
                AddHeader1(oWord, HeaderText, wdAlign);
            }
    
            private static void AddHeader1(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)
            {
                Object oMissing = System.Reflection.Missing.Value;
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                Microsoft.Office.Interop.Word.Shape textBox = WordApp.ActiveDocument.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationVertical, 150, 10, 40, 40);
                textBox.TextFrame.TextRange.Text = HeaderText;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
            }
        }
    }
    

    If I misunderstood or anything wrong, please let me know and you can get more information from below articles.

    Office development in Visual Studio

    http://msdn.microsoft.com/en-us/office/hh133430.aspx

    Abhout: AddTextbox Method

    http://msdn.microsoft.com/en-us/library/office/aa171543(v=office.11).aspx

    How to: Programmatically Insert Text into Word Documents

    http://msdn.microsoft.com/en-us/library/vstudio/6b9478cs.aspx

    Friday, July 19, 2013 11:40 AM
    Moderator

All replies

  • You'll be working with a vertically oriented text box and you have to place it into the HeaderFooter Object of Word.

    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, July 18, 2013 9:59 PM
  • Hi Leonard,

    According to your description, do you want to add text vertically into Page Header/Footer? I wrote a sample  for you.

    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AddTextToWord
    {
        class Program
        {
            static void Main(string[] args)
            {
                CreateNewDocument();
                Console.ReadLine();
            }
    
            private static void CreateNewDocument()
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Application oWord;
                Microsoft.Office.Interop.Word.Document oDoc;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = true;
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                String HeaderText = "Hello everyone!";
                WdParagraphAlignment wdAlign = WdParagraphAlignment.wdAlignParagraphCenter;
                AddHeader1(oWord, HeaderText, wdAlign);
            }
    
            private static void AddHeader1(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)
            {
                Object oMissing = System.Reflection.Missing.Value;
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                Microsoft.Office.Interop.Word.Shape textBox = WordApp.ActiveDocument.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationVertical, 150, 10, 40, 40);
                textBox.TextFrame.TextRange.Text = HeaderText;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
            }
        }
    }
    

    If I misunderstood or anything wrong, please let me know and you can get more information from below articles.

    Office development in Visual Studio

    http://msdn.microsoft.com/en-us/office/hh133430.aspx

    Abhout: AddTextbox Method

    http://msdn.microsoft.com/en-us/library/office/aa171543(v=office.11).aspx

    How to: Programmatically Insert Text into Word Documents

    http://msdn.microsoft.com/en-us/library/vstudio/6b9478cs.aspx

    Friday, July 19, 2013 11:40 AM
    Moderator
  • I would like to put something outside the margin on the side, not top or bottom. Is that possible?

    More detail:

    I'm replicating a company document with a logo starting against the side of the page next to the header.

    I can put the logo anywhere inside the header. I can even get it "against the side" in print layout by setting zero left margin. BUT then I seem stuck with zero left margin for the whole page.

    I've been looking for a way to select the picture object and set it's top left corner at an offset from the top left page corner.


    Zero degrees + fifteen years of ambitious fumbling = works for me


    • Edited by KevinMacKay Friday, February 6, 2015 8:23 PM
    Friday, February 6, 2015 8:20 PM
  • Kevin,

    You first need to make sure your inserted image is floating and not an inline shape and then you need to do something like this:

    Sub SetPosition()
        With ActiveDocument.Shapes(1)
         .Left = InchesToPoints(0)
         .RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage
         .Top = InchesToPoints(0)
         .RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage
        End With
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, February 6, 2015 8:51 PM