2016-11-26 32 views
2

我有一個字符索引,並希望獲取包含該索引的頁面的Range。爲了說明這個特定的用例,我運行了句子驗證規則(使用自然語言處理),並且我想在用戶正在處理的整個頁面上運行它們。所以:VSTO Word:檢索範圍

public static class ExtensionForDocument 
{ 
    public static Range GetPage(this Document document, int characterIndex) 
    { 
     throw new NotImplementedException(); 
    } 
} 

一個例子電話:

public void OnInspect(IRibbonControl control) 
    { 
     var selection = Application.Selection; 
     var selectionRange = Application.ActiveDocument.GetPage(selection.Range.Start); 
     // Process range here. 
    } 

我已經試過:

public static Range GetPage(this Document document, int characterIndex) 
    { 
     var sectionCount = document.Sections.Count; 

     for (var sectionIndex = 1; sectionIndex <= sectionCount; sectionIndex++) 
     { 
      var section = document.Sections[sectionIndex]; 
      var sectionRange = section.Range; 
      if (characterIndex >= sectionRange.Start && characterIndex <= sectionRange.End) 
      { 
       return sectionRange; 
      } 
     } 

     return null; 
    } 

節沒有工作。因此,獲取實際頁面的範圍:

public static Range GetPage(this Document document, int characterIndex) 
{ 
    var numberOfPages = (int)document.Content.Information[WdInformation.wdNumberOfPagesInDocument]; 

    for (var p = 1; p <= numberOfPages; p++) 
    { 
     object what = WdGoToItem.wdGoToPage; 
     object which = WdGoToDirection.wdGoToAbsolute; 
     object count = p; 
     var range = document.GoTo(ref what, ref which, ref count); 
     if (characterIndex >= range.Start && characterIndex <= range.End) return range; 
    } 

    return document.Range(); 
} 

但是在這些範圍內,開始和結束表示頁碼,而不是字符索引。我也嘗試過使用書籤的相同功能,因爲我看到應該有「\ page」書籤 - 但沒有任何書籤。什麼是正確的方法?

注意:Word 2016年,Office工具在VS2015下.NET4.6.1

+1

首先:分析是否必須基於頁面?這不是直接的當前頁面的範圍,Word在段落和段落上的效果更好。因此,如果在一個部分或一系列段落上工作,您可能更喜歡這種方法。使用「\ page」書籤只能獲取當前頁面的範圍。您似乎在使用此書籤時遇到了問題,但您沒有包含相關的源代碼,所以我們無法知道實際問題是什麼。請注意,內置的「\ page」書籤是隱藏的,但仍然存在。 –

回答

0

你GETPAGE()是非常接近的解決方案。函數GoTo()在range.Start和range.End中返回頁面的第一個字符索引,並且不返回頁碼。因此,要獲取頁面的開始和結尾,您需要運行GoTo兩次。

不是最大的代碼。我在C#中使用了一些Java代碼約定,抱歉。

public Word.Range getPageByCharacterIndex(int characterIndex) { 
      var numberOfPages = (int)doc.Content.Information[Word.WdInformation.wdNumberOfPagesInDocument]; 
      int lastCharInDoc = doc.Range(0).End; 

      Word.Range rangeStart = null; ; 
      Word.Range rangeEnd = null; 
      for (var p = 1; p <= numberOfPages; p++) { 
       object what = Word.WdGoToItem.wdGoToPage; 
       object which = Word.WdGoToDirection.wdGoToAbsolute; 
       object count = p; 
       rangeStart = doc.GoTo(ref what, ref which, ref count); 
       object countPlusOne = p + 1; 
       rangeEnd = doc.GoTo(ref what, ref which, ref countPlusOne); 
       if (rangeStart.Start <= characterIndex && characterIndex <= rangeEnd.End) { 
        Word.Range result = doc.Range(rangeStart.Start, rangeEnd.End); 
        return result; 
       } 
      } 
      // ¿is characterIndex in last page? 
      if (rangeEnd !=null & rangeEnd.Start <= characterIndex && characterIndex <= lastCharInDoc) { 
       Word.Range result = doc.Range(rangeEnd.Start, lastCharInDoc); 
       return result; 
      } 

      return null; 
     }