【问题标题】:Get replace normal text from word document in c#在c#中从word文档中获取替换普通文本
【发布时间】:2011-11-16 18:27:12
【问题描述】:

我正在将我的应用程序中的一些数据插入到 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)

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

【问题讨论】:

    标签: c# ms-word interop


    【解决方案1】:

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

    我建议使用类似休闲代码之类的东西,它将所有出现的“[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);
    

    【讨论】:

      【解决方案2】:

      你在正确的轨道上。如果:

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

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

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

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

      【讨论】:

      • 也感谢您的帮助,您上面的建议帮助我解决了我的问题。也有美好的一天
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2017-11-07
      • 2018-02-12
      • 2010-12-26
      • 1970-01-01
      • 2011-08-24
      • 2017-08-06
      相关资源
      最近更新 更多