Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a tab stop to a paragraph range in c#

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.

like image 700
user2390368 Avatar asked Nov 03 '22 21:11

user2390368


1 Answers

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

like image 150
Matthew Steven Monkan Avatar answered Nov 11 '22 08:11

Matthew Steven Monkan