Bullet points in Word with c# Interop

16,626

Solution 1

This happens because you're adding multiple paragraphs to the range after the range (it seems that setting the Text property is equivalent to InsertAfter). You want to InsertBefore the range so that the formatting you set gets 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);
    }

Notice that we add an End of Paragraph mark to all items except the last one. You will get an empty bullet if you add one to the last.

Solution 2

This is based on Tergiver's answer. The difference is it inserts the list items in the correct order after the initially created paragraph. For your own use make the starting range equal to the item you want to insert 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 don't really understand what I'm doing with the range here. This solution was arrived at after considerable trial and error. I suspect it may have to do with the fact that I'm reusing the same range and Tergiver's solution is grabbing a new range each time the range is accessed. I particularly don't understand the following lines:

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

Generally any alterations to the above code and the list gets intermingled with the previous element. If somebody could explain why this works, I'd be grateful.

Share:
16,626
Fraser Connor
Author by

Fraser Connor

Updated on June 04, 2022

Comments

  • Fraser Connor
    Fraser Connor almost 2 years

    I have the following code which is supposed to add a bulleted list to a word document that I'm generating automatically. From other answers I believe the code is correct, but the result doesn't produce any bullet points at all, it doesn't seem to apply the indent 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();
    
  • Giles Roberts
    Giles Roberts over 11 years
    Using assets.Range.InsertBefore(bulletItem); inserts the elements in the list in reverse order. Did you mean InsertAfter?
  • Tergiver
    Tergiver over 11 years
    @GilesRoberts InsertAfter doesn't work because the formatting (bullet default) doesn't get applied. So this code uses InsertBefore and adds the items in reverse order.
  • Giles Roberts
    Giles Roberts over 11 years
    That probably explains why I had so much difficultly in getting the bullet points to apply to the correct parts of the document. I ended up having to be very careful setting up my range before using the insert after. It did work in the end though.
  • Tergiver
    Tergiver over 11 years
    @GilesRoberts I couldn't figure out how to set up the range and get InsertAfter working, it's what I tried first. If you have another answer, you should add it.
  • Giles Roberts
    Giles Roberts over 11 years
    I've added my answer based on your answer but inserting the list in the correct order.