Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Email a file as an attachment in Outlook 2000 using Excel VB macro

Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Email a file as an attachment in Outlook 2000 using Excel VB macro

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Email a file as an attachment in Outlook 2000 using Excel VB m

My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
..Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Email a file as an attachment in Outlook 2000 using Excel VB m

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Email a file as an attachment in Outlook 2000 using Excel VB m

The code you sent me works, however, it does not test for the existence of
the intended file to be attached. Therefore, I still have to customize it to
detect for the existence of the file before sending an email. You provided me
with the structure. I appreciate that. Thanks.

"Ron de Bruin" wrote:

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Email a file as an attachment in Outlook 2000 using Excel VB m

It test it

If Dir(myvar) < "" Then
.Attachments.Add myvar
End If



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
The code you sent me works, however, it does not test for the existence of
the intended file to be attached. Therefore, I still have to customize it to
detect for the existence of the file before sending an email. You provided me
with the structure. I appreciate that. Thanks.

"Ron de Bruin" wrote:

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Email a file as an attachment in Outlook 2000 using Excel VB m

It apparently tests to see if the variable is assigned, not if the file
exists. Also, the code continues and either sends or displays the message
without the attachment.

"Ron de Bruin" wrote:

It test it

If Dir(myvar) < "" Then
.Attachments.Add myvar
End If



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
The code you sent me works, however, it does not test for the existence of
the intended file to be attached. Therefore, I still have to customize it to
detect for the existence of the file before sending an email. You provided me
with the structure. I appreciate that. Thanks.

"Ron de Bruin" wrote:

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Email a file as an attachment in Outlook 2000 using Excel VB m



"Ron de Bruin" wrote:

It test it

If Dir(myvar) < "" Then
.Attachments.Add myvar
End If



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
The code you sent me works, however, it does not test for the existence of
the intended file to be attached. Therefore, I still have to customize it to
detect for the existence of the file before sending an email. You provided me
with the structure. I appreciate that. Thanks.

"Ron de Bruin" wrote:

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Email a file as an attachment in Outlook 2000 using Excel VB m

This is a segment of the sample code I use to check for the existence of the
file that I plan to attach to the email:
Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String
sourcedir = "C:\Tmp\"
ChDir sourcedir
RfileType1 = "????file.zip"
zipfile = Dir(RfileType1)
myvar = sourcedir & zipfile
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
strbody = "This is the monthly zip file." & vbNewLine & _
"Line 1." & vbNewLine & _
"Thank you." & vbNewLine & vbNewLine _
On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the subject line."
.Body = strbody

If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.Display 'Displays the email dialog box but does not send the
email.
'.Send 'Sends the email without displaying the dialog box.
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
end sub

"Ron de Bruin" wrote:

It test it

If Dir(myvar) < "" Then
.Attachments.Add myvar
End If



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
The code you sent me works, however, it does not test for the existence of
the intended file to be attached. Therefore, I still have to customize it to
detect for the existence of the file before sending an email. You provided me
with the structure. I appreciate that. Thanks.

"Ron de Bruin" wrote:

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Email a file as an attachment in Outlook 2000 using Excel VB m

It will test if the file exist to avoid a error when you want to add it

It will send the mail with only text if the file not exist
Do you not want to send the mail when the file not exist ?

Then do the test after this line
myvar = Application.DefaultFilePath & "\" & "test.txt"

Try

Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

If Dir(myvar) < "" Then

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
.Attachments.Add myvar
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "Sorry file not exist"
End If
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
It apparently tests to see if the variable is assigned, not if the file
exists. Also, the code continues and either sends or displays the message
without the attachment.

"Ron de Bruin" wrote:

It test it

If Dir(myvar) < "" Then
.Attachments.Add myvar
End If



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
The code you sent me works, however, it does not test for the existence of
the intended file to be attached. Therefore, I still have to customize it to
detect for the existence of the file before sending an email. You provided me
with the structure. I appreciate that. Thanks.

"Ron de Bruin" wrote:

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub





  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Email a file as an attachment in Outlook 2000 using Excel VB m

