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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - Checking if in a nested table and then getting out 2

Status
Not open for further replies.

jtreefrog

Technical User
Oct 26, 2004
22
0
0
US
I have a word document that has a nested table like this:

tables.gif


I need to run a macro that will perform the same function whether the cursor is in the nested table OR in the parent table. The macro inserts autotext into a new row (created below the current "parent" row). I can get the thing to insert the autotext at the end of the "parent" table, but that actually defeats the purpose of the macro.

For example, in the table above, let's say I want the macro to insert the autotext in between rows 2 and 3. I should be able to have the cursor anywhere in row 2(in the nested table or the "parent") and the macro will put the new row below the current "parent" (actually, I split the table below and insert autotext)

How do I use VBA to check if I'm located within the nested table then...if I am...get the cursor out of the nested table and to the end of the "parent" row so that I can do the autotext thing? And, subsequently, perform the autotext entry "regularly" if I'm not in the nested table?

(Am I making sense?)

Thanks,

Jon
 
*bump*

Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
Hi jtreefrog,

Interesting. It's easy enough to tell if you're in a nested table but there is no obvious way to get to the enclosing table when you are. The nested table's parent is the document. After a bit of playing, this seemed about the best I could do - I haven't tried with the autotext but this gets your selection into the outside table so gives you something to build on.

Code:
[blue]If Selection.Information(wdWithInTable) Then
    If Selection.Tables(1).NestingLevel > 1 Then
        Selection.Tables(1).Select
        Selection.Collapse wdCollapseEnd
    End If
End If[/blue]

Is that enough?

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.
 
I'll give it a shot and get back to you. Thanks!

Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
Code:
If Selection.Information(wdWithInTable) Then
    If Selection.Tables(1).NestingLevel > 1 Then
        Selection.Tables(1).Select
        Selection.Collapse wdCollapseEnd
        Selection.InsertRowsBelow 1
    Else
        Selection.InsertRowsBelow 1
    End If
End If

Gerry
 
Thanks, folks! That did the trick.

Any recommendations for better reference material than VB Help? Microsoft.com has the same material and if a person doesn't know to they should use "collapse" or "nestingLevel", they'd never figure it out (because the existing documentation makes an assumption that you just "know" what you need to look up)!

There's gotta be some good books out there.


Jon

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
IMHO the best books, at least for Word, but also general VBA, are both from Sybex. You can usually get them second hand somewhere. They are:

Word 2000 Developer's Handbook, by Gary Hart-Davis. While it is true than some things have changed with version 2003, for getting a handle on 99% of VBA itself (again, at least for Word), this is an excellent book. 1200+ pages. With a CD. For the life of me I have no idea why (and I am surprised their lawyers allowed it), but I was astonished to see a full break down (line-by-line with explanation) of the Melissa virus. Although it does give a very good undertanding of how to work with the registry and other applications. Fortunately, Microsoft has plugged the specific hole Mellisa used. However, it still does give some good clues on using registry keys and accessing Outlook. Kind of weird though.....you sort of have to to wash your hands after.

VBA Developer's Handbook (2nd Edition), by Ken Getz and Mike Gerbert. From basic to advanced VBA, from accessing Network resources, to using API's, from using MCI, to working with the IDE Object Model, this book has a LOT of information. 1100+ pages With a CD.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top