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个回答

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问