Gab*_*abe 7 c# ms-word header footer office-interop
在C#中,使用wordDocument.Fields.Update()对主体中的所有内容都很有效.
是否有简洁优雅的方法来更新页眉和页脚中的字段?
这个花了我一点.不得不深入了解Iterop.Word文档.在VB中有很多这样的例子,但我认为下面的代码是C#中的一个很好的例子.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main(string[] args)
{
List<string> path = new List<string>(args);
string filePathstr = string.Join(" ", path.ToArray());
//System.Windows.Forms.MessageBox.Show("filepathstr: " + filePathstr);
string folderPathstr = Path.GetDirectoryName(filePathstr);
//System.Windows.Forms.MessageBox.Show("folderPathstr: " + folderPathstr);
try
{
Application ap = new Application();
Document document = ap.Documents.Open(filePathstr);
document.Fields.Update();
foreach (Section section in document.Sections)
{
document.Fields.Update(); // update each section
HeadersFooters headers = section.Headers; //Get all headers
foreach (HeaderFooter header in headers)
{
Fields fields = header.Range.Fields;
foreach (Field field in fields)
{
field.Update(); // update all fields in headers
}
}
HeadersFooters footers = section.Footers; //Get all footers
foreach (HeaderFooter footer in footers)
{
Fields fields = footer.Range.Fields;
foreach (Field field in fields)
{
field.Update(); //update all fields in footers
}
}
}
document.Save();
document.Close();
}
catch (NullReferenceException)
{
System.Windows.Forms.MessageBox.Show("A valid file was not selected.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要对Word文档中的每个字段执行更复杂的操作,所以我创建了一个更通用的迭代器扩展方法:
public static class WordDocument
{
public static IEnumerable<Field> GetAllFields(this Document _document)
{
// Main text story fields (doesn't include fields in headers and footers)
foreach (Field field in _document.Fields) {
yield return field;
}
foreach (Section section in _document.Sections) {
// Header fields
foreach (HeaderFooter header in section.Headers) {
foreach (Field field in header.Range.Fields) {
yield return field;
}
}
// Footer fields
foreach (HeaderFooter footer in section.Footers) {
foreach (Field field in footer.Range.Fields) {
yield return field;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在可以将原始示例代码更改为以下内容:
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main(string[] args)
{
List<string> path = new List<string>(args);
string filePathstr = string.Join(" ", path.ToArray());
string folderPathstr = Path.GetDirectoryName(filePathstr);
try
{
Application ap = new Application();
Document document = ap.Documents.Open(filePathstr);
foreach (Field field in document.GetAllFields())
{
field.Update();
// Other stuff with field can be done here as well
}
document.Save();
document.Close();
}
catch (NullReferenceException)
{
System.Windows.Forms.MessageBox.Show("A valid file was not selected.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
嗯,用的是这个:http://blog.vanmeeuwen-online.nl/2009/09/update-all-fields-in-word-include.html,适应C#,只是省略了条件If aField.Type = Word.WdFieldType.wdFieldDocProperty。
效果很好。
| 归档时间: |
|
| 查看次数: |
1989 次 |
| 最近记录: |