Results 1 to 6 of 6

Thread: Excel Error on SaveAs/Delete VBA Code

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    18

    Excel Error on SaveAs/Delete VBA Code

    Using the following code:

    Code:
        Sub DeleteProcedureFromModule()
            Dim VBProj As VBIDE.VBProject
            Dim VBComp As VBIDE.VBComponent
            Dim CodeMod As VBIDE.CodeModule
            Dim StartLine As Long
            Dim NumLines As Long
            Dim ProcName As String
            
            Set VBProj = ActiveWorkbook.VBProject
            Set VBComp = VBProj.VBComponents("Module1")
            Set CodeMod = VBComp.CodeModule
        
            ProcName = "DeleteThisProc"
            With CodeMod
                StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
                NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
                .DeleteLines StartLine:=StartLine, Count:=NumLines
            End With
        End Sub
    thisworkbook.saveas filename:=filesavename
    The code seems to work fine as long as the "Trust Access to Visual Basic Project" is checked... however, when I get to the thisworkbook.saveas line, Excel completely errors out and closes, asking me to submit a report to Microsoft. Using VS 2005 I was able to debug and get an error code:

    Unhandled exception at 0x651deb5b in EXCEL.EXE: 0xC0000005: Access violation reading location 0x753c0240.

    Any ideas on what is going on? I am using Excel 2003! Thanks!

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel Error on SaveAs/Delete VBA Code

    This works for me provided

    1) this line

    Code:
    thisworkbook.saveas filename:=filesavename
    is before "End Sub" and not after that.

    2) "filesavename" has been set

    For Example

    Code:
    Dim filesavename as String
    
    filesavename = "C:\MyFile.xls" '<~~ .xlsx for 2007
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    18

    Re: Excel Error on SaveAs/Delete VBA Code

    I am sorry, i must have posted wrong... filesavename is already defined and the code actually looks like this:

    Code:
    Dim filesavename as String
    filesavename = "C:\MyFile.xls" 
    call DeleteProcedureFromModule
    thisworkbook.saveas filename:=filesavename
    where

    Code:
     Sub DeleteProcedureFromModule()
      Dim VBProj As VBIDE.VBProject
      Dim VBComp As VBIDE.VBComponent
      Dim CodeMod As VBIDE.CodeModule
      Dim StartLine As Long
      Dim NumLines As Long
      Dim ProcName As String
      
      Set VBProj = ActiveWorkbook.VBProject
      Set VBComp = VBProj.VBComponents("ThisWorksheet")
      Set CodeMod = VBComp.CodeModule
     
      ProcName = "Workbook_BeforeSave"
      With CodeMod
       StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
       NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
       .DeleteLines StartLine:=StartLine, Count:=NumLines
      End With
     End Sub
    Hope this is better to understand!

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel Error on SaveAs/Delete VBA Code

    Where do you call the save as event? Is it before the DeleteProcedureFromModule() or after that?

    Can u show me the complete code?
    Last edited by Siddharth Rout; Aug 5th, 2010 at 03:28 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    18

    Re: Excel Error on SaveAs/Delete VBA Code

    the code I use is as follows:

    Code:
    Dim filesavename as String
    filesavename = "C:\MyFile.xls" 
    call DeleteProcedureFromModule
    thisworkbook.saveas filename:=filesavename
    however, in the workbook that I am trying to save there is code within "thisworksheet" and its a "BeforeSave" Event... this is the code I am deleting with the DeleteProcedureFromModule code... it works fine and deletes the code... but on the next line 'thisworkbook.saveas filename:=filesavename" is where my error occurs... I hope this clearifies for you

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

    Re: Excel Error on SaveAs/Delete VBA Code

    try a doevents before saveAs

    note before save event will run again when the saveAs is called, try a boolean to prevent code from running twice (or more)
    Last edited by westconn1; Aug 7th, 2010 at 07:46 AM.
    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