473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Copy and Paste Excel Sheet in to Outlook Email

283 100+
Hello,

Ok I am having a problem where I am taking an Excel File and cutting and pasting the info from the first sheet into an Email on a button click on a form.

I actually have it working but my problem is that it will only do it one time and then I have to close the database and re-open it to get it to work again. I think that the problem is that when the program executes I have it creating a temp file and copying the info from the temp and then pasting it, then its supposed to Kill the temp file but i dont think it is until I close out of the db for some reason.

Any help with this would be much appreciated.

My code is in two parts. One is creating the email and putting everything in, the other is a function to cut and paste the excel info.

create temp file, cut and paste:
Expand|Select|Wrap|Line Numbers
  1. Function RangetoHTML(rng As Range)
  2.     Dim fso As Object
  3.     Dim ts As Object
  4.     Dim TempFile As String
  5.     Dim TempWB As Workbook
  6.  
  7.     TempFile = Environ("temp") & "/" & Format(Date, "dd-mm-yy h-mm-ss") & ".htm"
  8.  
  9.     'Copy the range and create a new workbook to past the data in
  10.     rng.Copy
  11.     Set TempWB = Workbooks.Add(1)
  12.     With TempWB.Sheets(1)
  13.         .Cells(1).PasteSpecial Paste:=8
  14.         .Cells(1).PasteSpecial xlPasteValues, , False, False
  15.         .Cells(1).PasteSpecial xlPasteFormats, , False, False
  16.         .Cells(1).Select
  17.         .Application.CutCopyMode = False
  18.         On Error Resume Next
  19.         .DrawingObjects.Visible = True
  20.         .DrawingObjects.Delete
  21.         On Error GoTo 0
  22.     End With
  23.  
  24.     'Publish the sheet to a htm file
  25.     With TempWB.PublishObjects.Add( _
  26.          SourceType:=xlSourceRange, _
  27.          FileName:=TempFile, _
  28.          Sheet:=TempWB.Sheets(1).Name, _
  29.          Source:=TempWB.Sheets(1).UsedRange.Address, _
  30.          HtmlType:=xlHtmlStatic)
  31.         .Publish (True)
  32.     End With
  33.  
  34.     'Read all data from the htm file into RangetoHTML
  35.     Set fso = CreateObject("Scripting.FileSystemObject")
  36.     Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
  37.     RangetoHTML = ts.ReadAll
  38.     ts.Close
  39.     RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
  40.                           "align=left x:publishsource=")
  41.  
  42.     'Close TempWB
  43.     TempWB.Close savechanges:=False
  44.  
  45.     'Delete the htm file we used in this function
  46.     Kill (TempFile)
  47.  
  48.     Set ts = Nothing
  49.     Set fso = Nothing
  50.     Set TempWB = Nothing
  51.  
  52. End Function
  53.  
Create email:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CreateMail()
  2.  
  3. Dim bStarted As Boolean
  4. Dim oOutlookApp As Outlook.Application
  5. Dim oItem As Outlook.MailItem
  6. Dim rng As Range
  7. Dim XL_File As String
  8.  
  9. XL_File = "C:\test.xlsx"
  10.  
  11. On Error Resume Next
  12.  
  13. Dim xlApp As Excel.Application
  14. Set xlApp = Nothing
  15. Set xlApp = CreateObject("Excel.Application")
  16. xlApp.Visible = True
  17. xlApp.Workbooks.Open XL_File, , False
  18. 'Set xlApp = Nothing
  19.  
  20. Set rng = Nothing
  21. Set rng = xlApp.Sheets(1).UsedRange
  22. 'Set rng = Workbooks.Open("C:\test.xlsx").Sheets(1).UsedRange
  23.  
  24. 'Get Outlook if it's running
  25. Set oOutlookApp = GetObject(, "Outlook.Application")
  26. If Err <> 0 Then
  27.     'Outlook wasn't running, start it from code
  28.     Set oOutlookApp = CreateObject("Outlook.Application")
  29.     bStarted = True
  30. End If
  31.  
  32. 'Create new mail item
  33. Set oItem = oOutlookApp.CreateItem(olMailItem)
  34.  
  35. With oItem
  36.     'Set the recipient for the new email
  37.     .To = "someone@somewhere.com"
  38.     .Subject = "Hello"
  39.     .Attachments.Add "C:\test.xlsx"
  40.     .HTMLBody = RangetoHTML(rng)
  41.     .Display
  42. End With
  43.  
  44. 'If bStarted Then
  45.     'If we started Outlook from code, then close it
  46.     'oOutlookApp.Quit
  47. 'End If
  48.  
  49. 'Clean up
  50.  
  51. Set oItem = Nothing
  52. Set oOutlookApp = Nothing
  53. Set rng = Nothing
  54.  
  55. End Sub
  56.  
Jan 25 '12 #1
0 2914

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Rajiv | last post by:
HI ALL, actually , i m working in a company having large number of employee , i have two userid and password . on id is from admin group(id given to me by technology manager) of the server and...
9
by: sifar | last post by:
Hi, I am right now learning PHP & want to know if there is a way to send web form data to an Excel sheet located on a network. My windows xp PC doesnot have a copy of Excel. Also i am not...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
2
by: TM | last post by:
I have an Excel sheet where I setup my needed formatting, page settings, ect, and would like to take my data from a datagrid and paste it into the excel file and print the excel file. Any idea...
3
by: TM | last post by:
Is there any way that I can take the records from a datagrid and paste them into an Excel sheet, then print preview or print the sheet ? Is there any way I can sort the sheet before I print it ?...
7
by: VoTiger | last post by:
Hi everyone, i am ttrying to find a way to do the copy / paste between an existing excel file and my flexgrid (in runtime application). But the fact is that i don't know how to proceed. The...
1
by: datttanand | last post by:
How to copy the existing excel sheet into another excel sheet without using activex component in java script?
9
Boxcar74
by: Boxcar74 | last post by:
I think this is a little crazy but I supposed to figure out how to get the body of an email message (Outlook) into an Access DB table, with little to no effort :) Here is the deal I receive email...
0
by: six888 | last post by:
i need to make a report using excel. im given a few data in excel document for a 1 month report (1 document for each week). each data document have a few work sheet.i need only certain information in...
1
by: StanOlli | last post by:
Hi, Im trying to copy 4 rows from an Excel sheet and paste it in the same sheet ( in C#). e.g. copy rows 8-11 and paste it to row 20. (20-23) Code snippets are appreciate. Thanks Stan
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.