3

使用 选择页眉范围时.Select(),Microsoft Word 会自动从我当前的视图类型(打印布局)切换到草稿视图。如何阻止 Word 切换到草稿视图?

下面的代码示例演示了我在做什么:

 // this.Document is a Microsoft.Office.Interop.Word.Document
 Section section = this.Document.Sections.First;
 foreach (HeaderFooter header in section.Headers)
 {
   if (header.Exists) 
   {
     header.Range.Select(); // When I call this, Word switches to Draft View.
     break;
   }
 }

编辑(3):

显然保存视图类型并重置它确实有效。但是,当 Word 切换到草稿视图然后返回到打印布局时,这会导致令人讨厌的闪烁。此外,当我双击主文档空间以退出页眉部分时,Word 切换回草稿视图。

 WdViewType viewType = this.Document.ActiveWindow.View.Type;
 range.Select();
 this.Document.ActiveWindow.View.Type = viewType;
4

3 回答 3

4

View.SeekView在选择范围之前,必须为除 wdNormalView(草稿视图)之外的所有视图类型设置该属性。

var window = this.Document.ActiveWindow;
// wdNormalView == Draft View, where SeekView can't be used and isn't needed.
if (window.View.Type != WdViewType.wdNormalView)
{
  // -1 Not Header/Footer, 0 Even page header, 1 Odd page header, 4 First page header
  // 2 Even page footer, 3 Odd page footer, 5 First page footer
  int rangeType = range.Information[WdInformation.wdHeaderFooterType];
  if (rangeType == 0 || rangeType == 1 || rangeType == 4)
    window.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
  if (rangeType == 2 || rangeType == 3 || rangeType == 5)
    window.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
}
header.Range.Select();
于 2012-12-12T13:44:42.787 回答
1

7 年后,但在我的 中使用 Joe W 的答案时Word VSTO Addin,我遇到了一个问题并决定在此处发布修复:

BeforeView.SeekView设置为WdSeekView.wdSeekCurrentPageHeaderor WdSeekView.wdSeekCurrentPageFooter,检查是否View.SeekView已设置为页眉或页脚值。如果是,则抛出异常,因为当前View.SeekView无法更改为wdSeekCurrentPageHeaderifView.SeekView已设置为页眉值,并且当前View.SeekView无法更改为wdSeekCurrentPageFooterifView.SeekView已设置为页脚值。如果在执行代码以调整视图设置(例如,在 Word VSTO 插件中)时用户正在编辑页眉或页脚,则可能会出现这种情况。

此代码有效。它包括View.SeekView在调用之前保存设置的代码,SetView并在调用SetView. 这考虑到用户在SetView被调用时正在编辑页眉或页脚的场景,这可能是Word VSTO Addin.

// ... some Word VSTO Addin code that calls SetView 

Word.WdSeekView enumSavedSeekView = Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView;

bool viewResetNeeded = SetView(workingStoryRange);

if (viewResetNeeded)
{
    Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView = enumSavedSeekView;
}

// ... end of some Word VSTO Addin code that calls SetView 

private bool SetView(Word.Range range)
{
    bool viewResetNeeded = false;

    // wdNormalView == Draft View, where SeekView can't be used and isn't needed.
    if (Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.Type != Word.WdViewType.wdNormalView)
    {
        // -1 Not Header/Footer, 0 Even page header, 1 Odd page header, 4 First page header
        // 2 Even page footer, 3 Odd page footer, 5 First page footer

        switch (range.Information[Word.WdInformation.wdHeaderFooterType])
        {
            case 0:
            case 1:
            case 4:

                if ((Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView != Word.WdSeekView.wdSeekEvenPagesHeader) && (Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView != Word.WdSeekView.wdSeekFirstPageHeader) && (Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView != Word.WdSeekView.wdSeekPrimaryHeader))
                {
                    Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
                    viewResetNeeded = true;
                }

                break;

            case 2:
            case 3:
            case 5:

                if ((Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView != Word.WdSeekView.wdSeekEvenPagesFooter) && (Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView != Word.WdSeekView.wdSeekFirstPageFooter) && (Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView != Word.WdSeekView.wdSeekPrimaryFooter))
                {
                    Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
                    viewResetNeeded = true;
                }

                break;

            default:
                break;
        }
    }

    return viewResetNeeded;
于 2019-07-31T17:51:06.770 回答
-1

您可以利用范围对象中的 Words 集合来选择标题

 Section section = this.Document.Sections.First;
 foreach (HeaderFooter header in section.Headers)
 {
   if (header.Exists) 
   {
     header.Range.Words.First.Select()
     break;
   }
 }
于 2012-12-06T17:48:47.743 回答