使用 c# 和 Microsoft Word Interop 在 Word 中填充字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18533198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filling fields in Word using c# and Microsoft Word Interop
提问by sebastianmehler
I tried to Fill out Form Fields in Microsoft Word using C# Interop Assemblies with the following Code
我尝试使用 C# Interop Assemblies 和以下代码在 Microsoft Word 中填写表单字段
string filename = @"N:\mehler\Vorlage2.dotx";
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
doc = word.Documents.Open(filename);
doc.Activate();
foreach (Microsoft.Office.Interop.Word.FormField field in doc.FormFields)
{
switch (field.Name)
{
case "Text2":
field.Range.Text = "1";
break;
default:
break;
}
}
doc.SaveAs2(@"N:\mehler\Ausgefuellt.docx");
doc.Close();
word.Quit();
System.Diagnostics.Process.Start(@"N:\mehler\Ausgefuellt.docx");
Microsoft Word can not open the File Ausgefuellt.docx and Shows a Message saying that an unknown Error has occured.
Microsoft Word 无法打开文件 Ausgefuellt.docx 并显示一条消息,指出发生了未知错误。
I created a simple Word Document with a bit of unformated text and 2 Text-Form-Fields
我创建了一个简单的 Word 文档,其中包含一些未格式化的文本和 2 个文本表单字段
can anyone tell me, what went wrong or if ia have an Error in my Source Code
谁能告诉我,出了什么问题,或者我的源代码中是否有错误
Edit: I managed to specify the Problem. I created an Document only conaining one Text Form Field. In Word 2013 this is found unter the Topic "Formulare aus Vorversionen" (I would translate this to "Formfields from former Versions") If I comment out the whole foreach Block so that I would only open and save the Document, I get the same result.
编辑:我设法指定了问题。我创建了一个仅包含一个文本表单字段的文档。在 Word 2013 中,这是在主题“Formulare aus Vorversionen”下找到的(我会将其翻译为“以前版本的表单域”)如果我注释掉整个 foreach 块以便我只打开和保存文档,我会得到相同的结果结果。
If I open the Source File directly in Word it is no Problem. I also tried to load the Document and make Word Visible. The Result looked like an empty Word instance with no Document loaded.
如果我直接在 Word 中打开源文件没有问题。我还尝试加载文档并使 Word 可见。结果看起来像一个没有加载文档的空 Word 实例。
采纳答案by Nir Kornfeld
You should use:
你应该使用:
doc = Word.Documents.Add(filename);
Instead of:
代替:
doc = Word.Documents.Open(filename);
So Word will use the template to create a document file, and not open the template itself. It seems Word behaves differently when the active document is a Template.
因此 Word 将使用模板创建文档文件,而不是打开模板本身。当活动文档是模板时,Word 的行为似乎有所不同。
回答by Asieh hojatoleslami
Use that, it should work:
使用它,它应该可以工作:
Word.Application WordApp;
Word.Document WordDoc;
object misValue = System.Reflection.Missing.Value;
WordApp = new Word.ApplicationClass();
WordDoc = WordApp.Documents.Open(filePath2, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
WordDoc.Activate();