title: C#: Insert and indent bullet points at bookmark in word document using Office Interop libraries
body: <p>Here's my dilemma. I've been given a task to generate a specific section of an existing word document based on user input from a web front end. The back end of the system is being written in C# with the part that edits the word document using the <code>Microsoft.Office.Interop.Word</code> namespace.</p>
  
  <p>Basically, they would select from a list of available instructions, each being of the type <code>string</code>, which will then be used to generate the instructions part of the document, with each separate instruction being another bullet in the list. This part works fine. My problem is, the instructions can contain the character <code>\</code>, which need to be replaced with an indent, or the equivalent of hitting TAB while in the bullet if you had the document opened in word. So far I'm able to get it to insert the bullets into the middle of the list just fine, it continues numbering them appropriately as expected. The kicker is I can't get it to indent them as needed.</p>
  
  <p>I've tried pretty much all of the examples I was able to find here and on a few other sites to get this to work but with no success. The latest iteration is in the code below, which just indents the entire list as far as it can go.</p>
  
  <pre><code>var bookmark = "bookMarkName";
  var docPath = @"c:\temp\Template.docx";
  var app = new Application();
  var doc = app.Documents.Open(docPath);
  var range = doc.Bookmarks[bookmark].Range;
  var listTemplate = range.ListFormat.ListTemplate;
  
  range.ListFormat.ApplyListTemplate(listTemplate);
  
  string[] bulletList = new string[] {
      @"Point A",
      @"\Point B",
      @"\Point C",
      @"\\Point D",
      @"Point E"
  }
  
  var count = bulletList.Length;
  
  for (var i = 0; i &lt; count; i++)
  {
      var listLevel = 0;
      var currentItem = bulletList[i];
      var item = currentItem.Replace(@"\", "");
  
      if (i &lt; count - 1)
          item = item + "\n";
  
      listLevel += currentItem.ToList().Where(x =&gt; x == '\\').Select(x =&gt; x).Count();
  
      for (var x = 0; x &lt; listLevel; x++)
      {
          range.ListFormat.ListIndent();
      }
  
      range.InsertAfter(item);
  }
  
  doc.SaveAs(@"c:\temp\" + DateTime.Now.Ticks + ".docx");
  doc.Close();
  </code></pre>
  
  <p>So the output of my code should be:</p>
  
  <ul>
  <li>1 Point A
  
  <ul>
  <li>1.1 Point B</li>
  <li>1.2 Point C
  
  <ul>
  <li>1.2.1 Point D</li>
  </ul></li>
  </ul></li>
  <li>2 Point E</li>
  </ul>
  
  <p>This is the first time I've ever really had to work with the Office Interop libraries so I'm positive there's something I'm missing here. Any help would be greatly appreciated.</p>