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.
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:
paragraphs.TabStops
instead of range.Paragraphs.TabStops
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With