Results 1 to 12 of 12

Thread: VB & Word & Error Trapping

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Just add On Error Resume Next to ignore any errors.
    Code:
    On Error Resume Next
    'clear the Err object
    Err.Clear
    WordApp.Selection.MoveDown Unit:=wdLine, Count:=1 
    WordApp.Selection.MoveRight Unit:=wdCell 
    If Err Then
        'Houston we have a problem
        Exit Sub 'or function
    End If
    WordApp.Selection.TypeText Text:=txtEndUser
    Good luck!

  2. #2
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    Joacim,

    All I can say is tanks 'again' - worked a treat.

    Can I trouble you for one more. This may not be worth the trouble and I can live without it.

    As standard all of our Word file footers contain a table. The standard is to place a blank line in the footer before inserting the table (just means that a nice space is left between the body of the document and the footer).

    Sometimes people get careless and the space is not put in.

    When I get into the footer, I automatically perform a WordApp.Selection.MoveDown Unit:=wdLine, Count:=1 to skip the blank line and get into the table.

    Is there any way to tell - when I get into the footer if I have landed on a blank line or have I landed in a table??

    Thanks,
    Graham.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Again I think the easiest way is to use the error trapping you already put in the code.
    Start by trying to move in the table if an error is raised then you're not in the table so move down one line and try again.
    If an error is raised the second time assume you don't have a table.
    Code:
    On Error Resume Next
    'clear the Err object
    Err.Clear
    WordApp.Selection.MoveRight Unit:=wdCell
    If Err Then
        'Okidoki we where not in a table so we move down
        'a line and try again.
        'But first we have to clear the Err object
        Err.Clear
        WordApp.Selection.MoveDown Unit:=wdLine, Count:=1 
        WordApp.Selection.MoveRight Unit:=wdCell 
        If Err Then
            'We got a new error so we probably don't have any
            'table here so bail out.
            Exit Sub 'or function
        End If
    End If
    WordApp.Selection.TypeText Text:=txtEndUser 
    'and the rest of the code....
    Good luck!

  4. #4
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    Thanks, I should have thought of that.

    I'm probably pushing my luck but..


    I have recently installed VB6 and I mistakenly didn't install the help files so I am a bit stuck on a simple item.

    When I get an error I want to bypass a section of the code to do with the table, I don't want to Exit Sub. (I presume I want to use the forbidden goto!!)

    Can you please tell me how to goto a label or skip x lines of code.

    Thanks.
    Graham.

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    I'm NOT going to show you how to use GoTo but it's pretty easy to skip a few lines if an error is trapped:
    Code:
    On Error Resume Next
    'clear the Err object
    Err.Clear
    WordApp.Selection.MoveRight Unit:=wdCell
    If Err Then
        'Okidoki we where not in a table so we move down
        'a line and try again.
        'But first we have to clear the Err object
        Err.Clear
        WordApp.Selection.MoveDown Unit:=wdLine, Count:=1 
        WordApp.Selection.MoveRight Unit:=wdCell 
    End If
    If Err = 0 Then
        'We didn't get any error so we are in a table
        'Put the code that handles the table here
    End If
    'and the rest of the code here....
    Good luck!

  6. #6
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    If Err = 0 Then - is exaclty what I was looking for.

    Cheers
    Graham.

    (P.S. If you are ever in Dublin I owe you a pint - assuming you're old enough etc..)

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well I'm 32 so I guess I'm old enough!
    I live in Sweden but if I start swimming now I suppose I'll be in Dublin in a couple of weeks
    But then you have to buy me a whiskey as well so I heat up

  8. #8
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    No problem.

    Last question I promise (well tonight anyway)

    On a previous occasion you gave me a routine to search sub-directories looking for Word files..

    sFilename = Dir(sPath & "*.doc")

    How can I add the search for "*.dot" files aswell (Word Template files) - can I use the OR or AND function.

    Thanks
    Graham.

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Nope! You have to use two loops!
    Code:
    Dim i As Integer
    Dim sExt As String
    For i = 0 To 1
        Ìf i Then 
            sExt = "*.doc"
        Else
            sExt = "*.dot"
        End If
        sFilename = Dir(sPath & sExt)
        Do While Len(sFilename)
            'the code that I don't remember right now
            sFilename = Dir
        Loop
    Next
    BTW I desided to walk over to Norway to make the swim a little bit shorter

    Best regards

    [Edited by Joacim Andersson on 09-09-2000 at 06:24 PM]

  10. #10
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    I'll have a pint of Guinness and a Paddys Irish Whiskey waiting.

  11. #11
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    Should the

    If i then

    read

    If i=0 then?

  12. #12

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    It really doesn't matter how you type it.
    If i Then is True if i got any value other than zero. So in this case it's the same as typing If i = 1 Then.
    Remember that False = 0 and True = -1 but anything that is not zero must be not false i.e. true.

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