我真不懂NET 2016-05-23 02:59 采纳率: 80%
浏览 2355
已采纳

c# word 如何清空页眉页脚

现在实现的功能只能追加页眉页脚,而不是替换页眉页脚,之前的页眉页脚无法删除,求懂这方面的大神指导以下如何清空页眉页脚
附上实现追加页眉页脚代码

public static void AddPageHeaderFooter(string filePath)
{
Object oMissing = System.Reflection.Missing.Value;
object path = filePath;
Application WordApp = new Application();
Document mydoc = WordApp.Documents.Open(ref path, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
try
{
mydoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekPrimaryHeader;//页眉插入图片 指向除首页页眉
mydoc.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(LogoPic, ref oMissing, ref oMissing, ref oMissing);

            //二、页脚
            //页脚包括文字和页码,文字居中,页码右对齐
            mydoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//获取当前须编辑区域
            object oNumberAlignment = WdPageNumberAlignment.wdAlignPageNumberRight;
            object oFirstPage = true;

            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
            mydoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;//页眉插入图片  指向除首页页眉
            mydoc.ActiveWindow.ActivePane.Selection.Text = null;
            //当前编辑区域页眉页脚文字居中
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            //当前编辑区域页眉页脚字体
            WordApp.Selection.HeaderFooter.Range.Font.Name = "宋体";
            WordApp.Selection.HeaderFooter.Range.Font.Size = 10.5f;
            WordApp.Selection.HeaderFooter.Range.Font.Bold = 0;


            WordApp.Selection.TypeText("  填志愿,更轻松!   第");
            object page = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;//当前页码对象
            WordApp.Selection.Fields.Add(WordApp.Selection.Range, ref page, ref oMissing, ref oMissing);  
            WordApp.Selection.TypeText("页 共");
            object pages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;//当前文档总页数对象
            WordApp.Selection.Fields.Add(WordApp.Selection.Range, ref pages, ref oMissing, ref oMissing);
            WordApp.Selection.TypeText("页");

            //退出页眉页脚编辑模式
            mydoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

            //保存
            mydoc.Save();
            mydoc.Close(ref oMissing, ref oMissing, ref oMissing);
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
        }
        catch (Exception e)
        {
            //退出页眉页脚编辑模式
            mydoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
            //保存
            mydoc.Save();
            mydoc.Close(ref oMissing, ref oMissing, ref oMissing);
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            throw e;
        }
    }
  • 写回答

1条回答

  • 我真不懂NET 2016-05-23 03:23
    关注

    通过自己的一番测试与摸索,实现了清空页眉页脚
    附上实现代码:
    mydoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekPrimaryHeader;// 指向除首页页眉
    WordApp.Selection.HeaderFooter.Range.Delete(oMissing,oMissing);//清空之前的页眉

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题