VBA: Sending emails via VBA.

countryfan_nt

Well-known Member
Joined
May 19, 2004
Messages
765
Hello friends, Hope you are well! always!

please help me with the below code please.

1. Please help me make the strbody show in the body of the email. new lines taking effect please.
2. I need the following line to be in both BLUE & ITALICS:
"Should you have any questions, please do not hesitate to ask."

Thank you so much in advance!


Code:
 Sub EmailPDF()
 fdir = "c:\temp\"
 fname = Sheets("Sheet1").Range("bb7").Value
 fpath = fdir & fname & ".pdf"
 With Application
.EnableEvents = True
.ScreenUpdating = False
 End With

ToAddress = Sheets("Sheet1").Range("bh4").Value
CCAddress = Sheets("Sheet1").Range("bh5").Value

MailSub = fname


'Generate PDF document to c:. Substitute ActiveSheet for ActiveWorkbook to PDF the entire document
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
fpath, Quality:=xlQualityStandard, _
IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=False

Dim strbody As String
Dim OutApp As Object
Dim OutMail As Object
 
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

'Create Mail & attach PDF
 Set oOutlookApp = CreateObject("Outlook.Application")

'Create a new message
 Set oItem = oOutlookApp.CreateItem(olMailItem)
 With oItem
 .To = ToAddress
 .CC = CCAddress
 .Subject = MailSub
 .HTMLBody = strbody

strbody = "Hi there" & vbNewLine & vbNewLine & _
              "This is line 1" & vbNewLine & _
              "This is line 2" & vbNewLine & _
              "This is line 3" & vbNewLine & _
              "Should you have any questions, please do not hesitate to ask." & vbNewLine & _
              "This is line 5"

'Bring up new mail window
 oItem.Display

'Add Attachment
oItem.Attachments.Add fpath

'Cleanup , baby
 Set OutMail = Nothing
 Set OutApp = Nothing
 Set fs = CreateObject("Scripting.FileSystemObject")

fs.deletefile fpath

End With
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hello

I think that you better go to the basics of VBA, before you do "projects" like this.
For example, you use the variable strbody before you set the contents of the variable...

Please use code like:

Code:
Sub EmailPDF()

    Dim strbody As String
    
    With Sheets("Sheet1")
        fdir = "C:\temp\"
        fname = .Range("BB7").Value & ".pdf"
        fpath = fdir & fname
        ToAddress = .Range("BH4").Value
        CCAddress = .Range("BH5").Value
    End With

    'Generate PDF document
    ActiveSheet.ExportAsFixedFormat 0, fpath

    'Create Mail & attach PDF
    With CreateObject("Outlook.Application").CreateItem(0)
        .To = ToAddress
        .CC = CCAddress
        .Subject = fname
        .HTMLBody = Replace("Hi there" & vbNewLine & vbNewLine & _
                  "This is line 1" & vbNewLine & _
                  "This is line 2" & vbNewLine & _
                  "This is line 3" & vbNewLine & _
                  "[I]Should you have any questions, please do not hesitate to ask.[/I]" & vbNewLine & _
                  "This is line 5", vbNewLine, "")
        .Attachments.Add fpath
        .Display '.Send
        
    End With

    'Cleanup
    Kill fpath
    
End Sub

Due to the board software, the line that starts with "This is line 5" needs 4 characters in between the last two "": a smaller than character, followed by a b, followed by an r, followed by a greater than character
Likewise, to format as blue and italics:

" smaller than character font color=""blue"" greater than character smaller than character i greater than character Should you have any questions, please do not hesitate to ask. smaller than character /i greater than character smaller than character /font greater than character" & vbNewLine & _

Sorry for that but it's the board.
 
Last edited:
Upvote 0
great! thanks a lot!

can you please help me out with the 2nd point?

2. I need the following line to be in both BLUE & ITALICS:
"Should you have any questions, please do not hesitate to ask."


Thanks again!
 
Upvote 0
See my post again, I made changes.
 
Upvote 0

Forum statistics

Threads
1,223,993
Messages
6,175,844
Members
452,675
Latest member
duongtruc1610

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