如何使用interop.word查找并替换到列表中



因此,我试图将用于替换某些唯一句子的代码组合到每种类型的checkedbox的列表中。例如:

"uniquecode1"进入:

  • 列出项目1
  • 列表项2

我已经设法使用了c#word interop find and replace everything中的简单查找和替换方法但我仍然没有找到将这些转换为列表项的方法。有一刻我想我为什么不这样做呢?

for (int i = 0; i == checkedbox.Count; i++)
{
FindAndReplace(oWord, "uniquecode1", checkedbox[i]);
}

然后我意识到,一旦"uniquecode1"消失,它就几乎失败了。如果有人能给我一个答案或至少一条线索,我真的很感激。这是我当前的代码,我知道它很脏。。。

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

根据Sauntinsoft,他们使用

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

使用interop是一个多步骤的过程。首先,有必要找到目标——被搜索的文本。然后将信息插入Range,最后对其进行格式化

Word的Find.Execute返回一个布尔值-成功时为true,运行FindRange位于找到的位置。因此,将文本添加到Range,然后格式化该Range,如示例代码*:所示

Word.Document doc = wdApp.ActiveDocument;
//Code from question
//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);
Word.Range rng = doc.Content;
Word.Find f = rng.Find;
object oTrue = true;
object missing = Type.Missing;
string searchTerm = "uniquecode1";
f.ClearFormatting();
f.Text = searchTerm;
bool found = f.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, Word.WdFindWrap.wdFindStop, ref missing, ref missing, Word.WdReplace.wdReplaceNone,
ref  missing, ref missing, ref missing, ref missing);
if (found)
{
rng.Delete();
//Code from question
//for (int i = 0; i == checkedbox.Count; i++)
List<string> lst = new List<string> { "one", "two", "three" };
foreach (string entry in lst)
{
rng.InsertAfter(entry + "n");
}
rng.ListFormat.ApplyBulletDefault();
}

*考虑到我在问题中理解的需求,这可以完全使用Find&如果:,则替换

  • Replacement.Text可以是一个字符串。在本例中,通过在Find之前将所有值串联成一个字符串并将该字符串分配给f.Replacement.Text,这是可能的。那么参数CCD_ 13将是CCD_
  • 项目符号/编号定义为命名样式。这将需要文档中的段落样式(或由代码动态生成(链接到定义项目符号和/或编号的ListTemplate对象。假设存在,则可以将样式分配给Replacement.set_Style("stylename");,然后需要将参数Format设置为true

这基本上就是你不想花钱购买的库的幕后情况。

最新更新