2

I am attempting to create a word document where the line spacing is set to "No Spacing", or single spacing. I honestly don't know why this is so difficult.

I have tried all of these examples with no luck:

Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly ;
para1.LineSpacing = 1;
para1.Format.SpaceBefore = 5;
para1.Format.SpaceAfter = 0;
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe" + Environment.NewLine + "Address" + Environment.NewLine + "Anytown, AnyCity  12345";

Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle ;
para1.Format.SpaceBefore = 5;
para1.Format.SpaceAfter = 0;
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe" + Environment.NewLine + "Address" + Environment.NewLine + "Anytown, AnyCity  12345";

Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.LineSpacingRule = <tried them all> ;
para1.Format.SpaceBefore = 5;
para1.Format.SpaceAfter = 0;
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe\rAddress\rAnytown, AnyCity  12345";

Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.LineSpacingRule = <tried them all> ;
para1.Format.SpaceBefore = 5;
para1.Format.SpaceAfter = 0;
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe\rnAddress\rnAnytown, AnyCity  12345";

I am obviously missing something because every example turns out exactly the same. There is extra spacing between each line of the address.

Anyone know how to make this work?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Mark Bonafe
  • 1,461
  • 13
  • 23
  • which library are you using? – Daniel A. White Feb 13 '18 at 15:05
  • Microsoft.Office.Interop.Word, runtime v2.0.50727 – Mark Bonafe Feb 13 '18 at 15:07
  • I might be barking up the wrong tree here because my Office.Interop skills are really poor, but doesn't a new line constitute a new paragraph? So by adding new lines to the paragraph text, when displayed in Word, it treats these as individual paragraphs and so the SpaceBefore and SpaceAfter takes effect? Slap me if I'm wrong. Update: see https://stackoverflow.com/questions/24193619/inserting-a-line-break-not-a-paragraph-break-programatically-to-a-word-documen – Danielle Summers Feb 13 '18 at 15:12
  • That may be so, but i have tried setting the "SpaceBefore" and "SpaceAfter" properties to zero. Still no go. – Mark Bonafe Feb 13 '18 at 15:22
  • YES! Your link has the answer. Using "\v" instead of "\r" or "\n" did the trick. Thank you so much. – Mark Bonafe Feb 13 '18 at 15:26
  • Possible duplicate of [Inserting A Line Break (Not A Paragraph Break) Programatically To A Word Document](https://stackoverflow.com/questions/24193619/inserting-a-line-break-not-a-paragraph-break-programatically-to-a-word-documen) – Cindy Meister Feb 13 '18 at 15:35
  • You guys are obviously better at searching for answers than I am. Thanks again! – Mark Bonafe Feb 13 '18 at 15:38

1 Answers1

2

Danel A. White to the rescue!

Using This question, the answer is to use "vertical tabs". I have never heard of them until now.

So, replacing "\r\n" with "\v" works perfectly.

    Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe\vAddress\vAnytown, AnyCity  12345";
Mark Bonafe
  • 1,461
  • 13
  • 23