Solved

ignore error message and continue if path not found in VBA?

Posted on 2003-10-28
6
951 Views
Last Modified: 2010-08-05
I have the following piece of code that looks for various files, attaches them and then emails them. Problem is that some files are only there occaisionally (ie not every week), so when it can't find the path (varAttach(x - 1) = Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls") i get an error message and have to start again. I need a piece of code that will skip a path it can't find and move on to the next path without bringing up an error message.

PS any answers as simple as possible I'm not very good at VBA!

Many thanks in advance.

Private Sub Command0_Click()

  Dim varpeeps(10) As Variant
  Dim varAttach(10) As Variant
  Dim xlapp As Object
  Dim dbAddress As Database
  Dim Email As Recordset
  Dim x As Integer
  Dim DateBox As String
 
 
  Set dbAddress = CurrentDb
  Set Email = dbAddress.OpenRecordset("Email Addresses")
  DateBox = Forms![Form1]!DateBox
 
  DoCmd.SetWarnings False
 
 
  Email.MoveFirst
  Do Until Email.EOF
 
    For x = 1 To 7
      If IsNull(Email("Email Address " & x)) = False Then
        varpeeps(x - 1) = Email("Email Address " & x)
      End If
    Next x
    x = 0
   
    For x = 1 To 8
      If IsNull(Email("Account Number" & x)) = False Then
     
     
      varAttach(x - 1) = Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls"
      End If
    Next x
    x = 0
   
    Call SendNotesMail..........................................

    Email.MoveNext
   
    'Reset Arrays
    Erase varpeeps
    Erase varAttach
  Loop
   
  DoCmd.SetWarnings True
  dbAddress.Close
  MsgBox ("Complete")
End Sub
0
Comment
Question by:gybjw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

by:ampapa
ID: 9633652
Will this work? I think it might.

    For x = 1 To 8
      If IsNull(Email("Account Number" & x)) = False Then
     
      if dir(Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls") ="" then next
      varAttach(x - 1) = Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls"
      End If
    Next x
    x = 0

0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9633695
The simples way is to put the following line on the top of your Sub

On Error Resume Next

This will prevent your program to report an error every time it doesn't find a file, but it will only assign an error value to a variable err.

So the way you wuol use this is something like that:

On Error Resume Next

For x = 1 To 10

   Open file(x) For Input As #1              'generates error if file is missing
     If Err = 0 then
         'send that file through mail        
         Close #1  
     Else
         Err = 0                                'just clear the error value
     End If

Next X
   
0
 
LVL 11

Accepted Solution

by:
dbrckovi earned 125 total points
ID: 9633739
Instead of putting the line "On Error Resume Next"  on top of the Sub you can put it just before the piece of code where you would like it to take effect.

If you put it at the top of the sub, then every other possible error in that Sub would be ignored, and that can couse a lot of problems when debugging your program.

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 16

Expert Comment

by:JohnBPrice
ID: 9633794
Or you can check in advance with

dim fs as new filesystemobject ' you need to add a reference to microsoft scripting runtime

if fs.fileexists (Email("Path") & " " & Email("Account name") & " " & Email("Account Number" & x) & " September 2003.xls") then
    'do your email
endif
0
 

Author Comment

by:gybjw
ID: 9633892
Many thanks dbrckovi I never realised it could be so simple!
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9639678
Thanx!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question