How to Copy or Delete Excel Files using VB 6.0?

I

Ivan Lozada

Ladies, Gents:

This copy/delete operation (using FileSystem or FileCopy) will always fail
if the destination file is an Excel File that is open (the destination file
is not read-only).

Copy "c:\From\abc.xls", "c:\To\abc.xls"

Does anybody has a suggestion as to how to make this copy work when the
destination file is open?

It should be noticed that for a text file, for example, abc.txt, this
operation will work even if the destination file is open in a text editor. It
just doen't work for Excel.

Thanks for your time,
Ivan
 
C

Chip Pearson

Try some code like the following:

Dim XL As Object
On Error Resume Next
Set XL = GetObject(, "Excel.Application")
If XL Is Nothing Then
Exit Sub
End If
On Error Goto 0
XL.Workbooks("Book4.xls").changefileaccess 3 ' xlReadOnly
FileCopy "H:\Book4.xls", "H:\Book44.xls"
XL.Workbooks("Book4.xls").changefileaccess 2 ' xlReadWrite

You can substitute ActiveWorkbook for Workbooks("Book4.xls") if
you want.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


message
news:[email protected]...
 
I

Ivan Lozada

Chip,

I'll give it a try. I notice that you change the file permissions. In my
case the permissions are read/write before I try to do the copy ...
interesting! ... Thanks for your time.

Ivan
 
I

Ivan Lozada

Chip,

Oh Well! I tried it but it does not work.

I think this issue is a little more complicated and goes beyond simply VB or
Excel. If you try to do the copy from a DOS Window using "Copy" you get this
error message: "The requested operation cannot be performed on a file with a
user-mapped section open"

Just to be clear the Source Excel File is NOT open. The Destination Excel
File IS open.

However, it gets even more intersting, at least in my network, if the userid
of the person who opens the destination file is different than the userid of
the person execting the vb program, the the copy takes place .... :))

.... will always appreciate a suggestion,
Thanks, Ivan
 
Top