1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Word Macro Delete File

Discussion in 'Business Applications' started by theos340, Apr 11, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. theos340

    theos340 Thread Starter

    Joined:
    Apr 11, 2008
    Messages:
    2
    I am try to write a Word macro that when the user opens an existing file in Folder A and presses a specific key the file is saved in Folder B and the file in folder A is deleted.

    I can't get the delete part working. The new file is saved using the ActiveDocument.Saveas method, but It seems that the original file is still locked by word and therefore can not be deleted.

    I've tried using the following but no luck

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFile (FILENAME), True

    Any ideas?
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Please post the rest of your existing code.

    Regards,
    Rollin
     
  3. theos340

    theos340 Thread Starter

    Joined:
    Apr 11, 2008
    Messages:
    2
    (Original File would be located at c:\FolderA\text.doc

    '-----------------------------------------------------------------

    strUnproceededFile = ActiveDocument.FullName

    ActiveDocument.SaveAs FileName:="c:\FolderB\test.doc"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFile (strUnproceededFile), True

    '-----------------------------------------------------------------

    I've also tried the Kill command.
     
  4. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    The code works fine for me. Have you tried to manually close the document using code

    Code:
    Sub DeleteDoc()
    
    strUnproceededFile = ActiveDocument.FullName
    
    strDocName = ActiveDocument.Name
    
    ActiveDocument.SaveAs FileName:="c:\Test\test.doc"
    
    For Each wdDoc In Application.Documents
    If wdDoc.Name = strDocName Then
    wdDoc.Close
    End If
    Next wdDoc
    
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFile (strUnproceededFile), True
    
    End Sub
    Regards,
    Rollin
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/702755

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice