2013-02-19 210 views
0

这个困境相当简单。我需要创建一个能够清除所有字体背景颜色(保持表格单元格背景颜色不变)的小应用程序,并删除Word文档中带有删除线的所有文本,然后将文档保存到另一个文件夹中。否则,文档格式应保持不变。C#Word文档 - 如何清除格式?

下面是一个很大的例子,它可以从谷歌的随机示例中找到,它显示了如何将特定类型的格式应用于使用Find.Execute()发现的随机字符串。然而,我不知道如何才能如上所述那样做。

public static string searchDoc(string fileNameRef) 
    { 

     Microsoft.Office.Interop.Word._Application word = new Microsoft.Office.Interop.Word.Application(); ; 
     Microsoft.Office.Interop.Word._Document doc = new Microsoft.Office.Interop.Word.Document(); 
     object missing = System.Type.Missing; 

     try 
     { 
      System.IO.FileInfo ExecutableFileInfo = 
        new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location); 

      object fileName = 
       System.IO.Path.Combine(ExecutableFileInfo.DirectoryName, fileNameRef); 

      doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing 
       , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing 
       , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 
      doc.Activate(); 

      //object findStr = "hello"; //sonething to find 
      // THIS is the part where I fail, I can't find of a way to Find.Execute on formatting 
      // as opposed to mere strings. 
      //while (word.Selection.Find.Execute(ref findStr)) //found... 
      //{ 
      // //change font and format of matched words 
      // word.Selection.Font.Name = "Tahoma"; //change font to Tahoma 
      // word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed; //change color to red 
      //} 

      object saveFileName = ExecutableFileInfo.DirectoryName + "\\New\\" + fileNameRef; 

      doc.SaveAs(ref saveFileName, ref missing, ref missing, ref missing, ref missing 
       , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing 
       , ref missing, ref missing, ref missing, ref missing, ref missing); 

     } 
     catch (Exception) 
     { 
     } 
     finally 
     { 
      doc.Close(ref missing, ref missing, ref missing); 
      word.Application.Quit(ref missing, ref missing, ref missing); 
     } 

     return fileNameRef; 
    } 

感谢您的帮助!我的意思是任何,简单地开始如何确定格式将有助于很多,我想。 :)

回答

1

这不是C#特定的问题;这是一个Word对象模型问题(我指的是herehere)。

至于您的具体问题,我建议您打开Word中的宏记录器,执行操作,并查看生成的VBA代码。然后你可以在C#中应用它。

试试这个:

using System; 
using Microsoft.Office.Interop.Word; 
using System.IO; 
using System.Reflection; 

namespace WordFormattingFindReplace { 
    class Program { 
     static void Main(string[] args) { 
     } 

     public static string searchDoc(string fileName) { 
      _Application word = new Application(); ; 
      _Document doc; 

      string folderName = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); 
      string filePath = Path.Combine(folderName,fileName); 

      doc = word.Documents.Open(filePath); 

      var find=doc.Range().Find; 
      find.Text="Hello"; 
      find.Format=true; 
      find.Replacement.Font.Name="Tahoma"; 
      find.Replacement.Font.ColorIndex=WdColorIndex.wdRed; 
      find.Execute(Replace:WdReplace.wdReplaceAll); 

      doc.SaveAs2(Path.Combine(folderName,"New",fileName)); 

      doc.Close(); 

      //We need to cast this to _Application to resolve which Quit method is being called 
      ((_Application)word.Application).Quit(); 

      return fileName; 
     } 
    } 
} 

一些注意事项:

  • 使用using报表清晰。取而代之的Microsoft.Office.Interop.Word._Application word,在你的文件的顶部添加using Microsoft.Office.Interop.Word,然后你可以只写_Application word
  • 如果你需要的是文件夹名称,使用静态Path.GetDirectoryName方法,并保存为string变量,而不是创建一个FileInfo对象
  • 从.NET 4开始,在调用Documents.OpenDocument.SaveAsDocument.Close时,可以跳过可选参数。这也意味着你不需要object missing
  • 这里有没有什么用户真正需要看的,所以调用Document.Activate是不必要的
  • 这可能是更好的重用,而不是重新创建为每个调用Word.Application实例。