Consulting

Results 1 to 3 of 3

Thread: Searching sections

  1. #1

    Searching sections

    I have a document with several sections. In one section is a particular table that I reference with some Macros. Becuase others routinely edit the base document and could possible add tables above or below the table in question, I am running into some trouble. What I want to do is isolate this table in it's own section, and then search only that section for the correct table. The Tables collection ignores sections so I am not sure how to identify the first table in a given section.

    Any Ideas?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi tla,

    If your users can add tables to the document, there's noting to stop them from adding more tables to the section your table is in.

    I suggest bookmarking the table, then selecting the table in the bookmarked range. For example, suppose the table's bookmark is named 'myTbl'. You could use something along the lines of:
    Sub UpdateTable()
    With ActiveDocument.Bookmarks("myTbl").Range.Tables(1)
      .Cell(1, 2).Range.Text = "Hello"
      .Cell(2, 2).Range.Text = "Goodbye"
    End With
     
    End Sub
    to insert "Hello" into B1 and "Goodbye" into B2.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    To extend that thought, you can make a table object of that table - after you bookmark it as macropod suggests. Now it does not matter if you put tables before it, or move the table itself, or whatever. Assuming the same name as macropod...[vba]Dim oTable As Table
    Set oTable = ActiveDocument.Bookmarks("myTbl").Range.Tables(1)

    With oTable
    .Cell(1, 2).Range.Text = "Hello"
    .Cell(2, 2).Range.Text = "Goodbye"
    End With

    [/vba]The advantage of this is that you can access all the properties and methods of THAT table. If you declare oTable as a global (Public) variable, and Set it, you can use it in as many Sub routines as you like. It will no difference what Section it is in.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •