Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Sending excel email, then deleting it

Hi all:

I have an excel workbook that has 18 worksheets. After I have completed
a task, I copy 2 of the worksheets to a new workbook to email to a
person that stores it in an access database (I'll call her the database
keeper.) The database keeper wants the subject line to read a certain
way and so I have written the code in the following way...I have
written the code so that after I copy the two worksheets I need to a
new workbook, I remove all the equations (with a copy, paste special
value, values) and then save the workbook to the c:\ directory. I then
put that into an email using outlook and send it to the database
keeper. After that, I want to delete this new file on the c:\ I have
just made as I don't need it anymore. Here is where my problem arises.
After I send the email, I have a kill myfile code line. When the
database keeper opens the file, a box comes up that reads 'The system
cannot find the file specified.' If I take out the kill myfile code
line, the database keeper can open the file (but then the file stays on
my harddrive and I don't want that to happen.) Is there anyway, that I
can rewrite this code or alter this code so that I can delete this file
but still get it in the attachment. Below is the code that I am
referring to. I have added all the code (which is run from a command
button) as I am not sure what someone needs to help out. Any help is
greatly appreciated. Thanks in advance. ~Matt

The code...

Private Sub CommandButton1_Click()
' Macro recorded 7/13/2006 by Matt Sonnier
Dim sbj As String
Dim sCustomer As String
Dim sField As String
Dim sWellNo As String
Dim sSO_No As String
Dim sTreatment As String
Dim sPE_EngrName As String
Dim MyFile As String
Dim myOlApp As Variant
Dim myitem As Variant
Dim myAttachments As Variant
Dim myAttachment As Variant
Dim olMailItem As Variant
Dim email_msg As String
Dim email_address As String
Set myOlApp = CreateObject("Outlook.Application")
Set myitem = myOlApp.CreateItem(olMailItem)


' Finds cell named Customer, Field, Well, PE_SalesOrderNo, Treatment,
PE_EngrName and saves the contents in memory
Sheets("SC Database").Activate
Application.Goto Reference:="Customer"
sCustomer = ActiveSheet.Range("Customer")
'MsgBox "Is this correct?1", vbOKOnly, "Is this correct?"
sField = ActiveSheet.Range("Field")
sWellNo = ActiveSheet.Range("Well")
sSO_No = ActiveSheet.Range("PE_SalesOrderNo")
sTreatment = ActiveSheet.Range("Treatment")
sPeEngr1 = ActiveSheet.Range("PeEngr1")
' This puts the name of the subject line into one line.
sbj = sCustomer & "- " & sField & " " & sWellNo & " (SO #" & sSO_No
& ") " & sTreatment

' Copies the "Sum" and (SC Database" worksheet to a new workbook
Sheets(Array("Sum", "SC Database")).Select
Sheets(Array("Sum", "SC Database")).Copy
ActiveWorkbook.Sheets("SC Database").Select
ActiveWorkbook.Sheets("Sum").Select
ActiveWorkbook.Sheets("SC Database").Select
ActiveWorkbook.Sheets("Sum").Select
ActiveSheet.Range("A1:J58").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
Application.CutCopyMode = False
Sheets("SC Database").Select
ActiveSheet.Range("A1:G92").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone

Application.CutCopyMode = False
ActiveSheet.Range("A1").Select
ActiveWorkbook.Sheets("Sum").Select
ActiveSheet.Range("A1").Select
Sheets("SC Database").Visible = True

' Saves the open workbook to C:\ directory
ActiveWorkbook.SaveAs Filename:="c:\" & sbj & ".xls",
FileFormat:=xlNormal
' Closes New Active Workbook
ActiveWorkbook.Close
' Add a message box to open Outlook!
MsgBox "Make sure Microsoft Outlook is open." & vbCrLf _
& "If it isn't, open it before hitting the ok button!" _
, vbOKOnly, "Is Microsoft Outlook open?"

' The following code was added by Mike Pettee on 18Jul06
' This gets the information for the body of the email.
email_msg = "I have finished the report."

email_address = InputBox("Input the email address you" & vbCrLf &
"wish to send attachment to.", ,")
myitem.to = email_address
myitem.Subject = sbj
myitem.body = email_msg
' Inserts the attachment file that is created
myitem.Attachments.Add "c:\" & sbj & ".xls", olByValue, ,
"Attachment"
With myitem
.send
End With
On Error Resume Next 'On hitting errors, code resumes next code
'Sends a message box saying the email was sent!
MsgBox "The email was sent to:" & vbCrLf & vbCrLf & email_address,
vbOKOnly, "The email was sent!"
' Deletes file on c:\ that was created
MyFile = "c:\" & sbj & ".xls"
Kill MyFile
ActiveWorkbook.Sheets("SC Database").Select
ActiveSheet.Range("A1").Select
ActiveWorkbook.Sheets("Input").Select
ActiveSheet.Range("A1").Select

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Sending excel email, then deleting it

Hi Matt

Maybe you have links in other cells outside the range

Test this

Sub Mail_SheetsArray_Outlook()
'You must add a reference to the Microsoft outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim wb As Workbook
Dim strdate As String
strdate = Format(Now, "dd-mm-yy h-mm-ss")
Application.ScreenUpdating = False
Sheets(Array("Sum", "SC Database")).Copy

Worksheets.Select
Cells.Copy
Cells.PasteSpecial xlPasteValues
Cells(1).Select
Worksheets(1).Select
Application.CutCopyMode = False


Set wb = ActiveWorkbook
With wb
.SaveAs "Part of " & ThisWorkbook.Name _
& " " & strdate & ".xls"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add wb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send 'or .Display
End With
.ChangeFileAccess xlReadOnly
Kill .FullName
.Close False
End With
Application.ScreenUpdating = True
Set OutMail = Nothing
Set OutApp = Nothing
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Matt" wrote in message oups.com...
Hi all:

I have an excel workbook that has 18 worksheets. After I have completed
a task, I copy 2 of the worksheets to a new workbook to email to a
person that stores it in an access database (I'll call her the database
keeper.) The database keeper wants the subject line to read a certain
way and so I have written the code in the following way...I have
written the code so that after I copy the two worksheets I need to a
new workbook, I remove all the equations (with a copy, paste special
value, values) and then save the workbook to the c:\ directory. I then
put that into an email using outlook and send it to the database
keeper. After that, I want to delete this new file on the c:\ I have
just made as I don't need it anymore. Here is where my problem arises.
After I send the email, I have a kill myfile code line. When the
database keeper opens the file, a box comes up that reads 'The system
cannot find the file specified.' If I take out the kill myfile code
line, the database keeper can open the file (but then the file stays on
my harddrive and I don't want that to happen.) Is there anyway, that I
can rewrite this code or alter this code so that I can delete this file
but still get it in the attachment. Below is the code that I am
referring to. I have added all the code (which is run from a command
button) as I am not sure what someone needs to help out. Any help is
greatly appreciated. Thanks in advance. ~Matt

The code...

Private Sub CommandButton1_Click()
' Macro recorded 7/13/2006 by Matt Sonnier
Dim sbj As String
Dim sCustomer As String
Dim sField As String
Dim sWellNo As String
Dim sSO_No As String
Dim sTreatment As String
Dim sPE_EngrName As String
Dim MyFile As String
Dim myOlApp As Variant
Dim myitem As Variant
Dim myAttachments As Variant
Dim myAttachment As Variant
Dim olMailItem As Variant
Dim email_msg As String
Dim email_address As String
Set myOlApp = CreateObject("Outlook.Application")
Set myitem = myOlApp.CreateItem(olMailItem)


' Finds cell named Customer, Field, Well, PE_SalesOrderNo, Treatment,
PE_EngrName and saves the contents in memory
Sheets("SC Database").Activate
Application.Goto Reference:="Customer"
sCustomer = ActiveSheet.Range("Customer")
'MsgBox "Is this correct?1", vbOKOnly, "Is this correct?"
sField = ActiveSheet.Range("Field")
sWellNo = ActiveSheet.Range("Well")
sSO_No = ActiveSheet.Range("PE_SalesOrderNo")
sTreatment = ActiveSheet.Range("Treatment")
sPeEngr1 = ActiveSheet.Range("PeEngr1")
' This puts the name of the subject line into one line.
sbj = sCustomer & "- " & sField & " " & sWellNo & " (SO #" & sSO_No
& ") " & sTreatment

' Copies the "Sum" and (SC Database" worksheet to a new workbook
Sheets(Array("Sum", "SC Database")).Select
Sheets(Array("Sum", "SC Database")).Copy
ActiveWorkbook.Sheets("SC Database").Select
ActiveWorkbook.Sheets("Sum").Select
ActiveWorkbook.Sheets("SC Database").Select
ActiveWorkbook.Sheets("Sum").Select
ActiveSheet.Range("A1:J58").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
Application.CutCopyMode = False
Sheets("SC Database").Select
ActiveSheet.Range("A1:G92").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone

Application.CutCopyMode = False
ActiveSheet.Range("A1").Select
ActiveWorkbook.Sheets("Sum").Select
ActiveSheet.Range("A1").Select
Sheets("SC Database").Visible = True

' Saves the open workbook to C:\ directory
ActiveWorkbook.SaveAs Filename:="c:\" & sbj & ".xls",
FileFormat:=xlNormal
' Closes New Active Workbook
ActiveWorkbook.Close
' Add a message box to open Outlook!
MsgBox "Make sure Microsoft Outlook is open." & vbCrLf _
& "If it isn't, open it before hitting the ok button!" _
, vbOKOnly, "Is Microsoft Outlook open?"

' The following code was added by Mike Pettee on 18Jul06
' This gets the information for the body of the email.
email_msg = "I have finished the report."

email_address = InputBox("Input the email address you" & vbCrLf &
"wish to send attachment to.", ,")
myitem.to = email_address
myitem.Subject = sbj
myitem.body = email_msg
' Inserts the attachment file that is created
myitem.Attachments.Add "c:\" & sbj & ".xls", olByValue, ,
"Attachment"
With myitem
.send
End With
On Error Resume Next 'On hitting errors, code resumes next code
'Sends a message box saying the email was sent!
MsgBox "The email was sent to:" & vbCrLf & vbCrLf & email_address,
vbOKOnly, "The email was sent!"
' Deletes file on c:\ that was created
MyFile = "c:\" & sbj & ".xls"
Kill MyFile
ActiveWorkbook.Sheets("SC Database").Select
ActiveSheet.Range("A1").Select
ActiveWorkbook.Sheets("Input").Select
ActiveSheet.Range("A1").Select

End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sending email from excel 97 tysop Excel Programming 1 January 26th 06 03:01 PM
Sending Email From Excel Help! lehainam[_19_] Excel Programming 3 September 6th 05 05:11 AM
sending email from Excel nuExcel[_4_] Excel Programming 1 June 7th 04 04:24 AM
Sending email from Excel Denise Posey Excel Programming 9 February 4th 04 09:29 AM
sending email in Excel Peter W[_2_] Excel Programming 1 September 15th 03 04:13 PM


All times are GMT +1. The time now is 06:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"