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

Can I test for an xls file exist from a separate xls file

Status
Not open for further replies.

andycapp28

Technical User
Mar 2, 2010
86
GB
This is continuing on from some code I wrote where an excel spreadsheet had Column E holding email addresses. This column E was read and for each cell containing an address it would select Column A containing name of excel file to be put in an email.
I need to refine this as I need to ensure the excel file in Column A exists and only if it does send an email.

Finally just to ask if .From is valid to use object

My existing code is as follows;
Code:
Sub Email_ESS()
'
' Email all supported Evs held in excel file and _
attach his/her file from Monthly Query results
'

Dim r As Range, c As Range
Dim rLookfor As Range
Dim rLookin As Range

With Application
Application.ScreenUpdating = False
Application.EnableEvents = False
End With

With Worksheets
' Set rLookin = Range(Cells(2, "E"), Cells(2, "E").End(xlDown)) TOOK USE of this out as couldn't get to compile
End With

'For Each r In rLookin TOOK THIS OUT, COULDn't use as not get to compile

For Each r In Range(Cells(2, "E"), Cells(2, "E").End(xlDown))
' Set r = rLookin.Find(r.Value) NOT USING (see above)

If Not r Is Nothing Then

ESubject = "ESS_Report"
sendto = r.Value
NewFileName = r.Offset(0, -4)

Set App = CreateObject("Outlook.Application")
Set Itm = App.CreateItem(0)

With Itm
.Subject = ESubject
.to = sendto
.Attachments.Add "G:\Marketing\Management and admin\Monthly Management Reports\ESS & EIT Monthly Reporting\NewFileName"
.Send
End With

Set App = Nothing
Set Itm = Nothing

End If
Next

End Sub
 



Hi,

Some thoughts.

When your turn these off, then you must turn them on when you're done. It's good housekeeping!
Code:
    Application.ScreenUpdating = False
    Application.EnableEvents = False
This With...End With OBJECT makes no sense.
Code:
    With Worksheets
    ' Set rLookin = Range(Cells(2, "E"), Cells(2, "E").End(xlDown)) TOOK USE of this out as couldn't get to compile
    End With
This should refer to a Worksheet Object not the Worksheets collection.
Code:
    With Worksheets("[b]Your Sheet Name HERE[/b]")
     Set rLookin = .Range(.Cells(2, "E"), .Cells(2, "E").End(xlDown)) '''TOOK USE of this out as couldn't get to compile
    End With
This makes no sense, as r exists until the end of the collection
Code:
        If Not r Is Nothing Then
I think that you mean if the VALUE in object r is not [no string]
Code:
        If r.Value <> "" Then
Now to your question
Code:
    On Error Resume Next
    .Attachments.Add "G:\Marketing\Management and admin\Monthly Management Reports\ESS & EIT Monthly Reporting\NewFileName"
    If Err.Number = 0 Then
        .Send
    Else
        Err.Clear
    End If
    On Error GoTo 0
Also, I'd suggest using the server name rather than a DOS Drive.



Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 

Unless your file will always be named NewFileName (with no extension), consider this:

Code:
On Error Resume Next
.Attachments.Add "G:\Marketing\Management and admin\Monthly Management Reports\ESS & EIT Monthly Reporting\"[blue] & NewFileName[/blue]
    If Err.Number = 0 Then
        .Send
    Else
        Err.Clear
    End If
    On Error GoTo 0

Have fun.

---- Andy
 
Thanks Skip and Andy

I will take this on board and give it a go tomorrow.

AC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top