How to close an xlam

jcarlosd

New Member
Joined
Oct 20, 2002
Messages
40
I have a mymacros.xlam file that opens automatically when called from file mybook.xlsx, with a ribbon and functions for that specific workbook.
From another VBA code, I close mybook.xlsx, and I want to close also mymacros.xlam.

I read on this thread that it is as simple as running
VBA Code:
Workbooks("mymacros.xlam").Close SaveChanges:=False

However, it does not (always) work, because mymacros.xlam is not in the Workbooks collection. Sometimes it correctly close the macro file, sometimes it closes my current workbook, sometimes it crashes.

I tried also to write a macro in mymacros.xlam that closes itself, but again, sometimes it works, sometimes it does not.

I am very confused about this behavior. I googled for a way to close and xlam file but I did not find a solution.

Does anybody have experience with this practice?

J. Carlos
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
There are different ways to load an AddIn into Excel's memory space, so there are also different ways to unload an AddIn.
The code below should fix your issue.

VBA Code:
Sub UnLoadAddin()

    Const cAddIn As String = "AddInName.xlam"          ' <<< change file name to suit

    Dim oAI As Object

    For Each oAI In Application.AddIns
        If StrComp(cAddIn, oAI.Name, vbTextCompare) = 0 Then
            oAI.Installed = False
            Exit Sub
        End If
    Next oAI
    For Each oAI In Application.UsedObjects
        If TypeOf oAI Is Workbook Then
            If StrComp(cAddIn, oAI.Name, vbTextCompare) = 0 Then
                oAI.Saved = True
                oAI.Close
                Exit For
            End If
        End If
    Next oAI
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,798
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub DisableAddin()
  AddIns("mymacros").Installed = False
End Sub
 
Solution

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
@Fluff, whenever a loaded xlam is not located in the default AddIn folder (C:\Users\GWteB\AppData\Roaming\Microsoft\AddIns) or not loaded using Excel's UI (Developer tab > AddIns > browse button) your suggestion will fail.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,798
Office Version
  1. 365
Platform
  1. Windows
Wasn't aware of that, thanks.
I've always kept add-ins in the default folder.
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
I also do that at home, at the office that is not possible in my situation. Also not necessary, especially if several users (without knowing it themselves) use the same AddIn.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,295
Messages
5,595,289
Members
413,984
Latest member
stikpet

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top