z401095788
我真不懂NET
2016-05-23 02:59

c# word 如何清空页眉页脚

10

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

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

为你推荐