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

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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配