Results 1 to 1 of 1

Workbooks_Open crashes when File opened with code. Manually Open OK. UserForm Issue?

Threaded View

  1. #1
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Workbooks_Open crashes when File opened with code. Manually Open OK. UserForm Issue?

    Workbooks_Open crashes when File opened with code. Manually Open OK. UserForm Issue?


    Hi
    This is ( was ) driving me crazy.
    I have ( had ) a large Workboook.
    It has a Workbook_Open code in it.
    If I open this file manually I have no problem.

    If I use a certain code to open this then Excel crashed right at the end of a Workbook_Open code.

    I busted my balls for a day stripping the code down until I located what causes this. I also stripped down the File I give here to the minimum in order to demo the problem here and aid in anyone trying to help.

    The problem is somehow coming from the UserForms I have. If I remove these , or the code bit referencing them, then the problem is gone.

    I need to use a global variable for one of my user form instances in the original code. I suspect this may somehow come into the equation of the problem as well

    I am guessing that there is some form of initialisation or clearing which goes on when I close and open manually the code. May be for some strange reason this is not occurring when I close and open the File with a code. So maybe I am missing some code line to do before or after I close the File in the code before the code later re- opens it again. ( For various reasons I do need to open and close my original code. That is not negotiable! – I have just here in the demo codes I give tried to reduce everything to the minimum so as to not confuse the issue )

    Some Observations:
    _ The code will sometimes work the first time without crashing Excel

    _ The problem also appears to go away if I remove UserForm1, which in the demo code is not used at all? So clearly there is some strange initialisation or mixing up of existences of the Userforms which is Chucking a spanner in the works.

    Here the stripped down Workbook_Open code which gives the problem. It is in the File “ProAktuellex8600x2WSOpenCrash.xlsm” which I have uploaded. This file also includes the offending UserForms.
    Option Explicit
    Private Sub Workbook_Open()
    On Error GoTo Err
            If fm Is Nothing Then Set fm = New ufResults ' If this line was not yet passed, ( or the UserForm had been manually closed ), then a new instance of the Class ufResults with the name fm is made
            If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,
        fm.OptionButton2.Value = True
        fm.Refresh.Value = True
    If Application.Wait(Now + TimeValue("0:00:2")) Then
     MsgBox "Here comes the crash if you are opening the File with a code"
    Else
    End If
    Exit Sub
    
    Err: ' Just in case   ;)
    MsgBox Prompt:=Err.Description
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub
    _....................................__...


    Here an example of a code that would cause the problem. The crash actually occurs at the end of the Worksheet_Open code which is initiated in the last line of the below code at the point at which the code below opens the offending File. ( Line 120 ) . The code is also in the uploaded File “NeuProAktuelleMakrosWBOpenCrash.xlsm” There is nothing but that simple code in that File.
    I have written the demo codes so that they are stored and accessed from my desktop. So will need to alter that ( Lines 60, 70 and 120 ) to match where you save the files, should you download them.
    Option Explicit
    Sub SaveProDesktop()
    10   WB As Workbook: Set WB = Workbooks("ProAktuellex8600x2WSOpenCrash.xlsm") ' This will typically be open at the start of this code
    20  Dim WBFullName As String: Let WBFullName = WB.Name       'Determine it now as later when WB is closed it will not appear in Workbooks Collection
    30
    40 'save File and close
    50  Application.DisplayAlerts = False 'Stop being asked stuff on saving                                                                     ' 'ChDir "C:\Users\Elston\Desktop
    60  WB.SaveAs FileName:="C:\Users\Elston\Desktop\" & WBFullName & ""                                            'WB.Save        ' 'Quick in case I added and forgot to save in original Type!!  - I did a lot often
    70  WB.SaveAs FileName:="C:\Users\Elston\Desktop\Temp" & Format(Now(), "mmss") & ".xlsx", FileFormat:=xlOpenXMLWorkbook           ' File copy in .xlsx Form
    80  Application.DisplayAlerts = True 'Switch it back on
    90  WB.Close True 'Close Saved File
    100
    110 'Open file                                                                                                                                        'ChDir "C:\Users\Elston\Desktop"
    120 WB.Open FileName:="C:\Users\Elston\Desktop\" & WBFullName & ""
    End Sub
    _...........___...

    ( I get this problem on 2 different computers, one is using XL 2007, the other uses XL 2010 )

    Thanks for any help.
    Alan.

    _.........._______________________--

    P.s. Here a screen shot od the relavent things:
    Attachment 461303



    _...__

    P.p.s. Just out of interest the main code, but it is a bit big. – The demo codes replicate the original Problem.
    https://app.box.com/s/fpztob9pcp92fl6hh81zgpzumw9ntcp0

    _..._____________________

    Rem Ref http://www.excelforum.com/excel-prog...n-on-open.html
    Rem Ref http://www.excelforum.com/excel-prog...on-open-2.html
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 02-21-2016, 03:23 PM
  2. Opening Excel File Crashes. Last opened in 2 windows. 2013
    By bowbalitic in forum Excel General
    Replies: 0
    Last Post: 10-03-2014, 12:08 AM
  3. Excel 2010 crashes after 2nd file is opened as read only and then closed
    By tv69 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-22-2014, 03:53 PM
  4. can't open a new excel file while userform is opened
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-16-2013, 02:38 PM
  5. Sheet with userform crashes on load. Possible issue with VBE7.dll
    By dude2007 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-31-2012, 08:32 PM
  6. Replies: 0
    Last Post: 06-30-2011, 09:53 AM
  7. .csv file. Different when opened manually and via VBA
    By Richard Buttrey in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-22-2009, 10:02 AM

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