The code is ready and works. Thanks for your assistance.

"Ron de Bruin" wrote:

It will test if the file exist to avoid a error when you want to add it

It will send the mail with only text if the file not exist
Do you not want to send the mail when the file not exist ?

Then do the test after this line
myvar = Application.DefaultFilePath & "\" & "test.txt"

Try

Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

If Dir(myvar) < "" Then

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
.Attachments.Add myvar
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "Sorry file not exist"
End If
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
It apparently tests to see if the variable is assigned, not if the file
exists. Also, the code continues and either sends or displays the message
without the attachment.

"Ron de Bruin" wrote:

It test it

If Dir(myvar) < "" Then
.Attachments.Add myvar
End If


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
The code you sent me works, however, it does not test for the existence of
the intended file to be attached. Therefore, I still have to customize it to
detect for the existence of the file before sending an email. You provided me
with the structure. I appreciate that. Thanks.

"Ron de Bruin" wrote:

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub




  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Email a file as an attachment in Outlook 2000 using Excel VB m

You are welcome

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
The code is ready and works. Thanks for your assistance.

"Ron de Bruin" wrote:

It will test if the file exist to avoid a error when you want to add it

It will send the mail with only text if the file not exist
Do you not want to send the mail when the file not exist ?

Then do the test after this line
myvar = Application.DefaultFilePath & "\" & "test.txt"

Try

Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

If Dir(myvar) < "" Then

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
.Attachments.Add myvar
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "Sorry file not exist"
End If
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
It apparently tests to see if the variable is assigned, not if the file
exists. Also, the code continues and either sends or displays the message
without the attachment.

"Ron de Bruin" wrote:

It test it

If Dir(myvar) < "" Then
.Attachments.Add myvar
End If


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
The code you sent me works, however, it does not test for the existence of
the intended file to be attached. Therefore, I still have to customize it to
detect for the existence of the file before sending an email. You provided me
with the structure. I appreciate that. Thanks.

"Ron de Bruin" wrote:

This small test is working for me.
Good night, bed time for me


Sub Test()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myvar As String

myvar = Application.DefaultFilePath & "\" & "test.txt"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
If Dir(myvar) < "" Then
.Attachments.Add myvar
End If
.display 'or use .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
My test file name 0706mira.zip. I do change directory to the folder to check
for its existence.

To get the file name to be emailed, I use:
RfileType1 = "????mira.zip"
workfile = Dir(RfileType1)
.Attachments.Add workfile

The system responds saying it cannot find the file, which is in the expected
folder.

"Ron de Bruin" wrote:

Hi Freddy

Use

..Attachments.Add Range("C1").Value

Or

..Attachments.Add MyVar


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Freddy" wrote in message ...
Does anyone have Excel VBA code to email a file as an attachment in Outlook
2000 using a variable? The file name is not static. It changes every month.
Please note the code below, which is not executed, where it reads:
'.Attachments.Add ("C:\test.txt"). I want to use a variable instead of the
hard-coded path and file "C:\test.txt".


Sub Mail_workbook_Outlook()
'This example sends the last saved version of each open workbook
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim Wb As Workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "Monthly File Attachment"
.Body = "Hi there"
For Each Wb In Application.Workbooks
If Wb.Windows(1).Visible And Wb.Path < "" Then
.Attachments.Add Wb.FullName
End If
Next
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Send an email attachment (pdf file) from Excel 2003 using Outlook Express [email protected] Excel Programming 6 November 16th 06 03:02 PM
Excel Spreadsheet email attachment unable to open in Outlook Expr. Joanne from Tiffin, Oh Excel Discussion (Misc queries) 1 April 23rd 05 12:21 AM
Email Excel File Automatically using Outlook 2000 Niraj Excel Programming 3 September 24th 04 11:09 AM
Send current Excel File as an attachment using Outlook 2000 ZABU Excel Programming 3 May 28th 04 04:50 AM
Email Attachment from Excel using Outlook. Pyball[_3_] Excel Programming 2 December 17th 03 07:08 PM


All times are GMT +1. The time now is 11:52 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"