PC Review


Reply
Thread Tools Rate Thread

Copy Range of Cells from Excel

 
 
BillCPA
Guest
Posts: n/a
 
      28th Jan 2010
I have a range of cells on a spreadsheet that I want to copy to Outlook as
the body of the message. The range is 9 rows by 9 columns. Three rows have
data on them, and each row of data has 2 or 3 sections (2 or 3 columns) of
data. These sections are formatted differently - different fonts, different
colors, date format, number format, etc. Some sections have borders.

If I highlight the range in the spreadsheet, select copy, go to Outlook, and
paste, I get an exact replica of what is on the spreadsheet. I want to make
this happen using VBA. The code below works partially - it gives me all of
the data in the range. The dates show correctly, as do the numbers and
amounts. What it apparently is doing is copying all the text (which, of
course, is what .GetText would appear to do). The spacing between rows of
data is correct. But it is all in one font and one color, there are no
borders, and the spacing between columns of data is not exactly right.

How can I get all the formatting (areas with borders, color, font
parameters, etc.) to move to Outlook - in other words, how can I get VBA to
give me an exact replica like Copy and Paste does?


Public olToName As String
Public olSubject As String
Public olBody As String
Public olRange As Range
Public RangeData As DataObject
Public olAttach1 As String

Sub SendReceipt()

Dim olApp As Outlook.Application
Dim olMail As MailItem
Dim SigString As String
Dim Signature As String

Set olApp = New Outlook.Application
Set RangeData = New DataObject

olToName = Range("K5").Value
olSubject = "Dues Receipt"
Range("A1:I9").Copy
RangeData.GetFromClipboard
olBody = RangeData.GetText
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.To = olToName
.Subject = olSubject
.Body = olBody
.Send
End With

Set olMail = Nothing
Set olApp = Nothing

End Sub

--
Bill @ UAMS
 
Reply With Quote
 
 
 
 
Sue Mosher [MVP]
Guest
Posts: n/a
 
      29th Jan 2010
Copy the range to a new worksheet and then use the technique shown in the
sample at http://www.outlookcode.com/codedetail.aspx?id=1333. The same
MailEnvelope object also works for a Worksheet.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"BillCPA" <Bill @ UAMS> wrote in message
news:3573F798-40FB-4C26-BF7F-(E-Mail Removed)...
>I have a range of cells on a spreadsheet that I want to copy to Outlook as
> the body of the message. The range is 9 rows by 9 columns. Three rows
> have
> data on them, and each row of data has 2 or 3 sections (2 or 3 columns) of
> data. These sections are formatted differently - different fonts,
> different
> colors, date format, number format, etc. Some sections have borders.
>
> If I highlight the range in the spreadsheet, select copy, go to Outlook,
> and
> paste, I get an exact replica of what is on the spreadsheet. I want to
> make
> this happen using VBA. The code below works partially - it gives me all
> of
> the data in the range. The dates show correctly, as do the numbers and
> amounts. What it apparently is doing is copying all the text (which, of
> course, is what .GetText would appear to do). The spacing between rows of
> data is correct. But it is all in one font and one color, there are no
> borders, and the spacing between columns of data is not exactly right.
>
> How can I get all the formatting (areas with borders, color, font
> parameters, etc.) to move to Outlook - in other words, how can I get VBA
> to
> give me an exact replica like Copy and Paste does?
>
>
> Public olToName As String
> Public olSubject As String
> Public olBody As String
> Public olRange As Range
> Public RangeData As DataObject
> Public olAttach1 As String
>
> Sub SendReceipt()
>
> Dim olApp As Outlook.Application
> Dim olMail As MailItem
> Dim SigString As String
> Dim Signature As String
>
> Set olApp = New Outlook.Application
> Set RangeData = New DataObject
>
> olToName = Range("K5").Value
> olSubject = "Dues Receipt"
> Range("A1:I9").Copy
> RangeData.GetFromClipboard
> olBody = RangeData.GetText
> Set olMail = olApp.CreateItem(olMailItem)
> With olMail
> .To = olToName
> .Subject = olSubject
> .Body = olBody
> .Send
> End With
>
> Set olMail = Nothing
> Set olApp = Nothing
>
> End Sub
>
> --
> Bill @ UAMS



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make excel understand to copy a Range of cells which has datain it shriil Microsoft Excel Discussion 1 5th Feb 2009 04:27 AM
copy and paste range of cells excluding hidden data in excel Steve22055 Microsoft Excel Misc 3 23rd Oct 2008 01:57 PM
Need Access to copy range of cells from Excel to Word =?Utf-8?B?QWNjZXNzRGV2?= Microsoft Access VBA Modules 3 10th Oct 2005 05:46 PM
How do I copy a range of cells in Excel using variables? =?Utf-8?B?RG9taW5pYyBQcmluY2U=?= Microsoft Excel Programming 1 11th Apr 2005 11:40 PM
Excel - Copy range of cells based on Combobox Selection Excel-erate2004 Microsoft Excel Programming 2 3rd Apr 2004 05:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:19 AM.