PC Review


Reply
Thread Tools Rate Thread

Can't delete file?!!?

 
 
Ed from AZ
Guest
Posts: n/a
 
      7th Apr 2010
I'm running a macro in XL2007 in Vista which is manipulating a Word
document. At the end of everything, I want to delete the Word file.
The Word file has been properly saved through VBA unsing SaveAs - it
exists and I use the object to set ranges and such, so it's definitely
a good file.

But I can't delete it. I'm trying Kill and I'm trying
FSO.DeleteFile. For some reason, my computer keeps flipping my file
paths between C:\Users and C:\Documents and Settings, so I created two
strings and am using both operations on both strings. Debug.Print
shows good file paths. But the file won't go away!!

Any help is greatly appreciated!
Ed

On Error Resume Next
'Delete Word doc
strPath = objDoc.FullName
strFile = Replace(strPath, "Users", "Documents and Settings")

Debug.Print strPath
Debug.Print strFile
Stop
Stop

objDoc.Close SaveChanges:=wdSaveChanges
x = 0
Do
x = x + 1
objFSO.DeleteFile strPath, True
objFSO.DeleteFile strFile, True
Kill strPath
Kill strFile
If objFSO.FileExists(strPath) = False Then
If objFSO.FileExists(strFile) = False Then
Exit Do
End If
End If

If x = 6 Then
MsgBox "This program created a Word file." & _
vbCrLf & "Please delete this file."
Exit Do
End If
Loop
 
Reply With Quote
 
 
 
 
Ed from AZ
Guest
Posts: n/a
 
      7th Apr 2010
Further piddling shows that when I remove
objDoc.Close SaveChanges:=wdSaveChanges
from the On Error Resume Next, it throws an error.

So the file won't delete because it's still open. That's solved.
Now - why won't it close??

I will continue playing - any helpful drop-kicks are always
appreciated.
Ed


On Apr 7, 10:19*am, Ed from AZ <prof_ofw...@yahoo.com> wrote:
> I'm running a macro in XL2007 in Vista which is manipulating a Word
> document. *At the end of everything, I want to delete the Word file.
> The Word file has been properly saved through VBA unsing SaveAs - it
> exists and I use the object to set ranges and such, so it's definitely
> a good file.
>
> But I can't delete it. *I'm trying Kill and I'm trying
> FSO.DeleteFile. *For some reason, my computer keeps flipping my file
> paths between C:\Users and C:\Documents and Settings, so I created two
> strings and am using both operations on both strings. *Debug.Print
> shows good file paths. *But the file won't go away!!
>
> Any help is greatly appreciated!
> Ed
>
> On Error Resume Next
> 'Delete Word doc
> strPath = objDoc.FullName
> strFile = Replace(strPath, "Users", "Documents and Settings")
>
> Debug.Print strPath
> Debug.Print strFile
> Stop
> Stop
>
> objDoc.Close SaveChanges:=wdSaveChanges
> x = 0
> Do
> * x = x + 1
> * objFSO.DeleteFile strPath, True
> * objFSO.DeleteFile strFile, True
> * Kill strPath
> * Kill strFile
> * If objFSO.FileExists(strPath) = False Then
> * * If objFSO.FileExists(strFile) = False Then
> * * * Exit Do
> * * End If
> * End If
>
> * If x = 6 Then
> * * MsgBox "This program created a Word file." & _
> * * * * * *vbCrLf & "Please delete this file."
> * * Exit Do
> * End If
> Loop


 
Reply With Quote
 
Jay Freedman
Guest
Posts: n/a
 
      7th Apr 2010
