+ Reply to Thread
Results 1 to 5 of 5

Deleting or renaming an active file

Hybrid View

dflak Deleting or renaming an... 03-16-2017, 02:54 PM
dflak Re: Deleting or renaming an... 03-16-2017, 03:25 PM
k64 Re: Deleting or renaming an... 03-16-2017, 03:29 PM
dflak Re: Deleting or renaming an... 03-16-2017, 03:44 PM
k64 Re: Deleting or renaming an... 03-16-2017, 03:46 PM
  1. #1
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Deleting or renaming an active file

    I'm am looking for a way to make a file commit suicide.

    I have a requirement to process a file.

    When certain cells are filled in, the file is said to be complete. What the user wants to do is rename the file and put it in another directory and remove the original file.

    Copying the file to the directory with the new name is not an issue. The problem is getting rid of the file that's still running.

    I'm fairly certain that this is impossible without invoking another workbook that saves the file that calls it, closes that file and then moves it.

    Here is the appropriate part of the code. SavePath and SaveFile are defined.
    If DeleteFlag = True Then
        ThisWorkbook.Save
        Savefile = Replace(ThisWorkbook.Name, ".xlsb", "")
        Savefile = Savefile & " Complete.xlsb"
        ' Get rid of the copy to file if it exists.
        If Dir(SavePath & "\" & Savefile) <> "" Then
            Kill SavePath & "\" & Savefile
        End If
        FileCopy ThisWorkbook.Path & "\" & ThisWorkbook.Name, SavePath & "\" & Savefile
        Kill ThisWorkbook.Path & "\" & ThisWorkbook.Name
    End I
    The code doesn't like the Kill statement.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Deleting or renaming an active file

    I added the word "suicide" to describe what I needed the workbook to do. Apparently that was the magic word for Google. I found this and it works.

    With ThisWorkbook
    .Saved = True
    .ChangeFileAccess xlReadOnly
    Kill .FullName
    .Close False
    End With
    This wipes out the workbook completely. It bypasses the Recycle Bin. I have a lot of copies of my test file.

  3. #3
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Deleting or renaming an active file

    Well, that is simpler than what I came up with. Here is what I got though in case it fits your needs better. First run "addref" to add the required reference.

    Sub addref()
    On Error Resume Next
    ThisWorkbook.VBProject.References.AddFromGuid GUID:="{0002E157-0000-0000-C000-000000000046}", Major:=5, Minor:=3
    On Error GoTo 0
    End Sub
    
    Sub deleteme()
    Path = ThisWorkbook.FullName
    With Workbooks.Add
        With .VBProject.VBComponents.Add(vbext_ct_StdModule)
            .CodeModule.AddFromString "Sub deleteit()" & vbNewLine & _
                "Kill """ & Path & """" & vbNewLine & _
                "Thisworkbook.close false" & vbNewLine & _
                "End sub"
        End With
        Application.OnTime Now + 2 / 86400, .FullName & "!deleteit"
        ThisWorkbook.Close False
        .Close False
    End With
    End Sub
    1. Include a sample workbook with an example of the result you want
    2. Use [CODE] and [FORMULA] wrappers for your VBA code or excel formulas
    3. If your question has been answered, mark the thread as SOLVED and click on the "Add Rep" star to thank whoever helped you.

  4. #4
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Deleting or renaming an active file

    You are using VB code to write VB Code? I could really get dangerous with this. (Insert maniacal laugh here).

    I'll stick with what I have, but I am keeping this one for a future event.

  5. #5
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Deleting or renaming an active file

    Yes. Because of the possible danger, you have to have "Trust access to the VBA project object module" checked in Macro Security. I have put some "surprises" in workbooks that I have coded for my brother using this technique

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Deleting or Renaming Lists
    By Tail Wind in forum Excel General
    Replies: 0
    Last Post: 02-04-2006, 04:25 PM
  2. Re: Deleting or Renaming Lists
    By Max in forum Excel General
    Replies: 2
    Last Post: 02-04-2006, 04:10 PM
  3. Renaming and Deleting Lists
    By Tail Wind in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-04-2006, 01:10 PM
  4. [SOLVED] renaming copied worksheets deleting .xls]
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-06-2005, 03:05 PM
  5. [SOLVED] renaming copied worksheets deleting .xls]
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2005, 06:05 AM
  6. [SOLVED] renaming copied worksheets deleting .xls]
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 04:05 AM
  7. renaming copied worksheets deleting .xls]
    By dhl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  8. Sending active worksheet and renaming
    By Qaspec in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-23-2005, 06:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1