Bullet points in Word with C # Interop

I have the following code that should add a bulleted list to a Word document that I automatically generate. From other answers, I believe that the code is correct, but the result does not cause any bullet, it does not seem to apply indentation either. Any ideas?

Microsoft.Office.Interop.Word.Paragraph assets; assets = doc.Content.Paragraphs.Add(Type.Missing); // Some code to generate the text foreach (String asset in assetsList) { assetText = assetText + asset + "\n"; } assets.Range.ListFormat.ApplyBulletDefault(Type.Missing); // Add it to the document assets.Range.ParagraphFormat.LeftIndent = -1; assets.Range.Text = assetText; assets.Range.InsertParagraphAfter(); 
+3
source share
3 answers

This is because you add a few paragraphs to the range after the range (it seems that setting the Text property is equivalent to InsertAfter). You want InsertBefore to be so that the formatting applied is applied.

  Paragraph assets = doc.Content.Paragraphs.Add(); assets.Range.ListFormat.ApplyBulletDefault(); string[] bulletItems = new string[] { "One", "Two", "Three" }; for (int i = 0; i < bulletItems.Length; i++) { string bulletItem = bulletItems[i]; if (i < bulletItems.Length - 1) bulletItem = bulletItem + "\n"; assets.Range.InsertBefore(bulletItem); } 

Note that we add the end of paragraph mark to all but the last element. You will get an empty bullet if you add it to the last.

+4
source

This is based on Tergiver's answer. The difference is that it inserts the list items in the correct order after the paragraph that was originally created. For your own use, make the initial range equal to the element you want to insert in the list after.

 Paragraph assets = doc.Content.Paragraphs.Add(); rng = assets.Range; rng.InsertAfter("\n"); start = rng.End; end = rng.End; rng = _oDoc.Range(ref start, ref end); object listType = 0; rng.ListFormat.ApplyBulletDefault(ref listType); string[] bulletItems = new string[] { "One", "Two", "Three" }; for (int i = 0; i < bulletItems.Length; i++) { string bulletItem = bulletItems[i]; if (i < RowCount - 1) bulletItem = bulletItem + "\n"; rng.InsertAfter(bulletItem); } 

Please note: I do not quite understand what I am doing with the range here. This decision was obtained after considerable trial and error. I suspect this may be because I am reusing the same range, and the Tergiver solution captures a new range every time I access the range. I especially do not understand the following lines:

 rng.InsertAfter("\n"); start = rng.End; end = rng.End; rng = _oDoc.Range(ref start, ref end); 

Typically, any changes to the above code and list are mixed with the previous item. If anyone could explain why this works, I would be grateful.

+2
source

You can try under the code block if you want a subscription list :

 static void Main(string[] args) { try { Application app = new Application(); Document doc = app.Documents.Add(); Range range = doc.Range(0, 0); range.ListFormat.ApplyNumberDefault(); range.Text = "Birinci"; range.InsertParagraphAfter(); ListTemplate listTemplate = range.ListFormat.ListTemplate; //range.InsertAfter("Birinci"); //range.InsertParagraphAfter(); //range.InsertAfter("İkinci"); //range.InsertParagraphAfter(); //range.InsertAfter("Üçüncü"); //range.InsertParagraphAfter(); Range subRange = doc.Range(range.StoryLength - 1); subRange.ListFormat.ApplyBulletDefault(); subRange.ListFormat.ListIndent(); subRange.Text = "Alt Birinci"; subRange.InsertParagraphAfter(); ListTemplate sublistTemplate = subRange.ListFormat.ListTemplate; Range subRange2 = doc.Range(subRange.StoryLength - 1); subRange2.ListFormat.ApplyListTemplate(sublistTemplate); subRange2.ListFormat.ListIndent(); subRange2.Text = "Alt İkinci"; subRange2.InsertParagraphAfter(); Range range2 = doc.Range(range.StoryLength - 1); range2.ListFormat.ApplyListTemplateWithLevel(listTemplate,true); WdContinue isContinue = range2.ListFormat.CanContinuePreviousList(listTemplate); range2.Text = "İkinci"; range2.InsertParagraphAfter(); Range range3 = doc.Range(range2.StoryLength - 1); range3.ListFormat.ApplyListTemplate(listTemplate); range3.Text = "Üçüncü"; range3.InsertParagraphAfter(); string path = Environment.CurrentDirectory; int totalExistDocx = Directory.GetFiles(path, "test*.docx").Count(); path = Path.Combine(path, string.Format("test{0}.docx", totalExistDocx + 1)); app.ActiveDocument.SaveAs2(path, WdSaveFormat.wdFormatXMLDocument); doc.Close(); Process.Start(path); } catch (Exception exception) { throw; } } 

Note: If you do not know the length of the input, you should not determine the value of the end of the range, for example:

 static void Main(string[] args) { try { Application app = new Application(); Document doc = app.Documents.Add(); Range range = doc.Range(0, 0); range.ListFormat.ApplyNumberDefault(); range.Text = "Birinci"; range.InsertParagraphAfter(); ListTemplate listTemplate = range.ListFormat.ListTemplate; //range.InsertAfter("Birinci"); //range.InsertParagraphAfter(); //range.InsertAfter("İkinci"); //range.InsertParagraphAfter(); //range.InsertAfter("Üçüncü"); //range.InsertParagraphAfter(); Range subRange = doc.Range(range.StoryLength - 1, range.StoryLength - 1); subRange.ListFormat.ApplyBulletDefault(); subRange.ListFormat.ListIndent(); subRange.Text = "Alt Birinci"; subRange.InsertParagraphAfter(); ListTemplate sublistTemplate = subRange.ListFormat.ListTemplate; Range subRange2 = doc.Range(subRange.StoryLength - 1, range.StoryLength - 1); subRange2.ListFormat.ApplyListTemplate(sublistTemplate); subRange2.ListFormat.ListIndent(); subRange2.Text = "Alt İkinci"; subRange2.InsertParagraphAfter(); Range range2 = doc.Range(range.StoryLength - 1, range.StoryLength - 1); range2.ListFormat.ApplyListTemplateWithLevel(listTemplate,true); WdContinue isContinue = range2.ListFormat.CanContinuePreviousList(listTemplate); range2.Text = "İkinci"; range2.InsertParagraphAfter(); Range range3 = doc.Range(range2.StoryLength - 1, range.StoryLength - 1); range3.ListFormat.ApplyListTemplate(listTemplate); range3.Text = "Üçüncü"; range3.InsertParagraphAfter(); string path = Environment.CurrentDirectory; int totalExistDocx = Directory.GetFiles(path, "test*.docx").Count(); path = Path.Combine(path, string.Format("test{0}.docx", totalExistDocx + 1)); app.ActiveDocument.SaveAs2(path, WdSaveFormat.wdFormatXMLDocument); doc.Close(); Process.Start(path); } catch (Exception exception) { throw; } } 
0
source

Source: https://habr.com/ru/post/1495947/


All Articles