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 Microsoft.Office.Interop.Word
namespace.
Basically, they would select from a list of available instructions, each being of the type string
, 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 \
, 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.
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.
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 < count; i++)
{
var listLevel = 0;
var currentItem = bulletList[i];
var item = currentItem.Replace(@"\", "");
if (i < count - 1)
item = item + "\n";
listLevel += currentItem.ToList().Where(x => x == '\\').Select(x => x).Count();
for (var x = 0; x < listLevel; x++)
{
range.ListFormat.ListIndent();
}
range.InsertAfter(item);
}
doc.SaveAs(@"c:\temp\" + DateTime.Now.Ticks + ".docx");
doc.Close();
So the output of my code should be:
- 1 Point A
- 1.1 Point B
- 1.2 Point C
- 1.2.1 Point D
- 2 Point E
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.