VBA code to email different attachment to different person using outlook

rav4u_tom

New Member
Joined
Sep 27, 2010
Messages
4
Hello All,
I am writing a code to automate a work process in my company. In this process i need to mail a respective file to a manager who's looking after a city and after that i have to main another manager a different file corresponding to his region. So this process goes on. We need to mail a manager his corresponding file. All the files to be sent would be in a specific folder(same folder). i have attached the code which i have assemebled from different websie and customized it to suit my requirements. please go through the code and help me in writing VBA code for sending different files to different persons from same folder.
Sub Mail_Selection_Range_Outlook_Body()
' Working in Office 2000-2010
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
'Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a range if you want
Set rng = Sheets("sheet1").Range("B12:F31").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
surrfile = Sheets("sheet1").Cells(7, 8)
With OutMail
.To = Sheets("sheet1").Cells(5, 8)
.CC = Sheets("sheet1").Cells(6, 8)
.BCC = Sheets("sheet1").Cells(7, 8)
.Subject = Sheets("sheet1").Cells(4, 8)
.HTMLBody = RangetoHTML(rng)

'.Attachments.Add "D:\text.txt", olByValue, , _
"Attachment" ' insert attachment

' here i need to attach a file based on the person i am sending the file. For example, if am sending a mail to AGRA city manager, i need to attached the AGRA file from the folder automatically'

.Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
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"

'Copy the range and create a new workbook to past the data in
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

'Publish the sheet to a htm file
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

'Read all data from the htm file into RangetoHTML
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=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

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

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Thanks a lot Andrew. I am Attaching the snap shot of what I am working on.
C:\Documents and Settings\ravinders\Desktop\FIle.JPG
 
Upvote 0
Thanks a lot Mark.

I am extremely sorry for what i have done. As i am new to forums, i am unaware of the rules and regulations.

I sincerely aplologise to all.
 
Upvote 0
Not a problem, I certainly made goofs when joining my first forum:rolleyes:
 
Upvote 0

Forum statistics

Threads
1,217,040
Messages
6,134,146
Members
449,861
Latest member
DMJHohl

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