19 February 2016

Copy Cells From 2 Sheets and Paste to Outlook as Image


Hello,

Ini VBA yang aku edit dan tambah untuk snap cells untuk 2 sheets dan pasti ke dalam Outlook mail.
Disertakan juga sheet untuk To list dan Cc list.


==========================================================================

Sub OpenOutlook()


Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim strUser As String
Dim signature As String
Dim sTo As String
Dim sCC As String
Dim rng As Range

Set rng = Nothing
Set rng = ActiveSheet.UsedRange

'check outlook is open
Dim olApp As Object

CreateObject("shell.application").minimizeall

On Error Resume Next

Set olApp = GetObject(, "Outlook.Application")

On Error GoTo 0

If Not olApp Is Nothing Then
  'Outlook is open!
  'MsgBox "Outlook is running"
 
Else
  'Outlook not open
 
  'MsgBox "Outlook is not running"
  Shell ("OUTLOOK")
 
 
End If
'end check outlook is open
''''''


    'For To field
    Set emailRng = Worksheets("Email Contact").Range("A2:A30")

    For Each cl In emailRng
        sTo = sTo & ";" & cl.Value
    Next

    sTo = Mid(sTo, 2)

    'For CC field
    Set emailRngCC = Worksheets("Email Contact").Range("B2:B10")

    For Each cl In emailRngCC
        sCC = sCC & ";" & cl.Value
    Next

    sCC = Mid(sCC, 2)


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
    With OutMail
    .Display
    End With
        signature = OutMail.HTMLBody


    'strbody = "Good Morning;We have completed our main aliasing process for today.  All assigned firms are complete.  Please feel free to respond with any questions.Thank you."
   
    strbody = "Hi all,Below are today's jobs in the lab."
   
    'strbody = "This is line 1" & "
" & _
    '          "This is line 2" & "
" & _
     '         "This is line 3" & "


"
             
             
    With OutMail
        .SentOnBehalfOfName = ""
        .To = sTo
        .CC = sCC
        .BCC = ""
        .Subject = "Jobs in Lab on  " & Format$(Date, "dd-mm-yyyy")
        .HTMLBody = strbody & RangetoHTMLPassdown(rng) & RangetoHTMLReport(rng) & signature
       
        .Display
        '.Send
    End With
   
   
'Sub GCloseJobsinLab()

  'Close opened jobs in lab from iFAct.
  Windows("Jobs in Lab").Close False
  Windows("Jobs_in_lab_Macro.xlsm").Close False
   
   
End Sub


Function RangetoHTMLReport(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
   
Sheets("Report").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.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)
    RangetoHTMLReport = ts.readall
    ts.Close
    RangetoHTMLReport = Replace(RangetoHTMLReport, "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


Function RangetoHTMLPassdown(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
   
Sheets("Passdown Format").Select
'Range("A1").Select
'Range(Selection, Selection.End(xlToRight)).Select
'Range(Selection, Selection.End(xlDown)).Select
Range("A1:L27").Select
Selection.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)
    RangetoHTMLPassdown = ts.readall
    ts.Close
    RangetoHTMLPassdown = Replace(RangetoHTMLPassdown, "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
========================================================================


No comments: