【问题标题】:C# Word Document - How to clean formatting?C# Word 文档 - 如何清理格式?
【发布时间】:2013-02-04 10:15:28
【问题描述】:

困境相当简单。我需要创建一个小应用程序,它将清除所有字体背景颜色(保持表格单元格背景颜色不变),并删除 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;
    }

感谢您的帮助!我的意思是,我想,简单地开始如何发现格式会很有帮助。 :)

【问题讨论】:

    标签: c# replace formatting ms-word


    【解决方案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 实例,而不是为每次调用重新创建它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多