繁体   English   中英

在c#中从word文档中替换普通文本

[英]Get replace normal text from word document in c#

我正在将我的应用程序中的一些数据插入到 Word 文档中,然后再次保存。 我现在拥有的代码对于放在 word 文档中的表格中的文本可以正常工作,但它没有得到不在表格中的文本。 例如,word 文档的第一页不在表格中,代码跳过第一页并立即转到第二页,其中有文本放在表格中,并按原样替换文本。 这是我的代码:

Document docc = app.Documents.Open(ref path, ref o, ref readOnly, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
 docc.Activate();
 try
 {
      foreach (Paragraph p in docc.Paragraphs)
      {
           Range rng = p.Range;
           Style sty = (Style)p.get_Style();                      

           if ((bool)rng.get_Information(WdInformation.wdWithInTable) == true)
           {
                foreach (Cell c in rng.Cells)
                {
                     if (rng.Cells.Count > 0)
                     {
                          string testtt = c.Range.Text.ToString();
                          if (c.Range.Text.ToString().Contains("[Company_Name]\r\a"))
                               //   c.Next.Range.Text = "Sacramento";
                               c.Range.Text = "Sacramento";
                     }
                }
                docc.Save();                        
           }
           docc.Close(ref o, ref o, ref o);
      }
 }

我知道这一行:

 if ((bool)rng.get_Information(WdInformation.wdWithInTable) == true)

仅获取带有表格的页面,但我想知道如何从没有表格的页面获取数据并修改那里的文本。

首先,您在更改每个单元格后保存文档 - 没有必要这样做。 其次,更重要的是,您在第一段之后关闭文档,因此下一段(段落)将引发异常。

我建议使用类似空闲代码的东西,它将所有出现的“[Company_Name]”替换为“Sacramento”(在整个文档中)。

using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
using System.Runtime.InteropServices;

...

object o = Missing.Value;
object oFalse = false;
object oTrue = true;

Word._Application app = null;
Word.Documents docs = null;
Word.Document doc = null;

object path = @"C:\path\file.doc";

try
{
    app = new Word.Application();
    app.Visible = false;
    app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

    docs = app.Documents;
    doc = docs.Open(ref path, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
    doc.Activate();

    foreach (Word.Range range in doc.StoryRanges)
    {
        Word.Find find = range.Find;
        object findText = "[Company_Name]";
        object replacText = "Sacramento";
        object replace = Word.WdReplace.wdReplaceAll;
        object findWrap = Word.WdFindWrap.wdFindContinue;

        find.Execute(ref findText, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref replacText,
            ref replace, ref o, ref o, ref o, ref o);

        Marshal.FinalReleaseComObject(find);
        Marshal.FinalReleaseComObject(range);
    }

    doc.Save();
    ((Word._Document)doc).Close(ref o, ref o, ref o);
    app.Quit(ref o, ref o, ref o);
}
finally
{
    if (doc != null)
        Marshal.FinalReleaseComObject(doc);

    if (docs != null)
        Marshal.FinalReleaseComObject(docs);

    if (app != null)
        Marshal.FinalReleaseComObject(app);
}

有两个重要的事情:

1) 不要在 COM 对象中使用两个点:

// might be a problem soon:
app.Documents.Open(....

// better way:
Documents docs = app.Documents;
Document doc = docs.Open(...

2)一旦你不需要它们就以相反的顺序释放它们:

if (doc != null)
    Marshal.FinalReleaseComObject(doc);

if (docs != null)
    Marshal.FinalReleaseComObject(docs);

你在正确的轨道上。 如果出现以下情况,您需要在大文件中添加一个 else :

else {
    if (rng.Text.Contains("[Company_Name]\r\a"))
           rng.Text = "Sacramento";
}

文本已经是一个字符串,顺便说一句。

这与您现有的代码匹配(因为我复制并粘贴了它),但我认为您想要一些不同的东西:

rng.Text.Replace ("[CompanyName]","Sacramento");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM