Closing addin through VBA causes code to stop

M

Matthew Pfluger

I am trying to write a "Setup" workbook to install or uninstall an addin.
The uninstall code looks like this (simplified):

Function bUninstallAddin(ByVal sAddinName As String, _
ByVal sAddinSourceFilepath As String) As Boolean

' Attempt to close addin in case it is open
On Error Resume Next
' ******************************************
' Code stops after executing the line below
AddIns(sAddinName).Installed = False
On Error GoTo ErrorHandler

' Delete pre-existing registry keys (if any)
If Not bDeleteAddinRegistryKey(sAddinName) Then Err.Raise glHANDLED_ERROR

bUninstallAddin = TRUE

End Function

When the function above closes the target addin, the addin runs an
Auto_Close script to remove commandbars, save settings, and shut down.
However, that code causes code execution to stop, and I want the uninstall
macro to continue.

Here's the Auto_Close routine from the target addin:

Public Sub ShutdownApplication()

On Error Resume Next

Application.ScreenUpdating = False

' This flag prevents this routine from being called a second time
' by Auto_Close if has already been called by another procedure.
gbShutdownInProgress = True

' Do a bunch of things here....

' Continue if not in debug mode.
' ******************************************
' Code stops after executing the line below
If Not gbDEBUG_MODE Then ThisWorkbook.Close False

End Sub

Is there any way to run the shutdown script without terminating code
execution?

Thanks.
Matthew Pfluger
 
P

Peter T

Maybe there's more code that you haven't posted that causing things to halt.
But why this line in the close event
If Not gbDEBUG_MODE Then ThisWorkbook.Close False

Why force the wb to close in the close event when it's going to close
anyway.



' Attempt to close addin in case it is open
On Error Resume Next
' ******************************************
' Code stops after executing the line below
AddIns(sAddinName).Installed = False

As well from changing the Installed status this closes the addin. You'ld
only need the error handler if sAddinName ddoes not exist in the addins
collection, irrespective as to whether it's installed or loaded. Try
commenting the error handler and stepping through.

Regards,
Peter T
 
M

Matthew Pfluger

Peter T,

You were correct in pointing this out as the command that causes the code
execution to stop. Let me explain:

I need the "On Error Resume Next" when attempting to close the addin because
the Uninstall code could be called when the addin is already uninstalled.
This prevents an unnecessary error.

I originally included the line:
If Not gbDEBUG_MODE Then ThisWorkbook.Close False
because I didn't want end users to overwrite changes to the addin they were
using. However, I made some other changes to how I distribute the addin (as
well as set the network copy of the addin to READ ONLY), so this code is no
longer necessary.

Lesson learned:
<Workbook>.Close SaveChanges:=False
cause code execution to stop. Thanks again, Peter T, for the pointer.

Matthew Pfluger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top