Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8863343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:57:18+00:00 2026-06-14T15:57:18+00:00

I have a VBA code that is copying a range in Excel and pasting

  • 0

I have a VBA code that is copying a range in Excel and pasting into the body of an Outlook email. The code works on several of my colleagues computers but not mine. The code gets as far as creating the .temp file, populating the To:, CC: and Subject but nothing appears in the body. I am trying to paste HTML. I am thinking it’s a setting or something like that, but I’m not sure where to start. Any help on this would be greatly appreciated.

Sub CreateEmail()

   Dim OutApp As Object
   Dim OutMail As Object

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

    sbj = "***"
    toStr = "****"
    Ccstr = "*****"

   Dim rng As Range
   Set rng = Nothing
   Set rng = Range("Flash")

On Error Resume Next
With OutMail
    .To = toStr
    .Display
    .CC = Ccstr
    .BCC = ""
    .Subject = sbj & datestr
    .HTMLBody = RangetoHTML(rng)


End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

Application.ScreenUpdating = False

TempFile = Environ$("temp") & "/" & "temp" & ".htm"

    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

    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

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=")

TempWB.Close savechanges:=False

Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing

Application.ScreenUpdating = True

End Function

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:57:19+00:00Added an answer on June 14, 2026 at 3:57 pm

    I opted to used this method instead. This pastes a range of excel, but not the HTML version. I did not need to use HTML. This works just as good, but without colors and such.

    Sub SendFlash()
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    
    Set rng = Nothing
    On Error Resume Next
    Set rng = Range("K4:L8").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
    
    toStr = "*"
    Ccstr = "*"
    sbj = "Tran"
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    On Error Resume Next
    With OutMail
        .To = toStr
        .CC = Ccstr
        .BCC = ""
        .Subject = sbj & Date
        .Body = TableRangeToTabDelim(rng)
        .display
    End With
    On Error GoTo 0
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    End Sub
    
    Function TableRangeToTabDelim(TableRange As Range) As String
    Dim TableCols As Integer
    Dim TableRows As Integer
    Dim ReturnString As String
    
    TableCols = TableRange.Columns.Count
    TableRows = TableRange.Rows.Count
    
    i = 1
    j = 1
    
    Do While i <= TableRows
        Do While j <= TableCols
            ReturnString = ReturnString & TableRange.Cells(i, j).Value
            If j <> TableCols Then
                ReturnString = ReturnString & Chr(9)
            End If
            j = j + 1
        Loop
        j = 1
        If i <> TableRows Then
            ReturnString = ReturnString & Chr(10)
        End If
        i = i + 1
    Loop
    
    TableRangeToTabDelim = ReturnString
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VBA script that inserts long strings into Excel cells. In some
Does anyone have any vb.net or vba code that will format excel values or
I have written some vba code that selects a range from a workbook and
I have VBA code that refreshes an Excel table using a sproc With ActiveWorkbook.Connections(Connection).OLEDBConnection
I have VBA code that I wrote that creates an Excel Column Cluster chart
I have an Excel workbook, which using VBA code that opens another workbook, copies
I have some VBA code that needs to talk to a running c# application.
I have a method in my VBA code that needs to be assigned to
I have writen some code in VBA (Excel) with error handling labels. It worked
I have Office 2003 VBA code that uses the technique described here to embed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.