【问题标题】:MS Word Automation - find the page number where a table is locatedMS Word 自动化 - 查找表格所在的页码
【发布时间】:2017-05-23 08:36:33
【问题描述】:

我有一个包含 10 个表格的 Word 文档。当我读取这些表中的值时,我想警告操作员检查第 5 页的表 4。 我可以得到表号,但是有没有办法在那个表的页面上指出?我的代码片段如下:

int nTable = 0;
int nPage = 0;
foreach (Word.Table tb in doc.Tables)
{
  nTable++;
  nPage = PageNumberForTable(nTable); // I need a function like this
  numRows = tb.Rows.Count;
  numColumns = tb.Columns.Count;

  for (int row = 1; row <= numRows; row++) 
  {
    for(int col = 1; col <= numColumns; col++) 
    {
        var cell = tb.Cell(row, col); 
        cellValue = CleanRASpace(cell.Range.Text); 
        if(cellValue == 2) 
        {
           MessageBox.Show("Check table " + nTable + " on page " + nPage);
        }
    }
  }
}

【问题讨论】:

    标签: c# ms-word automation office-interop


    【解决方案1】:

    您可以获得表格所在的页码:

    table.Range.Information[Word.WdInformation.wdActiveEndAdjustedPageNumber];
    

    当你说你需要这样的功能时:

    nPage = PageNumberForTable(nTable);
    

    似乎很奇怪……我认为您可能需要两种方法,一种从名称(标题)中获取表格,另一种从给定页面上获取所有表格。为了获得给定页面上的表格,我使用了一个列表来保存它们,因为该页面上可能有多个表格。在 Word 文档中,我将表格 Titles 设置为 table1、table2、table3……等等……此外,如果表格拆分页面,它将返回表格结束的页面。希望这会有所帮助。

    private static int GetTablePageNumberFromTitle(string inTitle, Word.Document doc) {
      foreach (Word.Table tb in doc.Tables) {
        if (tb.Title == inTitle) {
          return tb.Range.Information[Word.WdInformation.wdActiveEndAdjustedPageNumber]; 
        }
      }
      return -1;
    }
    
    private static List<Word.Table> GetTablesOnPage(int targetPage, Word.Document doc) {
      List<Word.Table> tablesOnPage = new List<Word.Table>();
      int curPage = -1;
      foreach (Word.Table tb in doc.Tables) {
        curPage = tb.Range.Information[Word.WdInformation.wdActiveEndAdjustedPageNumber];
        if (curPage == targetPage) {
          tablesOnPage.Add(tb);
        }
      }
      return tablesOnPage;
    }
    

    一些测试

      Console.WriteLine("--------------");
      Console.WriteLine("Get page table named 'table3' is on...");
      int pageNum = GetTablePageNumberFromTitle("table3", doc);
      Console.WriteLine("'table3 is on page: " + pageNum);
      Console.WriteLine("--------------");
      Console.WriteLine("Get page table named 'table2' is on... It starts on page 2 and ends on page 3");
      pageNum = GetTablePageNumberFromTitle("table2", doc);
      Console.WriteLine("'table2 is on page: " + pageNum);
      Console.WriteLine("--------------");
      Console.WriteLine("Get tables on page 4");
      List<Word.Table> allTables = GetTablesOnPage(4, doc);
      foreach (Word.Table tb in allTables) {
        Console.WriteLine(tb.Title + " is on page " + 4);
      }
      Console.WriteLine("--------------");
      Console.WriteLine("Get tables on page 5");
      allTables = GetTablesOnPage(5, doc);
      foreach (Word.Table tb in allTables) {
        Console.WriteLine(tb.Title + " is on page " + 5);
      }
    

    【讨论】:

    • 约翰,非常感谢。您提供的代码工作正常。我不需要创建函数(我试图尽可能清楚地解释它,但我造成了更多的混乱:))。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    相关资源
    最近更新 更多