操作方法:使用C#从Word文档页脚中删除文本 [英] How-to: Remove text from Word document footer using C#

查看:185
本文介绍了操作方法:使用C#从Word文档页脚中删除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用C#4从Word文档中删除页脚。页脚如下所示:

I'm trying to remove a footer from a Word document using C# 4. The footer looks like this:


18,2012

Page 1 April 18, 2012

实际上,这是Word VBA中显示的页脚文本:

Actually, this the text for the footer when displayed in Word VBA:


第1页(2012年4月18日

Page 1 ( April 18, 2012

1和April。最后页脚应如下所示:

There's actually a bullet character between "Page 1" and "April". In the end the footer should look like this:


2012年4月18日

April 18, 2012

无论如何,在Word VBA中,我可以使用这个代码:

Anyway, in Word VBA, I'm able to do it using this code:

Dim rngFtr As Range
Set rngFtr = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
rngFtr.Collapse wdCollapseStart
rngFtr.MoveStart wdParagraph, 1
rngFtr.MoveEnd wdWord, 4
rngFtr.Delete

这是我的代码在C#4:
使用Microsoft.Office.Interop.Word;

I tried the same thing in C# but it removes the footer entirely. Here's my code in C# 4: using Microsoft.Office.Interop.Word;

Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
Document doc = ap.Documents.Open(docFile.FullName, ReadOnly: false, Visible: false);
doc.Activate();
Microsoft.Office.Interop.Word.Range ftrRng =
    doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
ftrRng.Collapse(WdCollapseDirection.wdCollapseEnd);
ftrRng.MoveStart(WdUnits.wdParagraph, 1);
ftrRng.MoveEnd(WdUnits.wdWord, 4);
ftrRng.Delete();

ap.Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false);

((_Application) ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);

我甚至尝试了其他方法来摆脱第1页项目符号,如:

I even tried other ways to get rid of "Page 1" & the bullet, such as:

var replaceText = string.Empty;
object mis = System.Type.Missing;
var targetFooterText =
    doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text.ToString().Substring(1, 10);
doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Execute(
    targetFooterText,
    ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis,
    replaceText,
    ref mis, ref mis, ref mis, ref mis, ref mis);

这不会做任何事。

请让我知道我做错了什么。先谢谢。

Please let me know what I'm doing wrong. Thank you in advance.

我不确定这是否重要,但是该项目符号是Unicode-2022。

I'm not sure if this is important, but the bullet is a Unicode-2022.

这是文档页脚的屏幕上限。我只需要删除文本第1页&项目符号并用日期替换它。其他应保持不变。

Here's a screen cap of the document footer. I just need to remove the text "Page 1" & bullet and replace it with the date. The rest should remain as is.

推荐答案

在使用Word VBA进行挖掘和玩耍之后,我找到了一个替代解决方案。这是我的最终代码:

After some digging and playing around with Word VBA, I was able to find an alternate solution. Here's my final code:

Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();

Document doc = ap.Documents.Open(docFile.FullName, ReadOnly: false, Visible: false);
doc.Activate();

// These 3 lines did the trick.
doc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
doc.Application.Selection.MoveRight(WdUnits.wdCharacter, 1);
doc.Application.Selection.Delete(WdUnits.wdCharacter, 9);

ap.Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false);

((_Application) ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);

这篇关于操作方法:使用C#从Word文档页脚中删除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