Hi All –
In a nutshell, I’m trying to open a file, run an Auto Open macro (shown below) that imports data, saves the file with a different name, and emails that file to several people, then closes Excel…All as a scheduled task.
Here’s what I’ve accomplished -
- I have a scheduled task that opens a file that has the Auto Open macro.
- The Auto Open macro opens and imports data from a second file and manipulates it properly.
- The Auto Open macro saves the file with a new name, closes the file, and sends an email via Gmail.
What I can’t accomplish is to have the newly created file attached to the email. Is it possible to do it through Gmail? Unfortunately, I can't do this through Outlook and our Exchange Server.
I’ve downloaded the sample and info from http://www.rondebruin.nl/cdo.htm, but still can’t get it to do what I want (you'll see most of the macro is based on his work).
When I try to add what I think is the part Ron Derubin’s code that attaches the file, I get an error.
If attaching a file isn’t possible, I would settle for a hyperlink.
Thanks in advance. And by the way, the macro is 99% other peoples creation. The 1% that doesn’t work is mine…
Sub Autpen()
'
' Autpen Macro
' Macro recorded 7/20/2011
'
'
Range("B3").Select
Workbooks.Open Filename:= _
"C:\Documents and Settings\me\My Documents\testopen.xls"
Range("B4:D20").Select
Selection.Copy
Windows("Daily.xls").Activate
Range("B4").Select
ActiveSheet.Paste
Range("E8").Select
Windows("testopen.xls").Activate
Range("K9").Select
ActiveWindow.Close
Range("E4").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-2]:RC[-1])"
Range("E4").Select
Selection.Copy
Range("E5:E20").Select
ActiveSheet.Paste
Range("G21").Select
ActiveWindow.SmallScroll Down:=-6
ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\me\My Documents\test" & _
Format(Now(), "mm_dd_yyyy hh mm AMPM"), FileFormat:=xlNormal, Password:="", _
WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
'If you have a GMail account then you can try this example to use the GMail smtp server
'The example will send a small text message
'You must change four code lines before you can test the code
'.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
'.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"
'Use your own mail address to test the code in this line
'.To = "Mail address receiver"
'Change YourName to the From name you want to use
'.From = """YourName"" <Reply@something.nl>"
'If you get this error : The transport failed to connect to the server
'then try to change the SMTP port from 25 to 465
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "mygmailaddressishere@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypwishere"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Update
End With
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
With iMsg
Set .Configuration = iConf
.To = "me@mywork.com"
.CC = ""
.BCC = ""
' Note: The reply address is not working if you use this Gmail example
' It will use your Gmail address automatic. But you can add this line
' to change the reply address .ReplyTo = "Reply@something.nl"
.From = """Robert"" <me@gmail.com>"
.Subject = "Important message"
.TextBody = strbody
.Send
End With
Application.Quit
ThisWorkbook.Close SaveChanges:=True
End Sub
In a nutshell, I’m trying to open a file, run an Auto Open macro (shown below) that imports data, saves the file with a different name, and emails that file to several people, then closes Excel…All as a scheduled task.
Here’s what I’ve accomplished -
- I have a scheduled task that opens a file that has the Auto Open macro.
- The Auto Open macro opens and imports data from a second file and manipulates it properly.
- The Auto Open macro saves the file with a new name, closes the file, and sends an email via Gmail.
What I can’t accomplish is to have the newly created file attached to the email. Is it possible to do it through Gmail? Unfortunately, I can't do this through Outlook and our Exchange Server.
I’ve downloaded the sample and info from http://www.rondebruin.nl/cdo.htm, but still can’t get it to do what I want (you'll see most of the macro is based on his work).
When I try to add what I think is the part Ron Derubin’s code that attaches the file, I get an error.
If attaching a file isn’t possible, I would settle for a hyperlink.
Thanks in advance. And by the way, the macro is 99% other peoples creation. The 1% that doesn’t work is mine…
Sub Autpen()
'
' Autpen Macro
' Macro recorded 7/20/2011
'
'
Range("B3").Select
Workbooks.Open Filename:= _
"C:\Documents and Settings\me\My Documents\testopen.xls"
Range("B4:D20").Select
Selection.Copy
Windows("Daily.xls").Activate
Range("B4").Select
ActiveSheet.Paste
Range("E8").Select
Windows("testopen.xls").Activate
Range("K9").Select
ActiveWindow.Close
Range("E4").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-2]:RC[-1])"
Range("E4").Select
Selection.Copy
Range("E5:E20").Select
ActiveSheet.Paste
Range("G21").Select
ActiveWindow.SmallScroll Down:=-6
ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\me\My Documents\test" & _
Format(Now(), "mm_dd_yyyy hh mm AMPM"), FileFormat:=xlNormal, Password:="", _
WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
'If you have a GMail account then you can try this example to use the GMail smtp server
'The example will send a small text message
'You must change four code lines before you can test the code
'.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
'.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"
'Use your own mail address to test the code in this line
'.To = "Mail address receiver"
'Change YourName to the From name you want to use
'.From = """YourName"" <Reply@something.nl>"
'If you get this error : The transport failed to connect to the server
'then try to change the SMTP port from 25 to 465
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "mygmailaddressishere@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypwishere"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Update
End With
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
With iMsg
Set .Configuration = iConf
.To = "me@mywork.com"
.CC = ""
.BCC = ""
' Note: The reply address is not working if you use this Gmail example
' It will use your Gmail address automatic. But you can add this line
' to change the reply address .ReplyTo = "Reply@something.nl"
.From = """Robert"" <me@gmail.com>"
.Subject = "Important message"
.TextBody = strbody
.Send
End With
Application.Quit
ThisWorkbook.Close SaveChanges:=True
End Sub