• Macro for loading templates and addins

    Author
    Topic
    #355136

    Using Word 2000 on Windows 2000. I’ve created a toolbar button which calls a macro that loads a template so that it will show the template’s toolbar. The button works like a toggle to close the toolbar as well, but I can’t find a way to actually UNload the template (i.e., so that it’s not checked in the templates and addins dialog). This is desirable since this particular template is in conflict with one of our global templates (overriding some of its menu items). Below is my code. I hope one of you gurus out there can help me figure this one out. Thanks very much.

    Public Sub ShowWestCheckButton()
    Dim iAddInCnt As Integer, sAddInName As String
    Dim iCnt As Integer, bToolBarFound As Boolean
    Dim bShowToolBar As Boolean, bcShowToolBar As Boolean
    Dim sSecondaryTemplates$

    On Error GoTo ErrorRoutine
    sSecondaryTemplates$ = Environ(“userprofile”)

    sSecondaryTemplates$ = sSecondaryTemplates$ & “Application DataMicrosoftAddins”
    iCnt = 1
    iAddInCnt = AddIns.Count

    Do While iCnt <= iAddInCnt
    sAddInName = AddIns.Item(iCnt).Name
    If sAddInName = "West Group Westcheck.dot" Then
    bToolBarFound = True
    Exit Do
    Else
    iCnt = iCnt + 1
    End If
    Loop

    If bToolBarFound = True Then
    bShowToolBar = CommandBars("Westcheck").Visible

    AddIns(sSecondaryTemplates$ & "West Group Westcheck.dot").Installed = True
    bcShowToolBar = CommandBars("Westcheck").Visible
    ActiveDocument.UpdateStylesOnOpen = False

    If bShowToolBar = False And bcShowToolBar = True Then
    'Just Position, it's already visible
    CommandBars("Westcheck").Position = msoBarTop
    CommandBars("Westcheck").Left = 100
    CommandBars("Westcheck").Top = 104
    ElseIf bShowToolBar = True And bcShowToolBar = True Then
    CommandBars("Westcheck").Visible = False
    ElseIf bShowToolBar = False And bcShowToolBar = False Then
    CommandBars("Westcheck").Visible = True
    CommandBars("Westcheck").Position = msoBarTop
    CommandBars("Westcheck").Left = 100
    CommandBars("Westcheck").Top = 104
    End If
    Else
    AddIns.Add FileName:=sSecondaryTemplates$ & "West Group Westcheck.dot", Install:=True
    ActiveDocument.UpdateStylesOnOpen = False
    CommandBars("Westcheck").Visible = True
    End If
    Exit Sub
    ErrorRoutine:
    If Err.Number = 5180 Then
    MsgBox "Word cannot open this document template." + Chr(13) + Chr(13) + "The Template being sought may not be" + Chr(13) + "resident in the Word template Directory.", 48, "Word cannot open . . ."
    ElseIf Err.Number = 5 Then
    Err.Clear
    Resume Next
    Else
    MsgBox "Error Number " & Err.Number & " Has Occurred " + Chr(13) + Error
    End If
    End Sub

    Viewing 2 reply threads
    Author
    Replies
    • #523259

      I’ve just recorded a macro of my going into the Templates and Add-Ins dialog and unloading one of the addins that was checked there.
      The resultant code:

      AddIns(“D:WordData97TemplatesMeBmkBrowser.dot”).Installed = False

      Does this help?

      • #523329

        smile Yes, it worked!! I just had to find the right spot to put it. Thank you very much.

      • #524319

        I’m looking for how to go the other way. How do I load an add-in (from a LAN directory)? Schatzie’s code looks to me as if the add-ins are there and that program is looking for a particular one. Would
        AddIns(“L:TemplatesTemplateName.dot”).Installed = True
        add it?

    • #524333

      I actually tried that. In my AutoNew, I coded:
      AddIns(“L:TemplatesProjectWatermark2.dot”).Installed = True

      I also tried running it once the new document was open, in case it just can’t do it as an AutoNew. In both cases, on other computers, I got “Runtime error ‘5941’ The requested member of the collection does not exist”. I copied the template location from the code into the find command and it found the file, so the location is correct and everyone’s drive mapping is the same. It works OK on my own computer, but I think that’s because I manually attached that template once (and can untick it but the delete button is not available to remove it). I need to have it work for other people on the LAN though. Do I have to do something to attach it before I can say installed=true?

      I also tried
      AddIns(“L:TemplatesProjectWatermark2.dot”).Autoload = True
      but that got a compile error: Can’t assign to a read only property.

      • #524347

        If the add-in is not already in the add-ins collection, you use the Add method to add it. wink

        AddIns.Add FileName:=”L:TemplatesProjectWatermark2.dot”, _
        Install:=True

        Now, there must be a way to see if the add-in already has been added but, if not, you could use On Error to handle it:

        On Error Goto addWatermarkDot
        AddIns(“L:TemplatesProjectWatermark2.dot”).Installed = True
        On Error Goto 0
        WatermarkDotInstalled:

        addWatermarkDot:
        AddIns.Add FileName:=”L:TemplatesProjectWatermark2.dot”, _
        Install:=True
        Resume WatermarkDotInstalled

        • #525058

          Jefferson, now I see what you mean about it being a good idea to check if it’s already installed. But On Error won’t do it, because it’s not an error if it’s already installed. It just installs it again, so you have it however many times you’ve installed it. That’s what’s not such a good idea.

          I haven’t figured out how to check if a particular addin is installed. Do I really have to read through all the addins to see if each one matches the one I’m looking for? I don’t even know how to do that. So much for pulling my code out of each template and putting it in a common template. I almost have as much code to just ADD IN the common template as I had IN the common template.

          • #525155

            I think I have the answer to checking if the addin is already there:
            intAddInCount = AddIns.Count
            For X = 1 To intAddInCount
            If AddIns.Item(X) = “TemplatesMacroCode.dot” Then
            booAddInFound = True
            End If
            Next X

            • #525159

              Wendy,

              This method of checking is the way to find out if a member of a collection already exists – when you need to find out whether it exists before you can safely reference it – another example of a use for this is in checking whether a style exists in a document.

              You’ll find that this will run a lot faster though if you use a For Each…Next construct to loop through the Addins collection.

              (without having tested this:)
              Public Function booAddInFound(strName As String) As Boolean
              Dim tAddIn As AddIn
              For Each tAddIn In AddIns
              If tAddIn.Name = strName Then
              booAddInFound = True
              Exit Function
              End If
              Next tAddIn
              End Function

              Gary

      • #524348

        Hi Wendy,

        “Installed = True” is equivalent to already having the add-in showing in the Templates and Add-Ins dialog (but unchecked), and “checking” the checkmark.

        Before Installing it, you need to add it to the AddIns collection (that’s why you’re getting the error). You can do it this way:

        AddIns.Add (“L:TemplatesProjectWatermark2.dot”)

        But VBA helps demos a method to Add and Load it in one swoop:

        AddIns.Add FileName:=”C:Program FilesMicrosoft Office” _
        & “TemplatesLetters & FaxesMyFax.dot”, Install:=True

        Gary

      • #524411

        I got the same error message (“The requested member of the collection does not exist”) when trying to run the macro under a different userprofile on the same Windows 2000 PC. The problem turned out to be one of permissions. The clue was that the macro worked for some profiles but not for others even though the file was in a common location (c:Program Files path).

        Permissions are very complicated and I don’t completely understand them. Apparently, the rights were broken for some users, even though the security dialog seemed to indicate that the “everyone” group should have had “read and execute” and “read” rights to the target template. There is an additional issue of “inheritable permissions from parent” which further complicates things. I had to ask one of my IT colleagues to straighten this out; but you may want to check into this possibility.

    • #524622

      YES, yes, yes. Addin.Add worked on my machine and it worked on another machine that had never seen that addin before. And it worked when I already had the addin loaded and installed, Jefferson, so the coding to handle it when it’s already been loaded doesn’t seem to be required.

      So that means I can move my common routines to a separate template and just put the Add code in the individual templates. Now to see whether all that’s required is to have the Add code in the template on which the other templates are based.

    Viewing 2 reply threads
    Reply To: Macro for loading templates and addins

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: