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

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);//清空之前的页眉

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题