none
Reading a table in a header RRS feed

  • Question

  • When I try to read a table properties that is held in the header, the table method does not detect the table

    sample code

    Document document = application.Documents.Open(HeaderDocument);
    document.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
    document.GoTo(ref what, ref which, ref count, ref missing);
    Document.ActiveWindow.ActivePane.View.SeekView = _
          Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
    foreach (Microsoft.Office.Interop.Word.Table tables in document.Tables)
    {
      // Table
      PreferredWidth = tables.PreferredWidth;
      fPreferredWidthType = tables.PreferredWidthType;
      iAllowBreakAcrossPages =tables.Rows.AllowBreakAcrossPages;
      //
    }

    If I try yo access the table directly via tables[1] the program throws an exception. So currently I can't read the table. I have looked at the internet but cant find any notes that covers how to read a table held in the header. I am using a .doc document

    Thursday, March 12, 2015 9:17 AM

Answers

  • Hi Ahernek

    A very important concept when working with the content of a Word document is the RANGE object. You usually won't see this when working with code generated by the Macro Recorder because that bases on the Selection object. You can think of a Range like an invisible selection with some major differences: there can be only one Selection in a given instance of the Word application while you can have many Range objects; working with Selection will change what's displayed in the Word application window on the screen while working with Ranges is transparent; working with Range objects will be safer because the user can't accidently interfere by clicking or using the Arrow keys.

    With this in mind: What you need to do is specifically address the Header Range that contains the table. The code you currently have is trying to locate a table in the MainStory StoryRange of the document. In order to get the RAnge of default header of the page where the Selection currently is you'd use something like

      Word.Range rng = wdApp.Selection.Sections[1].Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
      Word.Table tbl = rng.Tables[1];


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, March 12, 2015 6:59 PM
    Moderator