5

I need help with adding a tab stop(position at 1cm) to a word document using word interop and c#. This is what i tried already.

Range range = paragraph.Range;
int firstTabStart = range .Start;
range .SetRange(firstTabStart, firstTabStart);
range .Paragraphs.TabStops.Add(5, WdTabAlignment.wdAlignTabRight);

When i open my word document i dont see any tab stops. I can however insert tab alignments using

range .InsertAlignmentTab((int)WdAlignmentTabAlignment.wdCenter,
    (int)WdAlignmentTabRelative.wdMargin);

Although, these tabs are absolute and I cannot edit them in the word document.

Please help.

user2390368
  • 101
  • 2
  • 9
  • 2
    Google says that 1cm = 28 points. Doesn't exactly solve your problem, but FYI. – klugerama Jun 07 '13 at 15:41
  • 1
    Or you could use `application.CentimetersToPoints(2f)` (on your Word application object), or `MillimetersToPoints`, `InchesToPoints` etc. – joshuahealy Jun 25 '13 at 21:53

1 Answers1

2

I'm not able to reproduce the issue you're having, but I'm pasting the code I tested with so you can see if it differs any way from your existing code.

I saw tab stops appear in the ruler at 1 & 2 cm in every case:

  • Using either a .doc or .docx
  • Using paragraphs.TabStops instead of range.Paragraphs.TabStops
  • Using a blank document
  • Using a document with 1 or more paragraphs
  • Passing in 3rd argument for WdTabLeader in the TabStops.Add method.

And this was done in Word 2010

class Start
{
    public static void Main()
    {
        // Open a doc file.
        Application application = new Application();
        Document document = application.Documents.Open(@"C:\Users\mmonkan\Documents\word.docx");

        Paragraphs paragraphs = document.Paragraphs;
        Paragraph paragraph = paragraphs[1];
        Range range = paragraph.Range;
        range.SetRange(0, 0);

        range.Paragraphs.TabStops.Add(28, WdTabAlignment.wdAlignTabRight);
        range.Paragraphs.TabStops.Add(56, WdTabAlignment.wdAlignTabRight);

        // Close word.
        application.Quit(WdSaveOptions.wdSaveChanges);

        Console.ReadLine();
    }
}

enter image description here

Matthew Steven Monkan
  • 8,170
  • 4
  • 53
  • 71