Kill File

BGrubb

New Member
Joined
Feb 25, 2008
Messages
19
I have a macro that saves my original file as another file.

Sub Automatic()
'
' Auto_Open
'

'
ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\bgrubb\My Documents\Commercial\Work With Assessment Form -CSM CAM2.xlsm" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

ActiveSheet.Shapes("Rectangle 2").Select
ActiveSheet.Shapes("Rectangle 2").Delete
End Sub


After changes have been made to the saved file, I save a copy of 1 worksheet and email it. Then I want to delete the saved file. The following macro finishes running but when I go back and look, the file is still there.

Sub Send()
'
' Send Macro
'

'
Range("H2:J2").Select
Selection.Copy
Range("H2:J2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

emailvalue = Worksheets("email").Range("G2").Value
emailvalue2 = Worksheets("email").Range("G3").Value


Dim myOutlook As Object
Dim myMailItem As Object

Dim objSheet As Excel.Worksheet
Dim sheetname As String

Set otlApp = CreateObject("Outlook.Application")
Set otlNewMail = otlApp.CreateItem(olMailItem)

ActiveSheet.Name = ActiveSheet.Range("C2")

sheetname = ActiveWorkbook.ActiveSheet.Name


Set objSheet = ActiveWorkbook.Sheets(sheetname)
FName = "C:\Documents and Settings\BGrubb\My Documents\Commercial" & "\" & "CAM Assessment" & " " & ActiveWorkbook.ActiveSheet.Name


objSheet.Copy

ActiveWorkbook.SaveAs FName



With otlNewMail
.to = emailvalue
.cc = emailvalue2


.Subject = "CAM Assessment"
.Attachments.Add ActiveWorkbook.FullName
.HTMLBody = "<HTML><body><p><font face=""Comic Sans MS"" size=""2"">Attached is a completed CAM Assessment form.</font></b></u></font><br></body></HTML>"
.Send

End With
ActiveWindow.Close
Set otlNewMail = Nothing
Set otlApp = Nothing
Set otlAttach = Nothing
Set otlMess = Nothing
Set otlNSpace = Nothing

ActiveWindow.Activate
ActiveWindow.Close



Kill "C:\Documents and Settings\bgrubb\My Documents\Commercial\Work With Assessment Form -CSM CAM2.xlsm"


End Sub

I've run out of ideas. Any help would be appreciated.


 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try the deletefile method
DeleteFile Method

Description
Deletes a specified file.
Syntax
object.DeleteFile filespec[, force]
The DeleteFile method syntax has these parts:

<TABLE cellSpacing=4 cellPadding=4 cols=2><TBODY><TR vAlign=top><TH width="15%">Part</TH><TH width="85%">Description</TH></TR><TR vAlign=top><TD class=T width="15%">object</TD><TD class=T width="85%">Required. Always the name of a FileSystemObject.</TD></TR><TR vAlign=top><TD class=T width="15%">filespec</TD><TD class=T width="85%">Required. The name of the file to delete. The filespec can contain wildcard characters in the last path component.</TD></TR><TR vAlign=top><TD class=T width="15%">force</TD><TD class=T width="85%">Optional. Boolean value that is True if files with the read-only attribute set are to be deleted; False (default) if they are not.</TD></TR></TBODY></TABLE>


Remarks
An error occurs if no matching files are found. The DeleteFile method stops on the first error it encounters. No attempt is made to roll back or undo any changes that were made before an error occurred.
 
Upvote 0
It seems perhaps you are trying to delete the file the code is running from. This is probably not possible (since the file is in use it can't be killed; if the file were not in use the code could not run anymore). You might (when you save the copy) just save it to a temp directory that is later cleaned up (that's probably what I'd do).
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top