It would be useful to know the exact wording of the error message. There are
lots of possible causes -- the most common one is insufficient rights in the
folder where the file is stored.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Ed from AZ wrote:
> Further piddling shows that when I remove
> objDoc.Close SaveChanges:=wdSaveChanges
> from the On Error Resume Next, it throws an error.
>
> So the file won't delete because it's still open. That's solved.
> Now - why won't it close??
>
> I will continue playing - any helpful drop-kicks are always
> appreciated.
> Ed
>
>
> On Apr 7, 10:19 am, Ed from AZ <prof_ofw...@yahoo.com> wrote:
>> I'm running a macro in XL2007 in Vista which is manipulating a Word
>> document. At the end of everything, I want to delete the Word file.
>> The Word file has been properly saved through VBA unsing SaveAs - it
>> exists and I use the object to set ranges and such, so it's
>> definitely a good file.
>>
>> But I can't delete it. I'm trying Kill and I'm trying
>> FSO.DeleteFile. For some reason, my computer keeps flipping my file
>> paths between C:\Users and C:\Documents and Settings, so I created
>> two strings and am using both operations on both strings. Debug.Print
>> shows good file paths. But the file won't go away!!
>>
>> Any help is greatly appreciated!
>> Ed
>>
>> On Error Resume Next
>> 'Delete Word doc
>> strPath = objDoc.FullName
>> strFile = Replace(strPath, "Users", "Documents and Settings")
>>
>> Debug.Print strPath
>> Debug.Print strFile
>> Stop
>> Stop
>>
>> objDoc.Close SaveChanges:=wdSaveChanges
>> x = 0
>> Do
>> x = x + 1
>> objFSO.DeleteFile strPath, True
>> objFSO.DeleteFile strFile, True
>> Kill strPath
>> Kill strFile
>> If objFSO.FileExists(strPath) = False Then
>> If objFSO.FileExists(strFile) = False Then
>> Exit Do
>> End If
>> End If
>>
>> If x = 6 Then
>> MsgBox "This program created a Word file." & _
>> vbCrLf & "Please delete this file."
>> Exit Do
>> End If
>> Loop



 
Reply With Quote
 
mcescher
Guest
Posts: n/a
 
      7th Apr 2010
On Apr 7, 1:42*pm, Ed from AZ <prof_ofw...@yahoo.com> wrote:
> Further piddling shows that when I remove
> * * objDoc.Close SaveChanges:=wdSaveChanges
> from the On Error Resume Next, it throws an error.
>
> So the file won't delete because it's still open. *That's solved.
> Now - why won't it close??
>
> I will continue playing - any helpful drop-kicks are always
> appreciated.
> Ed
>



So, what is the value of wdSaveChanges? Is that a valid value for
SaveChanges? Are there other required parameters that you're
missing? Have you take the two Stop statements out?

Hope this helps,
Chris M.
 
Reply With Quote
 
Ed from AZ
Guest
Posts: n/a
 
      7th Apr 2010
The error message was simply: "Method SaveAs of object Document not
available."

Aware that I'm using Excel VBA to run Word, I broke it up a bit into
objDoc.Save and then objDoc.Close.

The Save threw an error. I hit Debug, checked a variable, then hit F8
to run back over the line and get the error message. It ran the line
and continued on.

I'm assuming I've got some background processes on my machine that are
slowing down file operations. Maybe I've got too much going on in
this macro with Word, Excel, and FSO. Anyway, I wrapped the Save in a
loop like this:

x = 0
On Error Resume Next
Do
x = x + 1
objDoc.Save
If x = 6 Then
If objDoc.Saved = False Then
MsgBox "Doc won't save!"
Exit Do
End If
End If
Loop While objDoc.Saved = False

objDoc.Close wdDoNotSaveChanges

Saves just fine on the second time (so far), then closes and deletes.

So all is good. Glad I found this on my machine, because it would
have given me fits if it worked okay on mine but not for someone else.

Thank you, Jay and Chris, for the support.
Ed



On Apr 7, 1:28*pm, mcescher <chris.meind...@gmail.com> wrote:
> On Apr 7, 1:42*pm, Ed from AZ <prof_ofw...@yahoo.com> wrote:
>
> > Further piddling shows that when I remove
> > * * objDoc.Close SaveChanges:=wdSaveChanges
> > from the On Error Resume Next, it throws an error.

>
> > So the file won't delete because it's still open. *That's solved.
> > Now - why won't it close??

>
> > I will continue playing - any helpful drop-kicks are always
> > appreciated.
> > Ed

>
> So, what is the value of wdSaveChanges? *Is that a valid value for
> SaveChanges? *Are there other required parameters that you're
> missing? *Have you take the two Stop statements out?
>
> Hope this helps,
> Chris M.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
can i delete an email and skip the delete file as a single step Hugh Microsoft Outlook Discussion 1 8th Apr 2010 01:07 PM
Delete only unread messages in my delete file =?Utf-8?B?U3Vl?= Microsoft Outlook Discussion 3 7th Jul 2005 11:21 AM
Difference between Delete Subfolders and File and Delete =?Utf-8?B?Um9i?= Microsoft Windows 2000 File System 0 29th Jun 2005 03:31 PM
Re: Macro to delete sheets and saves remaining file does not properly delete module gazornenplat Microsoft Excel Programming 0 22nd Jun 2005 01:12 AM
Macro to delete sheets and saves remaining file does not properly delete module pherrero Microsoft Excel Programming 7 21st Jun 2005 05:16 PM


Features
 

Advertising
 

Newsgroups
 


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