Consulting

Results 1 to 6 of 6

Thread: VBA code to email different attachment to different person using outlook

  1. #1

    VBA code to email different attachment to different person using outlook

    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 xublishsource=", _
    "align=left xublishsource=")

    '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

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location

  3. #3
    I have posted it on two forums so that i would get the solution faster

  4. #4
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Quote Originally Posted by rav4u_tom
    I have posted it on two forums so that i would get the solution faster
    Mark's point is, in his, "Please read here" link (which you apparently didn't read!), that it is considered rude to do so IF you don't include the link to the cross post. Even is you have to put a abbreviated link (as new users can't post links to help deter spam. Or even the board name and message title.)

  5. #5

    Extremely sorry

    Hello All,

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

    thanks a lot Mark and Tinbendr.

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Not a problem. I certainly made a goof or two when I first joined here.

    Welcome to the forum :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •