How to Change my code to late binding?

leostamato

New Member
Joined
Nov 7, 2018
Messages
2
Code:
Sub CommentsEmail()


Dim template As Workbook
Dim dashboard As Worksheet
Dim comments As Worksheet
Dim OutApp As Object
Dim OutMail As Object
Dim olApp As Outlook.Application
Dim mymail As Outlook.mailItem
Dim objSel As Word.Selection
Dim commentsrange As Range
Dim branch As String
Dim Sendto As String


UpdateScreen = False


Shell ("Outlook")


Set olApp = New Outlook.Application
Set mymail = olApp.CreateItem(olMailItem)
Set template = ActiveWorkbook
Set dashboard = template.Worksheets("Dashboard")
Set comments = template.Worksheets("Comments")
branch = dashboard.Cells(1, 25)
Sendto = comments.Cells(2, 10)
Set commentsrange = comments.Range(Cells(7, 1), Cells(52, 4))


template.Activate




Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


'OutMail.Display
Dim wordDoc As Word.Document
Set wordDoc = OutMail.GetInspector.WordEditor


Set objSel = wordDoc.Windows(1).Selection


        'construct the body of the email here
        With objSel


            'first text
            .InsertAfter "Dear All," & vbCrLf
            .Move wdParagraph, 1


            'second text
            .InsertAfter vbCrLf & "See below the Comments for Flash Indicator - " & branch & vbCrLf & vbCrLf
            .Move wdParagraph, 1


            'copy a range and paste a picture
            commentsrange.Copy ''again, you need to modify your target range
            .PasteAndFormat wdChartPicture
            .Move wdParagraph, 1


            .InsertAfter vbCrLf & "Let us know of any questions." & vbCrLf & vbCrLf
            .Move wdParagraph, 1


            .InsertAfter vbCrLf & "Kind Regards," & vbCrLf
        End With
        
        OutMail.To = OutMail.To & ";" & Sendto


        With OutMail
    
         .Subject = "Comments on Flash Indicator Results - " & branch
         .Attachments.Add (ActiveWorkbook.FullName)
         .Display
        End With
    
On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    


        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        Exit Sub
    
End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
is there anyone that can help me on this issue? I have stopped here:

Code:
[COLOR=#242729][FONT=Consolas]Sub CommentsEmail2()[/FONT][/COLOR]<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">
Dim template As Workbook
Dim dashboard As Worksheet
Dim comments As Worksheet
Dim OlaApp As Object
Dim OleMail As Object

Dim TempFilePath As String
Dim xHTMLBody As String

Dim commentsrange As Range
Dim branch As String
Dim Sendto As String

UpdateScreen = False

Set template = ActiveWorkbook
Set dashboard = template.Worksheets("Dashboard")
Set comments = template.Worksheets("Comments")
Set commentsrange = comments.Range(Cells(7, 1), Cells(52, 4))

branch = dashboard.Cells(1, 25)
Sendto = comments.Cells(2, 10)

template.Activate

 On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then Set olApp = CreateObject("Outlook.Application")
    On Error GoTo 0
    Set OleMail = olApp.CreateItem(0)

Call createJpg(ActiveSheet.comments, commentsrange, "DashboardFile")
        TempFilePath = Environ$("temp") & "\"
        xHTMLBody = "<span LANG=EN>" _
                & "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>" _
                & "Hello, this is the data range that you want:<br> " _
                & "<br>" _
                & "<img src='cid:DashboardFile.jpg'>" _
                & "<br>Best Regards!</font></span>"
        With OleMail
            .Subject = "test"
            .HTMLBody = xHTMLBody
          .Attachments.Add TempFilePath & "DashboardFile.jpg", olByValue
          .Attachments.Add (ActiveWorkbook.FullName)
            .To = " "
            .Cc = " "
            .Display
        End With




        Set OleMail = Nothing
        Set OlaApp = Nothing


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

End Sub

Sub createJpg(SheetName As String, commentsrange As String, nameFile As String)
    Dim xRgPic As Range
    ThisWorkbook.Activate
    Worksheets(comments).Activate
    Set xRgPic = ThisWorkbook.Worksheets(comments).Range(Cells(7, 1), Cells(52, 4))
    xRgPic.CopyPicture
    With ThisWorkbook.Worksheets(comments).ChartObjects.Add(xRgPic.Left, xRgPic.Top, xRgPic.Width, xRgPic.Height)
        .Activate
        .Chart.Paste
        .Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
    End With
    Worksheets(comments).ChartObjects(Worksheets(comments).ChartObjects.Count).Delete
Set xRgPic = Nothing </code>[COLOR=#242729][FONT=Consolas]End Sub
[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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