From: Ed from AZ on 7 Apr 2010 13:19 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
From: Ed from AZ on 7 Apr 2010 14:42 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...(a)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
From: Jay Freedman on 7 Apr 2010 15:10 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...(a)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
From: mcescher on 7 Apr 2010 16:28 On Apr 7, 1:42 pm, Ed from AZ <prof_ofw...(a)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.
|
Pages: 1 Prev: find and replace in footer Next: Adding a row to a protected table - (attn: G. Mayor) |