This is really only my second VBS script so be gentle... I did **** out anything that was personal or company related I am sure all those fields are correct anyways. The SMTP server is correct I double checked with the provider as that was the number 1 reason i found on other sites. This script will also pull information from a certain cell and paste it into the body... Any help would be greatly appreciated! Also its saying the error is on line 46 which is the "ObjSendMail.Send". Everything works except the emailing portion...
Dim ObjSendMail
Set ObjSendMail = CreateObject("CDO.Message")
Set objExcel = CreateObject("Excel.Application")
StopDate = DateAdd("d", -1 - Weekday(Date), Date)
StartDate = StopDate-13
Dim xlApp
Dim xlWkb
Dim monthEnd
Set xlApp = CreateObject("excel.application")
Set xlWkb = xlApp.Workbooks.Open("******")
xlWkb.RunAutoMacros 1
xlApp.Run ("UpdateAll")
monthEnd = xlApp.cells(2,7).value
xlApp.ActiveWorkbook.SaveAs strSaveFile & "Monthly Revenue Report " & Year(Now) & "." & Month(Now) & "." & Day(Now) & ".xls", 56
xlApp.Quit
Set xlWkb = Nothing
Set xlApp = Nothing
WScript.Sleep 10000
mailSubject = "Monhtly Revenue Report " & PrevMonthName
mailBody = "The Monthly Revenue Report is no ready. Month End: " & monthEnd
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 240
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "********"
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "********"
ObjSendMail.Configuration.Fields.Update
ObjSendMail.To = "*********"
ObjSendMail.Subject = mailSubject
ObjSendMail.From = "*******"
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = mailBody
ObjSendMail.Send
'Set ObjSendMail = Nothing
When in doubt, read the documentation. Office365 uses the submission port (587/tcp) for mail submission. Replace this:
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
with this:
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
and the error should go away (provided that outbound connections to port 587/tcp are allowed on your network).
You can test accessibility of the port with a port scanner like nmap, scanline, or PortQry), or manually with telnet:
telnet smtp.office365.com 587
Outbound connections to port 25/tcp are most likely blocked by your provider, as a measure to prevent/reduce botnet spam.
The following code worked for smtp.office365.com. You DO indicate smtpusessl=true, but you do NOT specify the port, otherwise you get error 5.7.57.
Sub SMPTTest2()
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "name#myaddress.com"
emailObj.To = "name#youraddress.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
'emailObj.AddAttachment "c:\windows\win.ini"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
'Exclude the following line
'emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "name#myaddress.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
emailConfig.Fields.Update
emailObj.Send
If Err.Number = 0 Then MsgBox "Done"
End Sub
From Unknown email code from CDO.Message send method
CDO_E_FAILED_TO_CONNECT 0x80040213L The transport failed to connect to the server.
If using SSL the port is normally 465.
Related
I need to periodically send e-mails and I used the following code for years:
Function SSLSendMail(sTO As String, sFROM As String, sSubject As String, sText As String, sServer As String, _
sUser As String, sPassword As String, Optional sAttach As String, Optional sCC As String, Optional sBCC As String) As Boolean
Dim ObjSendMail As Object
Set ObjSendMail = CreateObject("CDO.Message")
Dim iConf As Object
Set iConf = CreateObject("CDO.Configuration")
With iConf.Fields 'Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort ' 2
.Item(cdoSMTPAuthenticate) = cdoBasic ' 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True ' If use SSL set to True, if not, set to False
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item(cdoSendUserName) = sUser
.Item(cdoSendPassword) = sPassword
.Item(cdoSMTPServer) = sServer
.Item(cdoSMTPConnectionTimeout) = 10
.Update
End With
Set ObjSendMail.Configuration = iConf
With ObjSendMail
.To = sTO
.Subject = sSubject
.from = sFROM
.TextBody = sText
If sAttach > "" Then
.AddAttachment sAttach
End If
If sCC > "" Then
.cc = sCC
End If
If sBCC > "" Then
.BCC = sBCC
End If
On Local Error GoTo invalidTo
.Send
On Local Error GoTo 0
End With
Set ObjSendMail = Nothing
Set iConf = Nothing
SSLSendMail = True
Exit Function
invalidTo:
Debug.Print "Error on " & sTO & ": " & Err.description
Err.Clear
SSLSendMail = False
End Function
In the last days, I get error 0x80040217 and the mail is not sent. After some investigation, I found that Google changed the authentication method from Basic to OAuth2 or, at least, that is what I have to change in my Thunderbird client in order to be able to send mails.
I have seen an answer to a similar question suggesting to use Outlook object, but that is not applicable for a series of reasons: I don't have any outlook user configured (and I don't want to!), the "sender" seems not dynamically specified, etc.
I guess I have to change .Item(cdoSMTPAuthenticate) = cdoBasic to something else, but I cannot find anything related to OAuth2.
After days and days of unsuccessfully testing changes in that code, I finally realized the problem was in the SMTP server only. I had to access my Google configuration page, ask for a second level security for the specific machine on which the Excel VBA is running and I got an encrypted password, based on my regular password, I guess. Using that password, the above code works fine, as before. Thanks Google for making our life more difficult!
I have a project that i want to ditribute.
I would like it to autosend me a mail on error.
I tried here with CDO library, but i am not sure it is the best to use.
I have :
Private Sub MyErrorHandler()
'Here i save the file to send it next
Dim path As String: path = Environ("AppData") + "\Local"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs path & "AI_ErroringFile.xls"
Application.DisplayAlerts = True
'Here i create my email
Dim Cdo_Message As New CDO.Message
Set Cdo_Message.Configuration = GetSMTPGmailServerConfig()
With Cdo_Message
.To = "myadress#yahoo.fr"
.From = Environ("username") + "#ErrorHandler.com"
.Subject = "Test"
.TextBody = "Hello"
.send
End With
Set Cdo_Message = Nothing
End Sub
and my gmail config function :
Function GetSMTPGmailServerConfig() As Object
Dim Cdo_Config As New CDO.Configuration
Dim Cdo_Fields As Object
Set Cdo_Fields = Cdo_Config.Fields
With Cdo_Fields
.item(cdoSendUsingMethod) = cdoSendUsingPort
.item(cdoSMTPServer) = "smtp.gmail.com"
.item(cdoSMTPServerPort) = 465
.item(cdoSendUserName) = "AIErrorHandler#gmail.com"
.item(cdoSendPassword) = "mypassword"
.item(cdoSMTPAuthenticate) = cdoBasic
.item(cdoSMTPUseSSL) = True
.Update
End With
But on the .send instruction i have this error :
sendusing configuration value is invalid
I have try the 3 possibles sendusing configurations without success.
I am looking for a way to fix this or to send an email with vba without using outlook
Right now i am working on a new project where i have to automatize a dashboard.
This dashboard is getting information from avaya scripts.
I searched for the last 2 weeks a script in VBA that actually gets info from avaya reports and imports them to excel file in a certain sheet.
Well, my problem is that i have to export 1 report with 6 different timezones.
For example:
Historical\Designer\Multi Date Multi Split Skill interval - Europe/Brussels timezone
Historical\Designer\Multi Date Multi Split Skill interval - US/Eastern
etc.
Below is my VBA code that works with my cms but it does not take in account that my timezone is set as Europe/Brussels and it exports in default timezone.
Please, help me out so i can ease my work with few hours a week :)
Thank you guys
Sub EMEA()
Dim cvsApp As Object
Dim cvsConn As Object
Dim cvsSrv As Object
Dim Rep As Object
Dim Info As Object, Log As Object, b As Object
Dim CMSRunning As String
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
CMSRunning = "acsSRV.exe"
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to CIMV2 Namespace
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & CMSRunning & "'") 'determine if CMS is running
If objList.Count = 0 Then 'If 0 then process isn't running
End If
Set cvsApp = CreateObject("ACSUP.cvsApplication")
Set cvsConn = CreateObject("ACSCN.cvsConnection")
Set cvsSrv = CreateObject("ACSUPSRV.cvsServer")
Set Rep = CreateObject("ACSREP.cvsReport")
Application.ScreenUpdating = 0
Set cvsSrv = cvsApp.Servers(1)
Application.ScreenUpdating = 1
AgGrp = InputBox("Enter Agent Group Name", "Agent Group", "952;953;271;270;221;222;223;224;231;233;232;234;235;246;241;243;242;247;249;245;244;248;255;258;256;259;257;261;262;260") 'change as needed for variables to enter into report
RpDate = InputBox("Enter Date", "Date", "-1") 'change as needed for variables to enter into report
'Start code from CMS Export script
On Error Resume Next
cvsSrv.Reports.ACD = 1
Set Info = cvsSrv.Reports.Reports("Historical\Designer\Multi Date Multi Split Skill interval")
b = cvsSrv.Reports.CreateReport(Info, Rep)
If b Then
Rep.Window.Top = 1830
Rep.Window.Left = 975
Rep.Window.Width = 17610
Rep.Window.Height = 11910
Rep.SetProperty "Splits/Skills", AgGrp 'change as needed for report variables
Rep.SetProperty "Dates", RpDate 'change as needed for report variables
Rep.SetProperty "Times", "00:00-23:30"
Rep.TimeZone = "Europe/Brussels"
b = Rep.ExportData("", 9, 0, True, True, True)
Rep.Quit
If Not cvsSrv.Interactive Then cvsSrv.ActiveTasks.Remove Rep.TaskID
Set Rep = Nothing
End If
Set Info = Nothing
' End code from CMS Export Script
cvsConn.logout
cvsConn.Disconnect
cvsSrv.Connected = False
Set Log = Nothing
Set Rep = Nothing
Set cvsSrv = Nothing
Set cvsConn = Nothing
Set cvsApp = Nothing
Set Info = Nothing
Range("A1").Select
ActiveSheet.Paste
End Sub
I'm trying to create a VBA script which uses the Google server but company domain. A typical example would be firstname#company.com.
I'm using the code below but it gives me an error if I use a domain name that's not gmail. I want to ask the user to enter their login information (username and password).
Sub SendGMail()
' Object creation
Set objMsg = CreateObject("CDO.Message")
Set msgConf = CreateObject("CDO.Configuration")
' Server Configuration
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = UserForm1.TextBox1.Value
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = UserForm1.TextBox2.Value
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
msgConf.Fields.Update
' Email
objMsg.To = UserForm1.TextBox1.Value
objMsg.From = UserForm1.TextBox1.Value
objMsg.Subject = "Test send with Gmail account"
objMsg.HTMLBody = "HTML/Plain text message."
objMsg.Sender = "Mr. Name"
Set objMsg.Configuration = msgConf
' Send
objMsg.Send
' Clear
Set objMsg = Nothing
Set msgConf = Nothing
End Sub
You need to add Microsoft CDO Reference before running the code
Tool-->Reference---> Microsoft CDO for Windows 2000 Library
Sub MailTest()
Dim Mail As New Message
Dim Config As Configuration
Set Config = Mail.Configuration
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 25
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = InputBox("EnterUsername")
' Otherwise if you use text box
'config(cdoSendUserName) = UserForm1.TextBox1.Text
Config(cdoSendPassword) = InputBox("Password")
' Otherwise if you use text box
'config(cdoSendUserName) = UserForm1.TextBox2.Text
Config.Fields.Update
Mail.To = "yahoo44#gmail.com"
Mail.From = Config(cdoSendUserName)
Mail.Subject = "Email Subject"
Mail.HTMLBody = "<b> mail body </b>"
Mail.Sender = " MRSender"
Mail.Send
MsgBox "mail has been sent"
End Sub
I have an application deployed in the development server (notes client). The application composed a function to export the data in the local machine of the user along with the graph generation. My problem is, when the data are exported, the rendering of graph is incorrect (in the server), but when I run it in my local machine the graph rendering is fine.
I used MS 2007 in my local machine but in the development server there is NO MS Office installed.Is there any way to do this? Here is the code below.
Dim s As New notessession
Dim db As notesdatabase
Set db= s.currentdatabase
Dim uiw As New NotesUIWorkspace
Dim otherdoc As NotesDocument
Dim otherview As NotesView
Dim othercol As NotesDocumentCollection
Dim ViewNav As NotesViewNavigator
Dim entry As notesViewEntry
Dim tempdoc As notesdocument
Dim uiv As notesuiview
Set uiv = uiw.currentview
Dim VName As String
VName = uiv.ViewName
'if it is R4 then viewalias doesn't work so use
'environment variable stashed in the post open event
If Instr(s.Notesversion, "Release 4") Then
currentviewname = s.getenvironmentstring("Tracking")
If currentviewname="" Then
Msgbox "View not exist."
End
End If
Call s.setenvironmentvar("Tracking","")
Elseif uiv.viewalias <> "" Then 'use alias if it isn't blank
currentviewname = uiv.viewalias
Else ' use name
currentviewname = uiv.viewname
End If
'Get the view
Set otherview = db.GetView(currentviewname)
If otherview Is Nothing Then
Messagebox "Could not open the view. """ & currentviewname & """"
Exit Sub
End If
'Check if it is for all documents or only selected
Set othercol = db.unprocesseddocuments
If othercol.count >1 Then 'if more than one doc selected then confirm
resp = Messagebox("Do you want to export only the " & _
"selected " & othercol.count & " documents?", 36, "Selected only?" )
If resp=6 Then
Else
Exit Sub
End If
Else
Messagebox "Exporting all rows. (To export only selected " & _
"rows tick those required in the left margin first.)"
End If '6= yes
Dim object As NotesEmbeddedObject
Dim xlApp As Variant
Dim oWorkbook As Variant
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'set to visible, this can be moved to the end if you wish
Set oworkbook = xlApp.Workbooks 'handle to Workbook
oworkbook.Add
'Stick out the column headers
hcolmn=1
Forall c In otherview.Columns
xlApp.cells(1,hcolmn) = c.title
hcolmn=hcolmn+1
End Forall
row=2
Dim vc As NotesViewEntryCollection
Dim seldoc As notesdocument
Dim view As notesview
Set view = db.GetView(VName)
Set vc = view.AllEntries
If resp=6 Then
Set seldoc = othercol.GetFirstDocument
While Not seldoc Is Nothing
Set entry = vc.GetEntry(selDoc)
Print "Entry " entry.noteID " from document " selDoc.noteID '<--- new line
'Msgbox row
For colmn = 0 To Ubound(entry.ColumnValues)
col% = col% + 1
xlApp.cells(row,colmn+1) = entry.columnvalues(colmn)
Next
row=row+1
Set seldoc = othercol.GetNextDocument(seldoc)
Wend
Else ' all documents
Set otherdoc = otherview.GetFirstDocument
While Not otherdoc Is Nothing
For colmn = 0 To Ubound(otherview.Columns)
xlApp.cells(row,colmn+1) = otherdoc.columnvalues(colmn)
Next
row=row+1
Set otherdoc = otherview.GetNextDocument(otherdoc)
Wend
End If
'this highlights the headings
xlApp.application.Rows("1:1").Select
With xlApp.application.Selection.Font
.bold = True
.ColorIndex = 48
.Name = "Arial"
.Size = 10
End With
'this freezes the panes
xlApp.application.Rows("2:2").Select
xlApp.application.ActiveWindow.FreezePanes = True
xlChartType = 51
xlapp.ActiveWorkbook.Charts.Add
With xlapp.ActiveWorkbook.ActiveChart
.Name = "Chart"
.HasTitle = True
.ChartTitle.Text = "Total Submissions Received per Month"
.Axes("1").HasTitle = True 'xlCategory = x axis
.Axes("1").AxisTitle.Text = "Month"
.Axes("1").AxisTitle.AutoScaleFont = True
.Axes("1").AxisTitle.Font.Size = 8
.Axes("2").HasTitle = True 'xlValue = y axis
.Axes("2").AxisTitle.Text = "No. of Submission"
.Axes("2").AxisTitle.AutoScaleFont = True
.Axes("2").AxisTitle.Font.Size = 8
.ChartType = xlChartType
.PlotArea.Interior.ColorIndex = "0"
.PlotBy = "1" '2 = Column Plot
.SetSourceData xlApp.Worksheets("Sheet1").Range("A1","N6")
End With
The fact that you instantiate new NotesUIWorkspace means that the agent does not run on the server. So you should probably ask about the version of Excel on the computers of other users, who run the code. Note that the agent would only run on the server if it is scheduled or if you make it run on the server using notesAgent.RunOnServer( [ noteID$ ] ).
I changed this line and it works fine even there is no excel installed on the server.
change SetSourceData xlApp.Worksheets("Sheet1").Range("A1","N6") to SetSourceData xlApp.Worksheets("Sheet1").Range("A1","N" & row-1)