i currently have a form with a textbox and a button. you can enter any number in the textbox and when you hit the button it will show the number on a message box, but if there is an error, a message will pop up and you can click the debug button to see which line gave an error. This will be for the code:
On Error GoTo 0:
But if i replace the 0 with "error:", it will take me to a new subroutine that will send me an email regarding the issue but it will not highlight the line which gave me an error when i hit the debug button.
Is there a way i can send myself an email AND when i hit the debug button it will highlight the error line?
Private Sub CommandButton1_Click()
'on error goto error:
On Error GoTo 0:
Dim word As Double
word = TextBox1.Text
MsgBox word
Exit Sub
error:
Call error()
End Sub
Sub error()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "error"
With OutMail
.To = "blahblah#hotmail.com"
.CC = ""
.BCC = ""
.Subject = "error"
.Body = strbody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
Debug.Assert (Err.Number & Err.Description)
End Sub
The below pattern calls error() then breaks in the IDE on the line that caused the error:
Sub Foo()
On Error GoTo ERR_HANDLER
Dim i As Long
i = i / 0
Exit Sub
ERR_HANDLER:
Call error()
On Error GoTo 0
Resume
End Sub
Related
I have some vba code, which I have attached to a excel button. When pressing the button the outlook mail box will appear- ready to send the mail. But for some reason the workbook is not attached to the mail. In the code below - I think something is wrong with add.attachments. at the end
My VBA code is:
Dim xRg As Range
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("D7"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 200 Then
Call Mail_small_Text_Outlook
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"
On Error Resume Next
With xOutMail
.To = "Email Address"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Attachments.Add "W\Desktop\Files\Workbook1
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
The following line of code is not complete:
.Attachments.Add "W\Desktop\Files\Workbook1
The Add method of the Attachments class creates a new attachment in the Attachments collection. The source of the attachment can be a file (represented by the full file system path with a file name), for example:
.Attachments.Add "C:\Test.xslx", olByValue, 1, "Test"
I'd also suggest specifying a local file path.
I have created a macro that attaches selected email to the message and sends it to the pre-populated address.
However sometimes macro stops attaching selected email.
Can anyone advise what may be the reason? Here is my code.
Sub ForwardOutsource()
On Error Resume Next
Dim objItem As Outlook.MailItem
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If
For Each objItem In Application.ActiveExplorer.Selection
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.SentOnBehalfOfName = "info#info.com"
.Attachments.Add objItem, olEmbeddeditem
.Subject = objItem.Subject
.To = "address#address.com"
.Display
End With
Next
Set objItem = Nothing
Set objMsg = Nothing
End Sub
Well you should remove On Error Resume Next from your code, you are basically telling the code to continue to next line if an error occurred.
by the way you are not using it correctly as well
Here is good link http://www.cpearson.com/Excel/ErrorHandling.htm
Next try using Option Explicit and Declare variables
clean up your code example
Option Explicit
Public Sub Fw_Items_As_Atmt()
'// Declare variables
Dim msg As Outlook.MailItem
Dim Item As Outlook.MailItem
' Select msg
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No Item selected")
Exit Sub
End If
Set msg = Application.CreateItem(olMailItem)
For Each Item In Application.ActiveExplorer.Selection
With msg
.Attachments.Add Item, olEmbeddeditem ' Attch Selected email
.Subject = "See Attached Items"
.To = ""
.CC = ""
.HTMLBody = ""
.Display
' .Send
End With
Next
'// Clean up
Set Item = Nothing
Set msg = Nothing
End Sub
I'm trying to create a template where I can have VBA prompt me to change items in the subject. I've a template email with the subject line that should be Project / GC/Client, and in the template I've written that as #0# / #1#.
I'm at the below, but it's erroring.
Private Sub m_Inspector_Activate()
Dim Item As MailItem
Dim Value As String
If TypeOf m_Inspector.CurrentItem Is MailItem Then
Set mail = m_Inspector.CurrentItem
If mail.Subject = "subject" Then
Value = InputBox("Project")
mail.Subject = Replace(mail.Subject, "#0#", Value)
Value = InputBox("GC/Client")
mail.Subject = Replace(mail.Subject, "#1#", Value)
End If
End If
End Sub
This code is used on a template button in Outlook. i.e. the template file it is launching contains the subject line of #0# / #1#. Ideally, after the template launches, the macro prompts the user to update those two fields with the proper subjects.
Sub CommandButton1_Click()
Set MyItem = Application.CreateItemFromTemplate _
("V:\All Folders\Templates\Freebie.oft")
MyItem.Display
End Sub
If all that you really want to do is open a template and replace text in the subject line, this may work for you:
Sub CommandButton1_Click()
Call OpenTemplate
End Sub
Sub OpenTemplate()
Dim OutMail As Outlook.MailItem
Set OutMail = Application.CreateItemFromTemplate("V:\All Folders\Templates\Freebie.oft")
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = Replace(.Subject, "#0#", InputBox("Project"))
.Subject = Replace(.Subject, "#1#", InputBox("GC/Client"))
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
If we can avoid using the Inspector then this is much simpler
How can I define a sub or function in VBA?
This is my code:
Private Sub CommandButton1_Click()
Call Send_Mail
End Sub
In Worksheet "Sheet1" I have a CommandButton called Send_Mail and in "Sheet2" I have also a CommandButton. When I click the CommandButton in Sheet2 I want that the Button in Sheet1 will run.
With my code the : error "Sub or Function is not defined" appears.
EDIT:
Code for Send_Mail:
Public Sub Send_Mail_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim nameList As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error GoTo cleanup
For i = 4 To 22
If Range("B4").Value <> "" Then
nameList = nameList & ";" & Range("C" & i).Value
End If
Next
With OutMail
.To = nameList
.Subject = "Subject Line"
.Body = "Body Text"
.Send
End With
cleanup:
Set OutApp = Nothing
MsgBox "E-Mail sent."
MsgBox Err.Description
End Sub
I don't know what the underlying sub procedure attached to the Sheet1 button has been named but it is likely it has a similar name.
'Sheet2's button sub procedure
Private Sub CommandButton1_Click()
Call Sheet1.CommandButton1_Click
End Sub
If there is no attachment file is present at path mail should not send.
Is there any possibility that mail should not be send if there is no attachment?
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = " Hello"
On Error Resume Next
With OutMail
.to = "test#gmail.com"
.cc = "test1#gmail.com"
.BCC = ""
.Subject = "TRANSACTING : " & Format(Date, "DD-MMM-YYYY")
.HTMLBody = strbody
'You can add an attachment like this
.Attachments.Add ("E:\Auto Reports\test.xlsb")
.send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
First of all using On Error Resume Next in the way you did just mute any errors. But the errors still occur they just don't show. Using On Error Resume Next is a very bad practice and you should avoid that unless you really know why you need to do it.
Instead you should always implement a proper error handling like below.
Now the error is shown if adding an attachment to the email fails and the email is not sent.
Option Explicit
Public Sub SendMyEMail()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = " Hello"
On Error GoTo MAIL_ERROR 'jump to the error handler if an error occurs
With OutMail
.to = "test#gmail.com"
.cc = "test1#gmail.com"
.BCC = ""
.Subject = "TRANSACTING : " & Format(Date, "DD-MMM-YYYY")
.HTMLBody = strbody
'You can add an attachment like this
.Attachments.Add ("C:\Auto Reports\test.xlsb")
.send 'or use .Display
End With
On Error GoTo 0 'stop error handling here (no jumps to the error handler anymore.
'The following 2 lines can be omitted because it is done automatically on exit sub
'So these are completely unnecessary.
'Set OutMail = Nothing
'Set OutApp = Nothing
Exit Sub 'we need this to not to run into error handler if everything was ok
MAIL_ERROR: 'Show error message
MsgBox "An error occured during sending the email. The email was not sent: " & vbNewLine & Err.Description, vbCritical, "Error " & Err.Number, Err.HelpFile, Err.HelpContext
End Sub
Send multiple different e-mails
To send multiple different e-mails you need to make your procedure a more universal function receiving some parameters.
Public Function SendMyEMail(MailTo As String, MailCC As String, MailSubject As String, MailFileName As String)
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = " Hello"
On Error GoTo MAIL_ERROR 'jump to the error handler if an error occurs
With OutMail
.To = MailTo
.CC = MailCC
.BCC = ""
.Subject = MailSubject
.HTMLBody = strbody
'You can add an attachment like this
.Attachments.Add MailFileName
.send 'or use .Display
End With
On Error GoTo 0 'stop error handling here (no jumps to the error handler anymore.
'The following 2 lines can be omitted because it is done automatically on exit sub
'So these are completely unnecessary.
'Set OutMail = Nothing
'Set OutApp = Nothing
Exit Function 'we need this to not to run into error handler if everything was ok
MAIL_ERROR: 'Show error message
MsgBox "An error occured during sending the email. The email was not sent: " & vbNewLine & Err.Description, vbCritical, "Error " & Err.Number, Err.HelpFile, Err.HelpContext
End Function
And a procedure SendMultipleEmails looping through a worksheet and run SendMyEMail for every row.
Public Sub SendMultipleEmails()
Dim wsMail As Worksheet
Set wsMail = Worksheets("MyMailSheet")
Dim iRow As Long, lRow As Long
lRow = wsMail.Cells(wsMail.Rows.Count, "A").End(xlUp).Row 'find last used row in column A
For iRow = 1 To lRow 'run from first to last used row
SendMyEMail wsMail.Cells(i, "A"), wsMail.Cells(i, "B"), wsMail.Cells(i, "C"), wsMail.Cells(i, "D")
'SendMyEMail for every row in that sheet where MailTo is in column A, MailCC is in column B, …
Next iRow
End Sub
You can check if the file exists before attempting to draft the email.
Sub SendEmail()
'Exit if file does not exist
If Len(Dir("E:\Auto Reports\test.xlsb", vbDirectory)) = 0 Then Exit Sub
'Proceed
Dim OutApp As Object
'rest of code
End Sub