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

Attachment Help

Status
Not open for further replies.

gloudo

Programmer
Feb 3, 2002
34
NL
Hi There,

I need Some help with a problem in Excel.

I have a workbook in Excel wich need to be send by mail.
With a button in the workbook , the workbook has to be saved
as "anyName.xls" and then attached in outlook.

Now the SaveAs part works only when the name stays the same
as the original workbook-name. When I alter the name of the workbook, The attachment can't be made. An error pops up with 'Can't find file'.

What Am I doing wrong?

Here's the code:

Private Sub okbtn_Click()
On Error GoTo Err_okbtn_Click

Start:

Dim sFileName As String
Dim objOL As New Outlook.Application
Dim objMail As MailItem
Set objOL = New Outlook.Application
Set objMail = objOL.CreateItem(olMailItem)

sFileName = Application.GetSaveAsFilename( _
fileFilter:="Microsoft Excel-Werkmap(*.xls), *.xls")

If sFileName = "False" Then
ActiveWorkbook.SaveAs Filename:=sFileName
Else
DoCmd.CancelEvent

End If

Mail:

With objMail
.To = Form1.sReciptxt.Text
.CC = "Hans Zwennes"
.Subject = "Probleemhoek Formulier" & " " & Date
.Body = "Dit Is een Test"
.Display
.Attachments.Add sFileName
End With

Exit_okbtn_Click:
Set objMail = Nothing
Set objOL = Nothing
Unload Form1
Exit Sub

Err_okbtn_Click:
MsgBox Err.Description
Resume Exit_okbtn_Click

End Sub
 
Just checking - the logic of the if/else/endif statement following your GetSaveAsFilename seems turned around. Is it correct in your actual code?
Rob
 
I agree with Rob, your logic appears twisted. It should be

If sFileName <> &quot;False&quot; Then

The reason you are probably getting the &quot;Can't find file&quot; error is because under your current logic, you are trying to attach a file named False, without specifying a path. Jon Hawkins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top