• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA modification

Afarag

Member
Code:
Option Explicit

Sub Mail_Selection_Range_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2013
    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 fixed range if you want
    'Set rng = Sheets("YourSheet").Range("D4:D12").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
    With OutMail
        .To = "Ahmed Mohamed Abdelkader <ahmed.mabdelkader@tedata.net>"""
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send  'or use .Display
    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-2013
    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
Dears,

from the attached sheet i have a VBA code mail the selected data from the sheet to some receiver, but i want some modification as:

1- I want open the body of mail, not be send automatically

2- I want determine the receiver user :"to" - "CC" - "Body" by cells due to be adjustable, as mentioned in the attached sheet
 

Attachments

  • All-in-ONE Chart.xlsm
    116 KB · Views: 7
@Afarag

For #1, you need to change the following piece of code:
Code:
  On Error Resume Next
    With OutMail
        .To = "Ahmed Mohamed Abdelkader <ahmed.mabdelkader@tedata.net>"""
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send  'or use .Display
    End With
    On Error GoTo 0

This is the important line:
Code:
.Send 'or use .Display
".Send" means the mail will be sent. ".Display" means the file will be displayed so that you can edit is.

(Hint - if you want to save the mail as a draft, you can use the following code instead:
Code:
  .Save
  .Close olPromtForSave
This will create the email then save it as a draft. It comes in handy if you want to generate multiple emails to edit.)

So, your code should look like this.
Code:
  On Error Resume Next
    With OutMail
        .To = "Ahmed Mohamed Abdelkader <ahmed.mabdelkader@tedata.net>"""
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Display  'or use .Save
    End With
    On Error GoTo 0

I'm not sure what you mean by #2...could you explain a little more?
 
Dear tobediff,

thanks a lot for following, the first case related of send or display has been handled,
the 2nd case: i mean to determine the user by cell founded in Mail sheet
Code:
On Error Resume Next
    With OutMail
        .To = "(B27)"
        .CC = "(B28)"
        .BCC = "(B29)"
        .Subject = "(B30)"
        .HTMLBody = RangetoHTML(rng)
        .Display  'or use .Save
  End With
    On Error GoTo 0
 
Got it.

In VBA, when we want to get the value of a cell, we use the following:
Range("A1").Value

Just replace "A1" with the cell that you want.

So, your code would look like this.
Code:
  On Error Resume Next
  With OutMail
  .To = Range("B27").Value
  .CC = Range("B28").Value
  .BCC = Range("B29").Value
  .Subject = Range("B30").Value
  .HTMLBody = RangetoHTML(rng)
  .Display  'or use .Display
  End With
  On Error GoTo 0
 
Great Sir

It's solved merci, but i have a confusion in Vlookup function clarefied in the attached sheet
 

Attachments

  • All-in-ONE Chart.xlsm
    115 KB · Views: 3
Last edited:
@SirJB7

the above VBA code for selection or range data from table, when i select my data and click send, just determine my selected data in mail body without signature and a body
you can check the attached sheet
 

Attachments

  • All-in-ONE Chart Last (2).xlsm
    130.3 KB · Views: 6
Last edited by a moderator:
Hi, Afarag!

The problem is with the rng object definition, it just points to the selected cell or area. Post a sample of the wanted body, indicating where it should extract the data from; include required signature.

Regards!
 
@SirJB7

The required body:
Kindly please you have to check your total deduction for "this month"or this can be in cell "B31"

The required signature:



Thanks and Best Regards

Ahmed Abdel Kader

Info & Sales Call Center Operations Representative

8 Gezirt Elarab st, Infinity Tower

Mohandseen, Giza 12311, Egypt

:) +20 (2) 33 32 0700

7: +20 (2) 33 32 0818

M: 0 127 3533252

*:Ahmed.mabdelkader@tedata.net

www.TEData.net
 
Back
Top