如何使用interop.word查找并替换为列表?

问题描述 投票:0回答:1

因此,我试图将用于将某些唯一句子替换为每种类型的checkbox.checked的列表的代码放在一起:

“ 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 });
c# list ms-word office-interop
1个回答
0
投票
通过互操作进行此过程是一个多步骤的过程。首先,必须找到目标-正在搜索的文本。然后将信息插入到Range中,最后对其进行格式化。

Word的Find.Execute返回一个布尔值-成功时为true,并且在其上找到运行了RangeFind。因此,添加文本

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

*鉴于我在问题中理解的要求,可以完全使用“查找并替换” 

if

] ::完成此操作
  • Replacement.Text可以是一个字符串。在此示例中,可以通过将所有值连接到Find中的一个字符串before并将该字符串分配给f.Replacement.Text来实现。然后参数Word.WdReplace.wdReplaceNone将为wdReplaceOne
  • 项目符号/编号定义为命名样式
  • 。这将需要在文档中(或由代码动态生成)中的段落样式链接到定义项目符号和/或编号的ListTemplate对象。假设存在,可以将样式分配给Replacement.set_Style("stylename");,然后需要将参数Format设置为true
本质上,这是您不想付费使用的库在后台发生的事情。
© www.soinside.com 2019 - 2024. All rights reserved.