+ Reply to Thread
Results 1 to 10 of 10

Copy from Excel to Outlook as HTML

Hybrid View

vjethk Copy from Excel to Outlook as... 12-07-2012, 12:51 PM
TMS Re: Copy from Excel to... 12-07-2012, 01:24 PM
vjethk Re: Copy from Excel to... 12-07-2012, 11:11 PM
event21 Re: Copy from Excel to... 12-08-2012, 01:16 AM
vjethk Re: Copy from Excel to... 12-09-2012, 01:35 PM
vjethk Re: Copy from Excel to... 12-08-2012, 03:42 AM
event21 Re: Copy from Excel to... 12-08-2012, 04:04 AM
vjethk Re: Copy from Excel to... 12-08-2012, 04:41 AM
vjethk Re: Copy from Excel to... 12-08-2012, 04:43 AM
event21 Re: Copy from Excel to... 12-08-2012, 05:07 AM
  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Copy from Excel to Outlook as HTML

    Hi -

    You can alter the second code you have to something like;
    Sub InsertSheetContent()
      Dim onePublishObject As PublishObject
      Dim oneSheet As Worksheet
      Dim scriptingObject As Object
      Dim outlookApplication As Object
      Dim outlookMail As Object
      Dim htmlBody As String
      Dim htmlFile As String
      Dim textStream, fil As String
    
      Set scriptingObject = CreateObject("Scripting.FileSystemObject")
      Set outlookApplication = CreateObject("Outlook.Application")
      For Each oneSheet In ThisWorkbook.Worksheets
        htmlFile = ThisWorkbook.Path & "\" & ThisWorkbook.Name & "_" & oneSheet.Name & ".html"
        Set onePublishObject = ThisWorkbook.PublishObjects.Add(SourceType:=xlSourceRange, _
                                                                Filename:=htmlFile, _
                                                                Sheet:=oneSheet.Name, _
                                                                Source:=oneSheet.UsedRange.Address, _
                                                                HtmlType:=xlHtmlStatic, _
                                                                DivID:=oneSheet.Name)
        onePublishObject.Publish Create:=True
    
        Set textStream = scriptingObject.OpenTextFile(htmlFile)
        htmlBody = textStream.ReadAll
        Application.DisplayAlerts = False
        Sheets(oneSheet.Name).Copy
        ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & oneSheet.Name & ".xlsx"
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
        Set outlookMail = outlookApplication.CreateItem(0)
        With outlookMail
            .htmlBody = htmlBody
            .attachments.Add ThisWorkbook.Path & "\" & oneSheet.Name & ".xlsx"
            .Display
        End With
      Next oneSheet
    End Sub
    Regards,
    Event

  2. #2
    Registered User
    Join Date
    12-03-2012
    Location
    mandya
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Copy from Excel to Outlook as HTML

    Quote Originally Posted by event21 View Post
    Hi -

    You can alter the second code you have to something like;
    Sub InsertSheetContent()
      Dim onePublishObject As PublishObject
      Dim oneSheet As Worksheet
      Dim scriptingObject As Object
      Dim outlookApplication As Object
      Dim outlookMail As Object
      Dim htmlBody As String
      Dim htmlFile As String
      Dim textStream, fil As String
    
      Set scriptingObject = CreateObject("Scripting.FileSystemObject")
      Set outlookApplication = CreateObject("Outlook.Application")
      For Each oneSheet In ThisWorkbook.Worksheets
        htmlFile = ThisWorkbook.Path & "\" & ThisWorkbook.Name & "_" & oneSheet.Name & ".html"
        Set onePublishObject = ThisWorkbook.PublishObjects.Add(SourceType:=xlSourceRange, _
                                                                Filename:=htmlFile, _
                                                                Sheet:=oneSheet.Name, _
                                                                Source:=oneSheet.UsedRange.Address, _
                                                                HtmlType:=xlHtmlStatic, _
                                                                DivID:=oneSheet.Name)
        onePublishObject.Publish Create:=True
    
        Set textStream = scriptingObject.OpenTextFile(htmlFile)
        htmlBody = textStream.ReadAll
        Application.DisplayAlerts = False
        Sheets(oneSheet.Name).Copy
        ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & oneSheet.Name & ".xlsx"
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
        Set outlookMail = outlookApplication.CreateItem(0)
        With outlookMail
            .htmlBody = htmlBody
            .attachments.Add ThisWorkbook.Path & "\" & oneSheet.Name & ".xlsx"
            .Display
        End With
      Next oneSheet
    End Sub
    Regards,
    Event
    I''ve modified the code to add a subject but it doesn't work
    I'm getting a run time error 9
    Subscript out of range

    Sheets(oneSheet.Name).Copy (IN THIS LINE)

    Can any one help.

    Sub SendToALL()
      Dim onePublishObject As PublishObject
      Dim oneSheet As Worksheet
      Dim scriptingObject As Object
      Dim outlookApplication As Object
      Dim outlookMail As Object
      Dim htmlBody As String
      Dim htmlFile As String
      Dim textStream, fil As String
                                                             
                                                             
      Today = Format(Now(), "dd-mm-yyyy")
     
     
      Set scriptingObject = CreateObject("Scripting.FileSystemObject")
      Set outlookApplication = CreateObject("Outlook.Application")
      For Each oneSheet In ThisWorkbook.Worksheets
        htmlFile = ThisWorkbook.Path & "\" & ThisWorkbook.Name & "_" & oneSheet.Name & ".html"
        Set onePublishObject = ThisWorkbook.PublishObjects.Add(SourceType:=xlSourceRange, _
                                                                Filename:=htmlFile, _
                                                                Sheet:=oneSheet.Name, _
                                                                Source:=oneSheet.UsedRange.Address, _
                                                                HtmlType:=xlHtmlStatic, _
                                                                DivID:=oneSheet.Name)
        onePublishObject.Publish Create:=True
     
     
     
     
                                                            Dim StrBody As String
                                                            StrBody = " Dear" & " " & UCase(oneSheet.Name) & " " & "All," & vbNewLine & _
                                                            vbNewLine & _
                                                            "Please find attached the <B>'REPORT'<B>"
     
     
                                                     
                                                             
                                                             
        Set textStream = scriptingObject.OpenTextFile(htmlFile)
        htmlBody = textStream.ReadAll
        Application.DisplayAlerts = False
        Sheets(oneSheet.Name).Copy
        ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & oneSheet.Name & ".xlsx"
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
        Set outlookMail = outlookApplication.CreateItem(0)
        With outlookMail
            .htmlBody = StrBody & htmlBody
            .attachments.Add ThisWorkbook.Path & "\" & oneSheet.Name & ".xlsx"
            .Display
            .Subject = "REPORT" & " " & UCase(oneSheet.Name) & " " & "(" & Today & ")"
             
        End With
      Next oneSheet
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1