0

This is my first question I ask in this community, maybe you know the answer. I want a Word macro application “MyToolbox.docm” providing some functionality for the users using a dialog box. The following requirements apply:

• The Toolbox functionality must be available in all documents to the user • Toolbox functionality must be available as long as the file MyToolbox stays open • Dialog is started using a button • Use of separate installation file is not allowed • Data storage in normal.dot is not allowed

I have solved this problem with the following functionalities:

• The public Macro “Sub MyToolbox” starts the dialog • This macro is added to the Quick Access Toolbar with a nice Icon. • Icon is only visible when file “MyToolbox.docm” is open

Starting and using MyToolbox:

• After opening file “MyToolbox.docm” Autoopen macro starts • it loads “MyToolbox.docm” as AddIn • Loading as AddIn makes the functionality accessible to other open documents

Finishing the use of MyToolbox:

• Closing Word or the file MyToolbox runs an Autoclose macro • This macro unloads and deleted the AddIn

In Principle this works fine, after closing MyToolbox the AddIn disappears from the list of AddIns. However in the overview (File-Options-AddIns) the Addin is still listed as inactive!

After closing Word and Reopening Word the Addin is automatically loaded and the file is active again.

This happens everytime I restart Word.

Now I have programmed a Workaround in the Autoopen Macro which does the following:

• Wait 5 seconds (in case it does not work, increase the wait time to 10 sec) • Check if file MyToolbox.docm is open • In case it is not open (meaning the user did not want to start the Toolbox) the AddIn is removed again

After the implementation of this workaround the Restart of the AddIn happens only one time when I open Word. After closing Word and reopening a second time the AddIn has disappeared form the list of inactive Addins and word again is clean.

I do not know where Word stores the inactive AddIns, I also looked in the Registry. Can you help here?

To reproduce the effect you need a Macrofile with an empty Userform1 and a Module containing the following code:

Sub AutoOpen()
Dim MyToolbox_Fullname As String

'Get full document name
    MyToolbox_Fullname = ActiveDocument.Fullname

'Load program as AddIn
    AddIns.Add Filename:=MyToolbox_Fullname, Install:=True
    ActiveDocument.UpdateStylesOnOpen = False
    

'Workaround
    Call time_delay(5)

    If file_status(MyToolbox_Fullname) = 1 Then
        
        'File exists but is not open, unload AddIn
        AddIns(MyToolbox_Fullname).Installed = False
        AddIns(MyToolbox_Fullname).Delete

    End If
    
End Sub


Sub Autoclose()
Dim MyToolbox_Fullname As String, MyToolbox_File As String

'Get document name
    MyToolbox_Fullname = ActiveDocument.Fullname
    MyToolbox_File = ActiveDocument.name

'Unload AddIn
    If CheckAddin(MyToolbox_File) Then
        
        AddIns(MyToolbox_Fullname).Installed = False
        AddIns(MyToolbox_Fullname).Delete
    
    End If
    
'Unload form
    Unload UserForm1
    
End Sub


Public Sub MyToolbox()

'Start menu
    UserForm1.Show

End Sub



Function CheckAddin(AddIn_Name As String) As Boolean
Dim oAddin As AddIn
Dim AddIn_Exists As Boolean

    'Check if AddInn exists
    AddIn_Exists = False
    
    For Each oAddin In AddIns
        If oAddin.name = AddIn_Name Then
            AddIn_Exists = True
            Exit For
        End If
    Next
    
    CheckAddin = AddIn_Exists

End Function


Public Sub MyToolbox()

'Start menu
    UserForm1.Show

End Sub

  
Private Function GetFilenameFromPath(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function


Private Function file_status(Fullname As String) As Integer
'Check if file exists:
'file_status = 0  does not exist
'file_status = 1  does exist on file
'file_status = 2  does exist and is open
    file_status = 0
    If file_exists(Fullname) Then
            file_status = 1
    End If
    For Each aDoc In Documents
        If UCase(aDoc.name) = UCase(GetFilenameFromPath(Fullname)) Then
            file_status = 2
        End If
    Next aDoc
End Function


Private Function file_exists(Fullname As String) As Boolean
Set fs = CreateObject("Scripting.FileSystemObject")
    file_exists = fs.fileexists(Fullname)
End Function


Private Sub time_delay(Length As Integer)
    Start = Timer
    Do While Timer < Start + Length
        DoEvents
    Loop
End Sub

Markus
  • 1
  • It appears that you're trying to make the add-in delete itself. An open file can't do that. – John Korchok Nov 18 '22 at 18:54
  • Thank you for your input, but I do not think that is the real cause. Deleting the AddIn does not physically delete the file. There are 2 commands available: Inactivating an AddIn, this means it is not marked as active, and deletion of an AddIn which removes it from the list of word AddIns, but the file itself is not deleted! – Markus Nov 20 '22 at 09:47
  • You've revised your question and it is not clear what problem you're having. Is it that you have to start Word a second time to complete deletion of the add-in? – John Korchok Nov 23 '22 at 19:33
  • Yes, this is true. With my workaround I do have to delete the AddIn a second time to remove it completely. Basically I do not understand why and at which place Word stores information on AddIns which I have set inactive and deleted. – Markus Nov 27 '22 at 09:43

0 Answers0