CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Location
    Scotland
    Posts
    7

    Exiting a For Next Loop

    Hey chaps and chapess's I know this is really basic stuff for you but assistance would be most appreciated.

    I have set up a For Next loop that cuts and pastes tables another place in a word 2000 document.

    Since the number of tables present varies (anything from 5 to 50) I have set the For value at 50
    My problem is that I cannot figure out how to get the routine to Exit the loop when the error message appears stating that I have "run out of files"

    Is there a simple line I can add?

    Thank you in advance.
    Gem_man

    PS If this is an irritatingly simple question can anyone suggest a forum that will be hapy to answer my numerous ridiculously simple queries?

  2. #2
    Join Date
    Nov 2002
    Location
    Oakville, Ontario
    Posts
    48
    For i = o to 50
    'your code
    if ....then
    Exit For
    end if

    next i

    If that is what you are getting at (?) The Exit For line will break the loop. Cheers

    DA

  3. #3
    Join Date
    Jan 2003
    Location
    Scotland
    Posts
    7
    Well sort of.
    The problem is that the number of tables that I move, is not fixed.

    Unless I can get the code to count the number of tables present in the document, I cant see how I could use the If Then statement.

    Is there no code line that basically says "if an error occurs then exit the loop"?

    Failing that, is it possible for VB to be able to count how many tables are in a document?
    Then I could just set the For figure to this.

    Thank you for your help

    Adrian

  4. #4
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646
    To get table count in Word VBA use the tables property

    Selection.Document.Tables.Count

    for error traping in a loop:

    on error resume next
    for i=1 to 50
    ...do something
    if err.number <> 0 then
    err.clear
    exit for
    end if
    next

  5. #5
    Join Date
    Jan 2003
    Location
    Scotland
    Posts
    7
    Thank you Luthv thats exactly what I need.

    Very grateful to you
    Gem_man

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

Featured