• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Modified code to Send mail with two attachment.|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Modified code to Send mail with two attachment.|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosModified code to Send mail with two…
sp_PrintTopic sp_TopicIcon
Modified code to Send mail with two attachment.
Avatar
Yousef Qaroos
Member
Members
Level 0
Forum Posts: 12
Member Since:
April 27, 2020
sp_UserOfflineSmall Offline
1
August 21, 2020 - 5:43 pm
sp_Permalink sp_Print

Good day

 

As you know there is a VBA code that send an excel sheet as pdf on email attachment,

 

Could this code modified to let it attach the active sheet in two different format (.xlsx, .pdf) also let it put on the email body this range Sheets(“index”).Range(“J743:S760”) 

 

finally , if it possible to do if statement that looking if Sheets(“index”).Range(“J730").value = "Timer" 

Application.OnTime TimeValue ("04:30:00") , Procedure:= "MYcode"

 

else 

 

send the mail directly 

sp_AnswersTopicSeeAnswer See Answer
Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 412
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
2
August 22, 2020 - 1:41 am
sp_Permalink sp_Print sp_EditHistory

Do you have an example of the code you are using? much easier to view and amend an example workbook

When it comes to sending mails via VBA this site is amazing

https://www.rondebruin.nl/win/.....k/tips.htm towards the bottom is where you can delay sending a mail

Avatar
Yousef Qaroos
Member
Members
Level 0
Forum Posts: 12
Member Since:
April 27, 2020
sp_UserOfflineSmall Offline
3
August 22, 2020 - 3:50 pm
sp_Permalink sp_Print

this is the cod that I try to modify but not successes

 

Sub AZ()

Dim rng As Range
Dim olApp As Object
Dim NewMail As Object
Dim TempFilePath As String
Dim FileExt As String
Dim TempFileName As String
Dim FileFullPath As String
Dim FileFormat As Variant
Dim FolderName As String
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Dim StrBody As String
Set rng = Nothing
On Error Resume Next
Set Wb1 = ThisWorkbook
ActiveSheet.Copy
Set Wb2 = ActiveWorkbook
With Application
.ScreenUpdating = False
.EnableEvents = False
End With

With Wb2
If Val(Application.Version) < 12 Then
FileExt = ".xls": FileFormat = -4143
Else
Select Case Wb1.FileFormat
Case 51: FileExt = ".xlsx": FileFormat = 51
Case 52:
If .HasVBProject Then
FileExt = ".xlsx": FileFormat = 51
Else
FileExt = ".xlsx": FileFormat = 51
End If
Case 56: FileExt = ".xls": FileFormat = 56
Case Else: FileExt = ".xlsx": FileFormat = 51

End Select
End If
End With

 

TempFilePath = Environ$("temp") & "\"

TempXLFileName = [SubMG]

TempPDFFileName = [TitMG] & ".pdf"

xlFileFullPath = TempFilePath & TempXLFileName & FileExt

pdfFileFullPath = TempFilePath & TempPDFFileName

Wb2.SaveAs xlFileFullPath, FileFormat:=FileFormat

With ActiveSheet
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=pdfFileFullPath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With

Set rng = Sheets("index").Range("J743:S760").SpecialCells(xlCellTypeVisible)

On Error GoTo 0

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set olApp = CreateObject("Outlook.Application")
Set NewMail = olApp.CreateItem(0)

On Error Resume Next
With NewMail
.Subject = [SubMG]
.To = [toMG]
If [CCMG] <> "" Then .CC = [CCMG]
.HTMLBody = RangetoHTML(rng)
.Body = "This E-mail contains the morning report and has been created and sent automatically by the NAPEOFFWSD's Morning report software V 1.72" & vbLf & vbLf _
& "Regards," & vbLf _
& Application.UserName & vbLf & vbLf
.Attachments.Add xlFileFullPath
.Attachments.Add pdfFileFullPath
.send
Application.Visible = True
If Err Then
MsgBox "E-mail not sent", vbExclamation
Else
MsgBox "E-mail successfully Created, You may display your Morning report from your Outlook ... ", vbInformation
End If
End With
On Error GoTo 0

Wb2.Close savechanges:=False
Kill xlFileFullPath
Kill pdfFileFullPath

Set NewMail = Nothing
Set olApp = Nothing

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

Function RangetoHTML(rng As Range)

Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With

With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")

TempWB.Close savechanges:=False

Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

Avatar
Yousef Qaroos
Member
Members
Level 0
Forum Posts: 12
Member Since:
April 27, 2020
sp_UserOfflineSmall Offline
4
August 22, 2020 - 9:20 pm
sp_Permalink sp_Print

also you may find here the workbook that I try to modify the code on it to test the result before I implement it in original file. 

 

the result that I expect the code will do it.

 

1-if Sheets(“index”).Range(“J730").value = "Timer" 

Application.OnTime TimeValue ("Sheets(“index”).Range(“L730").value") , Procedure:= "MYcode"

elese "MYcode"

2-Creat an email

3- attach a copy of Sheets("DAILY OPS REPORT8") in two different format (.xlsx, .pdf)

4- let the mail body .HTMLBody=Sheets("index").Range("J743:S760").SpecialCells(xlCellTypeVisible)

 

*step number 1 that to let the code start direct to create the mail or wait the timer to create the mail

Avatar
Yousef Qaroos
Member
Members
Level 0
Forum Posts: 12
Member Since:
April 27, 2020
sp_UserOfflineSmall Offline
5
August 28, 2020 - 1:16 am
sp_Permalink sp_Print

any update in previous issue ?

even if the same result come from another Code  

Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 412
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
6
August 28, 2020 - 3:59 am
sp_Permalink sp_Print

i have had a quick look

You seem to be doing quite a bit in the Macro and i think yoiu need to try and understand what is happening - do you know how to step through code and check varibles?

I couldnt get the ENVIRON (temp) folder to save so i would always start with a folder i can see and use and know i can save to, so i have changed TempFilePath to TempFilePath = "F:\Moth" & "\".

Then [SubMG] wasnt picking anything up - this could be because form was not filled in but i made it in to a string to test and then both the XLSX and PDF saved in to my folder above

i think the range part doesnt seem to work as you are setting the rng to sheets("index") but there isnt a worksheet called index in that workbook.

Avatar
Yousef Qaroos
Member
Members
Level 0
Forum Posts: 12
Member Since:
April 27, 2020
sp_UserOfflineSmall Offline
7
August 28, 2020 - 6:48 pm
sp_Permalink sp_Print

Good day purfleet

 

as you say I am not that good on coding but I try to learn and discuss with others.

 

you could find [SubMG] in DAILY OPS REPORT8!C362 and it should =index!EG147

 

regarding ("index") worksheet, its on the file before the macro create copy of ("DAILY OPS REPORT8") worksheet

 

index.pngImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage index.png (13 KB)
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
8
September 8, 2020 - 2:36 pm
sp_Permalink sp_Print

Hi Yousef,

I've used the code I wrote in this post

https://www.myonlinetraininghu.....th-outlook

and modified it slightly to do what you want.  I also used the RangetoHTML function from Ron de Bruin found here

https://www.rondebruin.nl/win/.....bmail2.htm

Regards

Phil

sp_AnswersTopicAnswer
Answers Post
sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Andy Kirby, Tiffany Kang
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 204
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Maria Conatser
Jefferson Granemann
Glen Coulthard
Nikki Fox
Rachele Dickie
Raj Mattoo
Mark Luke
terimeri dooriyan
Jack Aston
AndyC
Forum Stats:
Groups: 3
Forums: 24
Topics: 6221
Posts: 27285

 

Member Stats:
Guest Posters: 49
Members: 31908
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.