ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Delete (Kill) Active Workbook (https://www.excelbanter.com/excel-programming/334050-delete-kill-active-workbook.html)

stefan via OfficeKB.com

Delete (Kill) Active Workbook
 
Hi,
I'd like to open an existing file, save it automatically by macro in a
different location with a new file name. This portion i can do. I'd like then
to delete the original, still active/open workbook. Any suggestions?
Thank you,
Stefan

okaizawa

Delete (Kill) Active Workbook
 
Hi,

Here is an example:

Sub Test()
On Error GoTo ErrorHandler
If ActiveWorkbook.Path < "" Then
If Not ActiveWorkbook.ReadOnly Then
ActiveWorkbook.Saved = True
ActiveWorkbook.ChangeFileAccess xlReadOnly
End If
Kill ActiveWorkbook.FullName
End If
Exit Sub

ErrorHandler:
MsgBox "Fail to delete file: " & ActiveWorkbook.FullName
Exit Sub

End Sub

I am not sure it is perfectly all right.
I recommend to close the workbook after deleted.

--
HTH

okaizawa

stefan via OfficeKB.com wrote:
Hi,
I'd like to open an existing file, save it automatically by macro in a
different location with a new file name. This portion i can do. I'd like then
to delete the original, still active/open workbook. Any suggestions?
Thank you,
Stefan


Bob Phillips[_7_]

Delete (Kill) Active Workbook
 
Why bother testing if readonly, just set it

Sub Test()
On Error GoTo ErrorHandler
With ActiveWorkbook
If .Path < "" Then
.Saved = True
.ChangeFileAccess xlReadOnly
Kill ActiveWorkbook.FullName
End If
End With
Exit Sub

ErrorHandler:
MsgBox "Fail to delete file: " & ActiveWorkbook.FullName
Exit Sub

End Sub


--
HTH

Bob Phillips

"okaizawa" wrote in message
...
Hi,

Here is an example:

Sub Test()
On Error GoTo ErrorHandler
If ActiveWorkbook.Path < "" Then
If Not ActiveWorkbook.ReadOnly Then
ActiveWorkbook.Saved = True
ActiveWorkbook.ChangeFileAccess xlReadOnly
End If
Kill ActiveWorkbook.FullName
End If
Exit Sub

ErrorHandler:
MsgBox "Fail to delete file: " & ActiveWorkbook.FullName
Exit Sub

End Sub

I am not sure it is perfectly all right.
I recommend to close the workbook after deleted.

--
HTH

okaizawa

stefan via OfficeKB.com wrote:
Hi,
I'd like to open an existing file, save it automatically by macro in a
different location with a new file name. This portion i can do. I'd like

then
to delete the original, still active/open workbook. Any suggestions?
Thank you,
Stefan




okaizawa

Delete (Kill) Active Workbook
 
Bob,

Why bother testing if readonly, just set it

That is only an example.
If a workbook is readonly, "ChangeFileAccess xlReadOnly" fails.
(I use Excel 2000)

--
Regards,
okaizawa

stefan via OfficeKB.com

Delete (Kill) Active Workbook
 
Hello Bob, Hello okaizawa,

Thank you for your help. This works. I went without the fail-save-option as i
am controling the workbooks that are being processed and none would be
readonly to begin with. So it should not be an issue... if so, i'd confuse
the user one way or another anyway. ;-)

Thank you again.

Stefan

okaizawa wrote:
Bob,

Why bother testing if readonly, just set it

That is only an example.
If a workbook is readonly, "ChangeFileAccess xlReadOnly" fails.
(I use Excel 2000)



--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200507/1


All times are GMT +1. The time now is 11:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com