Results 1 to 11 of 11

Thread: kill a excel file from the file itself (resolved)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    kill a excel file from the file itself (resolved)

    is it possible to kill a excel file upon a click button on the excel form itself?
    Last edited by Goh; Apr 28th, 2004 at 09:50 PM.

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Location
    Luton, UK
    Posts
    178
    Yes. You have to assign a macro to the button.
    The code statement is Kill.
    Regards
    BrianB
    -------------------------------

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908
    i cant kill the file from the file itself even with marcos...it says access denied...

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If you are trying to delete the Excel file itself then the answer is
    NO, not from within the same workbook.

    You would need to have your macro create another workbook
    with code in it to kill the original workbook.
    Add this code to a workbook called "KillMe.xls".
    It will save itself as Main.xls and then it deletes the KillMe.xls workbook.
    VB Code:
    1. Private Sub Workbook_Open()
    2.     ActiveWorkbook.SaveAs "Main.xls"
    3.     Kill Mid$(ActiveWorkbook.FullName, 1, InStrRev(ActiveWorkbook.FullName, "\")) & "KillMe.xls"
    4. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908
    how abt the Main.xls? it still exist rite? even if you close it...

  6. #6
    New Member
    Join Date
    Apr 2004
    Posts
    12
    This routine will create a new workbook that never gets saved. The orginal file will be deleted once the new workbook is closed. In order for this to run, a reference needs to be set to "Microsoft Visual Basic Extensibility 6.0" (with Excel 2000, should be similar, if not the same, for other versions).

    Be careful testing this, so that you don't delete a file you are not ready to lose.


    HTH


    VB Code:
    1. Sub KillMe()
    2.     Dim tempWB As Workbook
    3.    
    4.     Set tempWB = Workbooks.Add
    5.    
    6.     With tempWB.VBProject.VBComponents("ThisWorkbook").CodeModule
    7.         .InsertLines .CountOfLines + 1, _
    8.             "Private Sub Workbook_BeforeSave(" & _
    9.             "ByVal SaveAsUI As Boolean, Cancel As Boolean)" & vbCrLf & _
    10.                 "Cancel=True" & vbCrLf & _
    11.             "End Sub" & vbCrLf & vbCrLf & _
    12.             "Private Sub Workbook_BeforeClose(Cancel As Boolean)" & vbCrLf & _
    13.                 "ThisWorkbook.Saved=True" & vbCrLf & _
    14.                 "Kill """ & ThisWorkbook.FullName & """" & vbCrLf & _
    15.             "End Sub"
    16.     End With
    17.    
    18.     Set tempWB = Nothing
    19.  
    20.     ThisWorkbook.Close SaveChanges:=False
    21. End Sub

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Yes, the Main.xls workbook will stilll be there.

    medison, I get an application error 1004 on the with line of code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908
    so this code cant work?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    medison, I figure out part of the issue. The checkbox for trusting
    access to VB Projects needs to be checked in the tools > macros
    > security > Trusted sources tab.

    After that, I am now getting to the line that adds the code to the
    other vba project and then it crashes Excel.



    Attached Images Attached Images  
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    New Member
    Join Date
    Apr 2004
    Posts
    12
    How are you running the code? Are you trying to step through it? If so, try running the code from Excel instead of from the VB Editor.

    The code works fine on my computer (WinXP) with both Excel 97 & 2000 when I run it from Excel (Tools - Macro - Macros. . .).

    I have had trouble in the past when trying to step through code like this. I had never looked into the causes though. So, I did a little searching and learned a few things:

    The reference to "Microsoft Visual Basic Extensibility 6.0" is only necessary when declaring variables of type VBProject, VBComponent, etc. Since the example does not do this, the reference can be removed.

    The best information that I found, Chip Pearson's site, recommends setting the reference to "Microsoft Visual Basic For Applications Extensibility" instead.

    Chip's site also mentions putting a check next to "Trust Access To Visual Basic Project" for newer versions of Excel.

    About half-way down the page, in the section about adding a procedure, it mentions using "Application.Run" to call procedures that add code to different modules. I would guess that this is done to avoid similar problems.

    The CodeModule object has a CreateEventProc method which could be used to shorten the code a little.

    Try running the example from Excel and using the Application.Run method. If that does not work, I guess we'll keep looking.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Originally posted by RobDog888
    The checkbox for trusting access to VB Projects needs to be
    checked in the tools > macros > security > Trusted sources tab.
    Yes, I was stepping through the code - Excel's VBA Editor.
    I will try running from Tools > macros. etc.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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