Search code examples
c#ms-wordoffice-interop

How do I insert a tab character into a Word document with Word Interop?


The question says it all. I am attempting to insert a tab character into my Word document which is generated with Word interop. The usual \t does not work.

I literally just want to insert some text (tab) text (tab) text; so I am not talking about tab indents at the beginning of a line, but within text itself.

Thanks


Solution

  • Have you tried this:

    wordApp.oWord.ActiveWindow.Selection.TypeText("Your custom text");
    WordApp.oWord.ActiveWindow.Selection.TypeText("\t");
    wordApp.oWord.ActiveWindow.Selection.TypeText("Your next custom text");
    WordApp.oWord.ActiveWindow.Selection.TypeText("\t");
    

    or if you just want to go to the end of the document:

    object missing = Missing.Value;
    object what = Word.WdGoToItem.wdGoToLine;
    object which = Word.WdGoToDirection.wdGoToLast;
    doc.GoTo(ref what, ref which, ref missing, ref missing);
    

    credits to Alexander Kojevnikov