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

C# 如何使用interop.word查找并替换到列表中?,c#,list,ms-word,office-interop,C#,List,Ms Word,Office Interop,因此,我试图将代码放在一起,用于将某些独特的句子替换到每种类型的复选框的列表中。例如: “唯一代码1”分为: 清单项目1 清单项目2 我已经设法从中使用了简单的查找和替换方法 但我仍然没有找到一种方法将它们转换为列表项。 有一刻我在想为什么我不这样做呢 for (int i = 0; i == checkedbox.Count; i++) { FindAndReplace(oWord, "uniquecode1", checkedbox

因此,我试图将代码放在一起,用于将某些独特的句子替换到每种类型的复选框的列表中。例如:

“唯一代码1”分为:

  • 清单项目1
  • 清单项目2
我已经设法从中使用了简单的查找和替换方法 但我仍然没有找到一种方法将它们转换为列表项。 有一刻我在想为什么我不这样做呢

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);
string[]Mycollection=newstring[]{“一、二、三”};
字符串温度;
SaveFileDialog saveFileDialog1=新建SaveFileDialog();
saveFileDialog1.Filter=“pdf文件(*.pdf)|*.pdf”;
saveFileDialog1.FilterIndex=2;
saveFileDialog1.RestoreDirectory=true;
saveFileDialog1.ShowDialog();
temp=saveFileDialog1.FileName;
//创建Word.exe的实例
Microsoft.Office.Interop.Word.Application oWord=新Word.Application
{
//使word的此实例不可见(仍可以在taskmgr中看到它)。
可见=假
};
//互操作需要对象。
对象omising=System.Reflection.Missing.Value;
对象isVisible=true;
对象只读=true;//不会导致出现任何word对话框
//对象只读=false;//使word对象对话框在转换结束时显示
object oInput=textBox1.Text;
对象输出=温度;
格式对象=WdSaveFormat.wdFormatPDF;
//将文档加载到word.exe实例中
文档oDoc=oWord.Documents.Open(
ref输入,ref省略,ref只读,ref省略,ref省略,
引用引用,引用引用引用,引用引用引用,引用引用引用,引用引用引用,
ref-omising,ref-isVisible,ref-omising,ref-omising,ref-omising,ref-omising
);
//使此文档成为活动文档。
oDoc.Activate();
//替换文本1
查找并替换(oWord,“,richTextBox1.Text);
//将文本替换为列表1
foreach(Mycollection中的字符串listex)
{
//将文本转换为项目符号或数字列表的程序
}
//使用Word保存此文档
oDoc.SaveAs(参考输出、参考格式、参考删除、参考删除、,
引用引用,引用引用引用,引用引用引用,引用引用引用,引用引用引用,
引用省略,引用省略,引用省略,引用省略,引用省略,引用省略,引用省略,引用省略,引用省略
);
//始终关闭Word.exe。
oWord.Quit(引用删除,引用删除,引用删除);
根据他们使用的软件

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 });
string loadPath=@.\..\example.docx”;
DocumentCore dc=DocumentCore.Load(加载路径);
string[]myCollection=新字符串[]{“一”、“二”、“三”、“四”、“五”};
ListStyle bullList=新的ListStyle(“项目符号”,ListTemplateType.Bullet);
dc.Styles.Add(bullList);
智力水平=0;
List parList=新列表();
foreach(myCollection中的字符串listText)
{
p段=新的(dc)段;
p、 Content.End.Insert(listText,new CharacterFormat(){Size=14.0,FontColor=Color.Black});
p、 Style=bullList;
p、 ListFormat.ListLevelNumber=级别;
p、 ParagraphFormat.SpaceAfter=0;
添加(p);
}
Regex Regex=newregex(@“Hello”,RegexOptions.IgnoreCase);
foreach(dc.Content.Find(regex.Reverse())中的ContentRange项)
{
foreach(parList.Reverse()中的p段)
项目。结束。插入(第页内容);
}
foreach(dc.Content.Find(regex.Reverse())中的ContentRange项)
{
项目.删除();
}
字符串savePath=Path.ChangeExtension(加载路径,“.replaced.docx”);
Save(savePath,SaveOptions.DocxDefault);
//打开原始文档和结果文档进行演示。
System.Diagnostics.Process.Start(新的System.Diagnostics.ProcessStartInfo(加载路径){UseShellExecute=true});
System.Diagnostics.Process.Start(新的System.Diagnostics.ProcessStartInfo(保存路径){UseShellExecute=true});

使用互操作是一个多步骤的过程。首先,有必要找到目标-正在搜索的文本。然后将信息插入该
范围
,最后格式化

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

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();
}
Word.Document doc=wdApp.ActiveDocument;
//问题代码
//文档oDoc=oWord.Documents.Open(
//ref输入,ref省略,ref只读,ref省略,ref省略,
//引用引用,引用引用引用,引用引用引用,引用引用引用,引用引用引用,
//ref omising,ref isVisible,ref omising,ref omising,ref omising,ref omising);
Word.Range rng=单据内容;
Word.Find f=rng.Find;
object oTrue=true;
对象缺失=类型。缺失;
字符串searchTerm=“uniquecode1”;
f、 ClearFormatting();
f、 文本=搜索词;
bool found=f.Execute(参考缺失、参考缺失、参考缺失、参考缺失、参考缺失、参考缺失、,
裁判失误