So I'm trying to put together code for replacing certain unique sentences into a list for every type of checkedbox.checked for example:
"uniquecode1" into:
- List item1
- List item2
I had managed to use the simple find and replace method from c# word interop find and replace everything But I still haven't found a way to convert those into a list item. There was a moment I thought why don't I do it this way?
for (int i = 0; i == checkedbox.Count; i++)
{
FindAndReplace(oWord, "uniquecode1", checkedbox[i]);
}
and then I realize once the "uniquecode1" is gone it's pretty much failed. I really appreciate it if someone can give me an answer or at least a clue. This is my current code, I know it's dirty...
string[] Mycollection = new string[] { "One, Two, Three" };
string temp;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "pdf files (*.pdf)|*.pdf";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.ShowDialog();
temp = saveFileDialog1.FileName;
// Create an instance of Word.exe
Microsoft.Office.Interop.Word.Application oWord = new Word.Application
{
// To make this instance of word invisible (Can still see it in the taskmgr).
Visible = false
};
// Interop requires objects.
object oMissing = System.Reflection.Missing.Value;
object isVisible = true;
object readOnly = true; // Does not cause any word dialog to show up
//object readOnly = false; // Causes a word object dialog to show at the end of the conversion
object oInput = textBox1.Text;
object oOutput = temp;
object oFormat = WdSaveFormat.wdFormatPDF;
// Load a document into our instance of word.exe
Document oDoc = oWord.Documents.Open(
ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing
);
// Make this document the active document.
oDoc.Activate();
//Replace Text1
FindAndReplace(oWord, "<Input Module1>", richTextBox1.Text);
//Replace Text to List1
foreach(string lisText in Mycollection)
{
//program to convert Text into a bullet or number list
}
// Save this document using Word
oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
);
// Always close Word.exe.
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
As per Sauntinsoft they used
string loadPath = @"..\..\example.docx";
DocumentCore dc = DocumentCore.Load(loadPath);
string[] myCollection = new string[] { "One", "Two", "Three", "Four", "Five" };
ListStyle bullList = new ListStyle("Bullets", ListTemplateType.Bullet);
dc.Styles.Add(bullList);
int level = 0;
List<Paragraph> parList = new List<Paragraph>();
foreach (string listText in myCollection)
{
Paragraph p = new Paragraph(dc);
p.Content.End.Insert(listText, new CharacterFormat() { Size = 14.0, FontColor = Color.Black });
p.ListFormat.Style = bullList;
p.ListFormat.ListLevelNumber = level;
p.ParagraphFormat.SpaceAfter = 0;
parList.Add(p);
}
Regex regex = new Regex(@"Hello", RegexOptions.IgnoreCase);
foreach (ContentRange item in dc.Content.Find(regex).Reverse())
{
foreach (Paragraph p in parList.Reverse<Paragraph>())
item.End.Insert(p.Content);
}
foreach (ContentRange item in dc.Content.Find(regex).Reverse())
{
item.Delete();
}
string savePath = Path.ChangeExtension(loadPath, ".replaced.docx");
dc.Save(savePath, SaveOptions.DocxDefault);
// Open the original and result documents for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath) { UseShellExecute = true });
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });