Excel crashing without error when form.Controls.Add is called

0

Excel 2016 version 1811 (build 11029.20079 Click-to-Run) is crashing without error when the following code is called from a user's interaction with a UserForm. Thing is, it only crashes in THIS (main) UserForm. If I add the container to any other UserForm it executes without any issue.

Private container As Object

'***************************************************************************
'Purpose: Create the container frame that holds the page title and preview image
'***************************************************************************
Private Sub createContainer()
    Set container = main.pageThumbnails.Controls.add("Forms.Frame.1", name) ' Break here, continuing causes *CRASH*

    With container
        .Caption = ""

        .height = containerHeight
        .width = containerWidth
        .Left = 12
        .top = areaHeight

        .BackColor = defaultBackColor
        .BorderColor = defaultBorderColor
    End With

    With main.pageThumbnails
        .ScrollHeight = areaHeight
    End With

End Sub

Checking Windows Event Viewer gives me the following:

Faulting application name: EXCEL.EXE, version: 16.0.11029.20079, time stamp: 0x5bf4c8c3
Faulting module name: coml2.dll, version: 10.0.17134.407, time stamp: 0xaad4943a
Exception code: 0xc0000005
Fault offset: 0x0000000000016521
Faulting process id: 0x100788
Faulting application start time: 0x01d48cb0460efe6f
Faulting application path: C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE
Faulting module path: C:\WINDOWS\System32\coml2.dll
Report Id: 3ff6565e-7c37-4733-87fd-7e5ab12cba1f
Faulting package full name: 
Faulting package-relative application ID: 

Things I've tried:

  1. Running in compatibility mode (Windows 7 and 8)
  2. Moving the file from a network location to my Downloads folder on the local machine
  3. Casting container as an MSForms.frame instead of an Object
  4. Restarting the computer and Excel
  5. Running Excel in safe mode: excel.exe /s
  6. Changing container from Private to Public and referencing Me.container
  7. Tried each (and every) method provided by Microsoft here
  8. Installed the KB4011128 update from Microsoft. It detected an issue and "fixed it" but the problem remains
  9. Redditors suggested that uninstalling update 4018319 could fix the issue but I don't have it installed.
  10. Referencing the main form using a singleton module instead of main.thumbnails
  11. Creating the frame on the main form code-behind
  12. Referencing the created frame from a singleton module
  13. Created a new .xlsm file, copied over all class, modules, and forms (except main). Created main from scratch
  14. Added a newly created CommandButton to the frame (this worked...)
  15. Changed the Id of thumbs frame to a bunch of random letters
excel
vba
asked on Stack Overflow Dec 5, 2018 by kneeki • edited Feb 15, 2020 by braX

2 Answers

0

Throughout my searching I found other people with the same crash and error logs in Windows Event Viewer. None of those posts were using VBA as I was. I solved the issue by deleting the referenced Control main.pageThumbnails and recreating it in the editor.

At this time I have no idea what caused the issue nor if this solution is permanent.


Update 30 Jan 2019

After continuing to struggle with this issue and not coming up with a final solution or even an explanation as to why it's happening I created a workaround.

Instead of adding a MSForms.Frame object to the container I instead added the frame contents (an image and four labels) to the container and control their positioning via a Sub setTop method.

At this moment I'm unsure of the performance impact but at least I'm able to continue working on additional functionality.

answered on Stack Overflow Dec 6, 2018 by kneeki • edited Jun 20, 2020 by Community
0

Is pageThumbnails also a frame? And do you have other containers in your userform? If so, this is an Excel bug.

This is an old question but I've just spent hours trying to find a solution so here are my findings.

First, to replicate the issue:

  • Create a new UserForm (UserForm1)
  • Add a Frame (FrameA)
  • Add this code:
Private Sub UserForm_Activate()
    Set b = FrameA.Controls.Add("Forms.Frame.1", "FrameB")
End Sub
  • Add a module (Module1) with:
Sub showUserForm1()
    UserForm1.Show
End Sub
  • Run showUserForm1

Everything works fine. Now add a second Frame (FrameC) along with this code in UserForm_Activate(): Set d = FrameC.Controls.Add("Forms.Frame.1", "FrameD"). Run showUserForm1 again: Excel crashes. More precisely, it crashes on the first line (if you skip it, the second line works fine).

After further testings, it appears that it fails only if the frame you are trying to add another frame in is at the back. If you bring successively FrameA or Frame C at the front you will notice that it fails always on the Frame wich is at the back.

So I can think of only 3 possibilities:

  • don't use frames and place your controls directly in the UserForm as the OP suggested,
  • make sure your frame is at the top in design mode (using Zorder does not work),
  • if you can't have your frame at the top (e.g. multiple frames), try by creating them all at runtime (FrameA and FrameB within, then FrameC and FrameD within etc).
answered on Stack Overflow Dec 16, 2020 by HiPierr0t

User contributions licensed under CC BY-SA 3.0