Results 1 to 6 of 6

Thread: [RESOLVED] Stepping through each line in Word2007 VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    13

    Resolved [RESOLVED] Stepping through each line in Word2007 VBA

    I know Excel VBA quite well, and VB in general, but I'm working in Word and I'm not terribly famlliar with the Word Object model.

    I can work within Word Tables quite well, provided the cursor is in one of the cells.

    But what I'd like to be able to do is step through each line of a word document to get a table count. Or if there's a way to jump from table to table that would be just as good.

    Can anyone point me to a quick code snippet that would get me started? I can build onto it after that.

    Thanks.
    Jeff

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Stepping through each line in Word2007 VBA

    Code:
    cnt = thisdocument.tables.count
    change document object to suit

    Code:
    for each t in thisdocument.tables
       msgbox t.cell(1, 1).range.text
       'whatever
    next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    13

    [SOLVED][WORD]Re: Stepping through each line in Word2007 VBA

    Quote Originally Posted by westconn1 View Post
    Code:
    cnt = thisdocument.tables.count
    change document object to suit

    Code:
    for each t in thisdocument.tables
       msgbox t.cell(1, 1).range.text
       'whatever
    next
    Ah, many thanks westconn1. That will be a great start for me. - Jeff

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    13

    Re: Stepping through each line in Word2007 VBA

    Quote Originally Posted by westconn1 View Post
    Code:
    cnt = thisdocument.tables.count
    change document object to suit

    Code:
    for each t in thisdocument.tables
       msgbox t.cell(1, 1).range.text
       'whatever
    next
    Just thought I would say that I didn't get this to work as posted (probably my fault). But when I used ActiveDocument instead of thisdocument, it did.

    thanks.
    Jeff

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Stepping through each line in Word2007 VBA

    note the line
    change document object to suit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    13

    Re: [RESOLVED] Stepping through each line in Word2007 VBA

    Quote Originally Posted by westconn1 View Post
    note the line
    change document object to suit

    Yup, my bad. Thanks.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width