Results 1 to 5 of 5

Thread: [2003] Excel+VBA: Save old doc to new location/delete previous doc

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    9

    [2003] Excel+VBA: Save old doc to new location/delete previous doc

    Here's my current issue, hopefully someone has some advice on this solution:


    Basically, I have an excel doc saved somewhere on a networked drive. What I want, is if someone goes to that excel doc, clicks a button, it would save that excel doc to another location (i can do this already), and delete the old excel doc from the previous location.

    I'm not sure how I would go about automating the delete process of the old excel doc. I can run a batch file, but a couple of problems arise there;
    1) What if there are more than one excel docs in the folder. The batch file needs to know which one to delete.
    2) How can an excel doc run a batch file if its already closed.

    Not sure if there are answers to these other than it can't be done.

    Any help would be appreciated. Thanks.

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    9

    Re: [2003] Excel+VBA: Save old doc to new location/delete previous doc

    resolved

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [2003] Excel+VBA: Save old doc to new location/delete previous doc

    there are several ways this could be achieved

    one that springs to mind is to put the filename (with path) into an empty cell prior to doing saveas

    vb Code:
    1. Workbooks("jw.xls").Sheets("sheet1").Range("x1") = Workbooks("jw.xls").FullName ' select an empty cell somewhere
    2. Workbooks("jw.xls").SaveAs "jw2.xls"

    vb Code:
    1. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    2. If Not IsEmpty(Sheets("sheet1").Range("x1")) Then
    3.     If Not Dir(Sheets("sheet1").Range("x1")) = "" Then
    4.         Kill Sheets("sheet1").Range("x1")
    5.         Sheets("sheet1").Range("x1") = ""
    6.     End If
    7. End If
    8.  
    9. End Sub

    note this could cause problem if later you wanted to store a filename in the selected cell, also the code will run everytime the selection changes
    you could put the delete code into the workbook open event, but it will not run until the next time the file is opened, but that may not matter,
    there will be other ways to do the same thing
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    9

    Re: [2003] Excel+VBA: Save old doc to new location/delete previous doc

    I already resolved this, here's how I chose to do it:

    1) User clicks button
    2) Current xls doc is saved to a different location
    3) A pop-up comes up indicating to close the current workbook.
    4) Upon confirming the pop-up, a batch file is ran on a 30 second timer.
    5) After 30 seconds, the old xls document is deleted automatically from the batch file.
    6) Issue resolved

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [2003] Excel+VBA: Save old doc to new location/delete previous doc

    when you saveas the original document closes without saving changes, the new (savedas) document is the one currently open, so there should be no need to close the document to delete the original
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width