Modify VBA code for automatic emails

Jhaynes1011

New Member
Joined
Jan 22, 2018
Messages
2
Hi, I have this code below that would work perfect for me if it would attached the existing sheet as a PDF.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRgPre As Range
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Range("D7")
Set xRgPre = xRg.Precedents
If xRg.Value > 200 Then
If Target.Address = xRg.Address Then
Call Mail_small_Text_Outlook
ElseIf (Not xRgPre Is Nothing) And (Intersect(Target, xRgPre).Address = Target.Address) Then
Call Mail_small_Text_Outlook
End If
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
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Im guessing at where you want the file made.

Code:
Option Explicit


'------------
Private Sub Worksheet_Change(ByVal Target As Range)
'------------
Dim xRgPre As Range
Dim vFile, vPtr


On Error Resume Next


If Target.Cells.Count > 1 Then Exit Sub


Set xRg = Range("D7")
Set xRgPre = xRg.Precedents


  'save the pdf
vFile = "c:\temp\MyFile.pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFile, Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True




If xRg.Value > 200 Then
    If Target.Address = xRg.Address Then
        Mail_small_Text_Outlook vFile
    ElseIf (Not xRgPre Is Nothing) And (Intersect(Target, xRgPre).Address = Target.Address) Then
        Mail_small_Text_Outlook vFile
    End If
End If
End Sub


'------------
Sub Mail_small_Text_Outlook(Optional pvFile)
'------------
Dim xOutApp As Outlook.Application
Dim xOutMail As Outlook.MailItem
Dim xMailBody As String


Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)


On Error Resume Next


xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"


With xOutMail
    .To = "Email Address"
    .CC = ""
    .BCC = ""
    .Subject = "send by cell value test"
    .Body = xMailBody
    
     If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
    
    .Display     'or use .Send
End With


On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 
Upvote 0
Thank you for taking time to look at this.

I am getting a compile error, variable not defined.





Im guessing at where you want the file made.

Rich (BB code):
Option Explicit


'------------
Private Sub Worksheet_Change(ByVal Target As Range)
'------------
Dim xRgPre As Range
Dim vFile, vPtr


On Error Resume Next


If Target.Cells.Count > 1 Then Exit Sub


Set xRg = Range("D7")
Set xRgPre = xRg.Precedents


  'save the pdf
vFile = "c:\temp\MyFile.pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFile, Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True




If xRg.Value > 200 Then
    If Target.Address = xRg.Address Then
        Mail_small_Text_Outlook vFile
    ElseIf (Not xRgPre Is Nothing) And (Intersect(Target, xRgPre).Address = Target.Address) Then
        Mail_small_Text_Outlook vFile
    End If
End If
End Sub


'------------
Sub Mail_small_Text_Outlook(Optional pvFile)
'------------
Dim xOutApp As Outlook.Application
Dim xOutMail As Outlook.MailItem
Dim xMailBody As String


Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)


On Error Resume Next


xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"


With xOutMail
    .To = "Email Address"
    .CC = ""
    .BCC = ""
    .Subject = "send by cell value test"
    .Body = xMailBody
    
     If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
    
    .Display     'or use .Send
End With


On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 
Upvote 0
sorry, instead of :Worksheet_Change(ByVal Target As Range)

use
Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top