Search code examples
c#.netms-wordoffice-interop

How to insert a bulleted list from a text box?


I need to make a bulleted list in word. It gets the input from a Multi-Line text box. Each new line that is started on the multt-line text box should produce a new bullet point.

Here is an example of what I am trying to achieve: Input Result

Here is the code that I have, which generates text that was already inputted. (BTW it is not mine)

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);
}

Solution

  • I'm not exactly what you're trying to achieve, but as I understand it you're trying to insert bullets before each line in a TextBox and then trying to put that text into Word?

    Maybe this snippet of code will help you.

    string[] unbulleted = textBox1.Lines;
    string[] bulleted = new string[unbulleted.Length];
    for (int i = 0; i < bulleted.Length; i++)
        bulleted[i] = "\u2022" + unbulleted[i];
    

    So now you have a string[] with bullets before each string! I'm not sure how one inserts it into Word though.