[英]C# Word Document - How to clean formatting?
The dilemma is rather simple. 困境很简单。 I need to create a small app that will clear all font background colors (leave table cell background colours unchanged), and remove all text with strikethrough in a word document, and then save the document into another folder. 我需要创建一个小型应用程序,该应用程序将清除所有字体背景颜色(保留表格单元格背景颜色不变),并删除Word文档中带有删除线的所有文本,然后将文档保存到另一个文件夹中。 Otherwise the document's formatting should remain untouched. 否则,文档的格式应保持不变。
Below is a large-ish example scraped together from random examples available in google showing how to apply specific kinds of formatting to random strings found using Find.Execute(). 以下是从google上的随机示例中整理的一个大型示例,展示了如何将特定类型的格式应用于使用Find.Execute()找到的随机字符串。 I have no clue however, on how to only do as described above. 但是,我不知道如何仅如上所述进行操作。
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;
}
Thanks for any help! 谢谢你的帮助! And I do mean any, simply getting started on how to spot formatting would help a great deal, I imagine. 我的意思是,我想,任何只是简单地入门如何进行格式化都会有很大帮助。 :) :)
This is not a C#-specific question; 这不是C#特有的问题。 it's a Word Object Model question (I refer you to here and here ). 这是一个Word对象模型问题(我在这里和这里介绍您 )。
As to your specific question, I suggest you turn on the Macro Recorder in Word, perform the actions, and see the generated VBA code. 关于您的特定问题,建议您在Word中打开Macro Recorder,执行操作,然后查看生成的VBA代码。 Then you can apply it in C#. 然后,您可以在C#中应用它。
Try this: 尝试这个:
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;
}
}
}
Some notes: 一些注意事项:
using
statements for clarity. 为了清楚起见,使用using
语句。 Instead of Microsoft.Office.Interop.Word._Application word
, add using Microsoft.Office.Interop.Word
at the top of your file, and you can then just write _Application word
代替Microsoft.Office.Interop.Word._Application word
,在文件顶部using Microsoft.Office.Interop.Word
添加,然后可以只写_Application word
Path.GetDirectoryName
method and save as a string
variable, instead of creating a FileInfo
object 如果您只需要文件夹名称,请使用静态Path.GetDirectoryName
方法并将其另存为string
变量,而不是创建FileInfo
对象 Documents.Open
, Document.SaveAs
and Document.Close
. 从.NET 4开始,在调用Documents.Open
, Document.SaveAs
和Document.Close
时可以跳过可选参数。 This also means you don't need an object missing
. 这也意味着您不需要object missing
任何object missing
。 Document.Activate
is unnecessary 用户真正需要查看的内容完全没有,因此无需调用Document.Activate
Word.Application
instance, instead of recreating it for each call. 最好重用Word.Application
实例,而不是为每个调用重新创建它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.