View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Error in a "Kill" command

Nope.

In fact, if you set the attribute to readonly, then you'll have trouble deleting
the file.

The code that you tried to use is based on code that deletes a workbook that's
open in excel. If the file is open in read/write mode, then windows will yell
at you when you try to delete it (either manually or via code).

The .changefileaccess line doesn't change the file's attribute. Instead, it
tells excel to change its access to that file as readonly. Kind of like
originally opening the file in readonly mode.

Then Windows won't see the file/workbook as in-use and the Kill statement will
work.

====
Some notes about your original code:

For Each TheFile In Array("One.doc", "Two.doc", "Three.doc")
Workbooks(ThePathDocs & TheFile).ChangeFileAccess xlReadOnly
Kill Workbooks(ThePathDocs & TheFile).FullName

This doesn't really make sense to me. The file extensions are .doc. I would
think that you would not have this kind of file open in excel.

That means that you would refer to them through the workbooks() collection.

If your code were using excel files and these files were open, you could use:

For Each TheFile In Array("One.xls", "Two.xls", "Three.xls")
Workbooks(TheFile).ChangeFileAccess xlReadOnly
Kill Workbooks(TheFile).FullName

When you use workbooks(), you don't specify the drive/folder/path--just the
filename.



Otto Moehrbach wrote:

Don
Thanks for your help. I understand that one must set the file attribute
to "Read Only" prior to "killing" the file. Is that true? Otto
"Don Guillett" wrote in message
...
This is a sub I use to kill a file where the full path is typed into a
cell along with some other stuff in the same row.
Maybe you need to just set the path and kill that.
Or, maybe just change to

Kill ThePathDocs & TheFile.FullName

Sub KillFile()
mc = Choose(ActiveCell.Column, 4, 3, 2, 1, 0)
wbn = ActiveCell.Offset(0, mc).Value
MyAnswer = MsgBox("Do you want to KILL this file?", vbYesNo)
If MyAnswer = vbYes Then
Kill wbn
ActiveCell.EntireRow.Delete
End If
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Otto Moehrbach" wrote in message
...
Excel XP & Win XP
In this snippet of code I am trying to "Kill" 3 files. I get a
"Subscript out of range" error on the "Kill" command with the first file.
So I changed the order of the files in the "For Each.." line and again I
get that error with the first name. This tells me that the problem is
with the "Kill" line. The "ThePathDocs" is the path to the folder that
holds the files. What did I do wrong? Thanks for your time. Otto

For Each TheFile In Array("One.doc", "Two.doc", "Three.doc")
Workbooks(ThePathDocs & TheFile).ChangeFileAccess xlReadOnly
Kill Workbooks(ThePathDocs & TheFile).FullName



--

Dave Peterson