I'm managing Word templates for my company and have started using VBA for a few very simple things. I havn't worked with VBA before this. The Word version is 2016 (Word info says Word 2016 (16.0.5408.1001)
To break down on the issue I have created a template, where there is only one ActiveX CommandButton. In VBA code there are only 2 Subs (directly written in "ThisDocument"):
Private Sub Document_Open()
'there is no code here yet'
End Sub
Private Sub CommandButton1_Click()
ActiveDocument.Shapes("Control 2").Delete
End Sub
When I click the button it gets properly deleted. When I save the Word document and reopen it afterwards, Word crashes. The Windows eventviewer gives me the errorcode C0000005 which translates to a memory error as far as I found out.
I think Word crashes as soon as another Sub is called and there is no CommandButton around anymore. When I comment out the "Document_Open" sub and create a sub "Document_ContentControlOnExit" for a dropdown, the doc would open but crash as soon as the dropdown is exited.
I tried several things to find out, how I could fix this. The two "dirty" soultions I came up with are:
- Just move the button out of view and don't delete it. The problem there is, that the user sometimes deletes the whole paragraph where the button is "anchored" to, deleting the button in the process, which then causes the crash problem when reopening Word. I could try moving the anchor at the same time when moving the button out of view.
- Creating a second "dummy" CommandButton and hiding it somewhere, where it's harder to delete (at the moment I have a hidden button in the header of the document, pushed out of view) also fixes the problem
My question is: Is there a simpler not so dirty solution that I'm not seeing?
Sadly I couldn't find out, what causes this issue. It seems that Word needs a CommandButton to not crash and it's not relevant where the second "dummy" button is or how it's named.
Thanks for the help