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: -
Function RangetoHTML(rng As Range)
-
Dim fso As Object
-
Dim ts As Object
-
Dim TempFile As String
-
Dim TempWB As Workbook
-
-
TempFile = Environ("temp") & "/" & Format(Date, "dd-mm-yy h-mm-ss") & ".htm"
-
-
'Copy the range and create a new workbook to past the data in
-
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
-
-
'Publish the sheet to a htm file
-
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
-
-
'Read all data from the htm file into RangetoHTML
-
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=")
-
-
'Close TempWB
-
TempWB.Close savechanges:=False
-
-
'Delete the htm file we used in this function
-
Kill (TempFile)
-
-
Set ts = Nothing
-
Set fso = Nothing
-
Set TempWB = Nothing
-
-
End Function
-
Create email: -
Private Sub CreateMail()
-
-
Dim bStarted As Boolean
-
Dim oOutlookApp As Outlook.Application
-
Dim oItem As Outlook.MailItem
-
Dim rng As Range
-
Dim XL_File As String
-
-
XL_File = "C:\test.xlsx"
-
-
On Error Resume Next
-
-
Dim xlApp As Excel.Application
-
Set xlApp = Nothing
-
Set xlApp = CreateObject("Excel.Application")
-
xlApp.Visible = True
-
xlApp.Workbooks.Open XL_File, , False
-
'Set xlApp = Nothing
-
-
Set rng = Nothing
-
Set rng = xlApp.Sheets(1).UsedRange
-
'Set rng = Workbooks.Open("C:\test.xlsx").Sheets(1).UsedRange
-
-
'Get Outlook if it's running
-
Set oOutlookApp = GetObject(, "Outlook.Application")
-
If Err <> 0 Then
-
'Outlook wasn't running, start it from code
-
Set oOutlookApp = CreateObject("Outlook.Application")
-
bStarted = True
-
End If
-
-
'Create new mail item
-
Set oItem = oOutlookApp.CreateItem(olMailItem)
-
-
With oItem
-
'Set the recipient for the new email
-
.To = "someone@somewhere.com"
-
.Subject = "Hello"
-
.Attachments.Add "C:\test.xlsx"
-
.HTMLBody = RangetoHTML(rng)
-
.Display
-
End With
-
-
'If bStarted Then
-
'If we started Outlook from code, then close it
-
'oOutlookApp.Quit
-
'End If
-
-
'Clean up
-
-
Set oItem = Nothing
-
Set oOutlookApp = Nothing
-
Set rng = Nothing
-
-
End Sub
-
0 2914 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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 ?...
|
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...
|
by: datttanand |
last post by:
How to copy the existing excel sheet into another excel sheet without using activex component in java script?
|
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...
|
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...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |