Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Tables in Word using VB Macro

Status
Not open for further replies.

alexander1113

Programmer
Dec 26, 2004
26
US
To whomever can assist,
Does anyone know how to access a table in word using VB macro.

So far i have this:

Set myCell = ActiveDocument.Tables(count).Cell(Row:=1, Column:=1)
myCell = Left$(myCell, Len(myCell) - 2)

What I'm trying to do is access the cell so i can save what's in there in memory. But this does it for the whole document so i have to specify a count. I want to access it page by page. So on page 1 i want to access the first table, page 2, the same. I hope i provided enough information. Thanks in advance.

 
I don't think a "page" is an inherent property of the doc, that's just where the text happens to flow.
You have a collection of tables within that portion (story range?) of the doc. You may be able to select the story, then look at each table, decide which page the table is on? then perform operations on that table.

You shouldn't design a doc that depends on a table being on a particular page, unless you build in page breaks or section breaks to ensure the location. If those breaks are in place, then you can use them to locate the pages and tables on the pages.
Whadda mean "save what's in there in memory"?

jay
 
Hi alexander1113,

myCell is a reference to the cell itself - not the contents. Use, instead, myCell[red].Range.Text[/red] ..

[blue][tt] myCellText = Left$(myCell.Range.Text, Len(myCell.Range.Text) - 2)[/tt][/blue]

I don't completely understand your question about tables on pages. Do you want to access the first table on each page? This isn’t entirely straightforward or reliable because page flow depends on the printer but the following (including the line above) should do it.

[blue]
Code:
With Selection

    .HomeKey wdStory
    
    Do
        
        If .Bookmarks("\Page").Range.Tables.Count > 0 Then
            Set myCell = .Bookmarks("\Page").Range.Tables(1).Cell(Row:=1, Column:=4)
            mycelltext = Left$(myCell.Range.Text, Len(myCell.Range.Text) - 2)
            [green]' Do what you want with he cell text[/green]
        End If
        
        If .Information(wdActiveEndPageNumber) _
         = .Information(wdNumberOfPagesInDocument) Then Exit Do
         
        .GoTo wdGoToPage, wdGoToNext, 1
        
    Loop

End With
[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Thanks everyone for your replies. I actually did it by just moving the cursor because for every page there's a set number of characters to move to access the table. Thanks again.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top