2013-02-19 207 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實例。