用 C# 窗体中的文本替换 Word 中的文本
Replace text in Word with text from C# form
我正在尝试用 C# 创建一个应用程序。按下单选按钮时,我想打开 Microsoft Word 文档(发票)并用我的表单中的文本替换一些文本。 Word 文档还包含一些带有文本的文本框。
我已经尝试实现 link Word Automation Find and Replace not including Text Boxes 中编写的代码,但是当我按下单选按钮时,会出现 window 要求 "the encoding that makes the document readable" 然后Word 文档打开,里面全是黑色三角形和其他东西,而不是我最初的发票模板。
我的发票如何处理:
这是我试过的方法:
string documentLocation = @"C:\Documents\Visual Studio 2015\Project\Invoice.doc";
private void yes_radioBtn_CheckedChanged(object sender, EventArgs e)
{
FindReplace(documentLocation, "HotelName", "MyHotelName");
Process process = new Process();
process.StartInfo.FileName = documentLocation;
process.Start();
}
private void FindReplace(string documentLocation, string findText, string replaceText)
{
var app = new Microsoft.Office.Interop.Word.Application();
var doc = app.Documents.Open(documentLocation);
var range = doc.Range();
range.Find.Execute(FindText: findText, Replace: WdReplace.wdReplaceAll, ReplaceWith: replaceText);
var shapes = doc.Shapes;
foreach (Shape shape in shapes)
{
var initialText = shape.TextFrame.TextRange.Text;
var resultingText = initialText.Replace(findText, replaceText);
shape.TextFrame.TextRange.Text = resultingText;
}
doc.Save();
doc.Close();
Marshal.ReleaseComObject(app);
}
因此,如果您的单词模板每次基本上都相同
- 复制模板
- 处理模板
- 以所需格式保存
- 删除模板副本
您要在 Word 文档中替换的每个部分都必须为该位置插入书签(在区域中输入文本的最简单方法)。
我总是创建一个函数来实现这一点,我最终传递了路径 - 以及所有文本来替换我的文档内书签。函数调用有时会变长,但它对我有用。
Application app = new Application();
Document doc = app.Documents.Open("sDocumentCopyPath.docx");
if (doc.Bookmarks.Exists("bookmark_1"))
{
object oBookMark = "bookmark_1";
doc.Bookmarks.get_Item(ref oBookMark).Range.Text = My Text To Replace bookmark_1;
}
if (doc.Bookmarks.Exists("bookmark_2"))
{
object oBookMark = "bookmark_2";
doc.Bookmarks.get_Item(ref oBookMark).Range.Text = My Text To Replace bookmark_2;
}
doc.ExportAsFixedFormat("myNewPdf.pdf", WdExportFormat.wdExportFormatPDF);
((_Document)doc).Close();
((_Application)app).Quit();
这段代码应该让你起来 运行 除非你想将所有值传递给一个函数。
编辑:如果您需要更多示例,我也在写博客 post,所以如果您的用例还不够清楚,我会提供更多详细信息。
我正在尝试用 C# 创建一个应用程序。按下单选按钮时,我想打开 Microsoft Word 文档(发票)并用我的表单中的文本替换一些文本。 Word 文档还包含一些带有文本的文本框。
我已经尝试实现 link Word Automation Find and Replace not including Text Boxes 中编写的代码,但是当我按下单选按钮时,会出现 window 要求 "the encoding that makes the document readable" 然后Word 文档打开,里面全是黑色三角形和其他东西,而不是我最初的发票模板。
我的发票如何处理:
这是我试过的方法:
string documentLocation = @"C:\Documents\Visual Studio 2015\Project\Invoice.doc";
private void yes_radioBtn_CheckedChanged(object sender, EventArgs e)
{
FindReplace(documentLocation, "HotelName", "MyHotelName");
Process process = new Process();
process.StartInfo.FileName = documentLocation;
process.Start();
}
private void FindReplace(string documentLocation, string findText, string replaceText)
{
var app = new Microsoft.Office.Interop.Word.Application();
var doc = app.Documents.Open(documentLocation);
var range = doc.Range();
range.Find.Execute(FindText: findText, Replace: WdReplace.wdReplaceAll, ReplaceWith: replaceText);
var shapes = doc.Shapes;
foreach (Shape shape in shapes)
{
var initialText = shape.TextFrame.TextRange.Text;
var resultingText = initialText.Replace(findText, replaceText);
shape.TextFrame.TextRange.Text = resultingText;
}
doc.Save();
doc.Close();
Marshal.ReleaseComObject(app);
}
因此,如果您的单词模板每次基本上都相同
- 复制模板
- 处理模板
- 以所需格式保存
- 删除模板副本
您要在 Word 文档中替换的每个部分都必须为该位置插入书签(在区域中输入文本的最简单方法)。
我总是创建一个函数来实现这一点,我最终传递了路径 - 以及所有文本来替换我的文档内书签。函数调用有时会变长,但它对我有用。
Application app = new Application();
Document doc = app.Documents.Open("sDocumentCopyPath.docx");
if (doc.Bookmarks.Exists("bookmark_1"))
{
object oBookMark = "bookmark_1";
doc.Bookmarks.get_Item(ref oBookMark).Range.Text = My Text To Replace bookmark_1;
}
if (doc.Bookmarks.Exists("bookmark_2"))
{
object oBookMark = "bookmark_2";
doc.Bookmarks.get_Item(ref oBookMark).Range.Text = My Text To Replace bookmark_2;
}
doc.ExportAsFixedFormat("myNewPdf.pdf", WdExportFormat.wdExportFormatPDF);
((_Document)doc).Close();
((_Application)app).Quit();
这段代码应该让你起来 运行 除非你想将所有值传递给一个函数。
编辑:如果您需要更多示例,我也在写博客 post,所以如果您的用例还不够清楚,我会提供更多详细信息。