Auto Install Excel Addin - Error 1004 - Application Defined or Object-Defined Error

Hi Experts,

The code below was working when I was still on Windows XP but ever since changing over to Windows 7 it's not working anymore.

When it reaches line 77, I'm getting Error 1004 - Application-defined or object-defined error.

Any ideas?

Private Sub Workbook_Open()
    Dim ai As Excel.AddIn, NewAi As Object
    Dim M As String
    Dim Ans As Integer
    Dim sUser As String
    Dim sComputer As String
    Dim lpBuff As String * 1024
    Dim oXl As Object
    Dim sAddinFullName As String
    Dim sUDT As String
    Dim DirTest As Variant
    Dim strDir As String
     
    On Error GoTo ErrorHandler
    
    'Was just installed using the Add-Ins dialog box?
    If InstalledProperly Then Exit Sub
    
    'If in Debug mdoe then just exit
    If gbDEBUG_MODE Then Exit Sub
    
    
    sUDT = GetApplicationConfigSetting("UDT_Name")
    
   'Is it in the AddIns collection?
   For Each ai In AddIns
      If ai.Name = ThisWorkbook.Name Then
         If ai.Installed Then
                Exit Sub
             Exit Sub
         End If
      End If
   Next ai


    'Get the Login User Name
    GetUserName lpBuff, Len(lpBuff)
    sUser = Left$(lpBuff, (InStr(1, lpBuff, vbNullChar)) - 1)
    lpBuff = ""
      
    'It's not in AddIns collection, prompt user.
    M = "You just opened an add-in. Do you want to install it?"
    M = M & vbNewLine
    M = M & vbNewLine & "Yes - Install the add-in. "
    M = M & vbNewLine & "No - Open it, but don't install it."
    M = M & vbNewLine & "Cancel - Close the add-in"
    Ans = MsgBox(M, vbQuestion + vbYesNoCancel, ThisWorkbook.Name)
    Select Case Ans
        Case vbYes
        
            Application.DisplayAlerts = False
            
               'Is it in the AddIns collection?
                For Each ai In AddIns
                   If Left(ai.Name, Len(sUDT)) = sUDT Then
                      ai.Installed = False
                   End If
                Next ai
                
                Set oXl = CreateObject("Excel.Application")
                oXl.Workbooks.Add
                
                'Test if computer is XP
                strDir = "C:\Documents and Settings\" & sUser & "\Application Data\Microsoft\AddIns\"
                DirTest = Dir$(strDir, vbDirectory)
                
                'iSpace if DirTest return vbnullstring
                'Change directory to iSpace format.
                If DirTest = "" Then
                    strDir = "C:\Users\" & sUser & "\AppData\Roaming\Microsoft\AddIns\"
                End If
                
                'Get the addin full name
                sAddinFullName = strDir & sUDT & ".xlam"
                
                'Install Addin
                Set NewAi = oXl.AddIns.Add(sAddinFullName, True)
                NewAi.Installed = True
                
                MsgBox "Installation Successful!", vbInformation

                oXl.Close False
                Set oXl = Nothing
                
            Application.DisplayAlerts = True
            
        Case vbNo
            'no action, leave it open
        Case vbCancel
            ThisWorkbook.Close
    End Select


ProcedureExit:

    Exit Sub

ErrorHandler:
    If bCentralErrorHandler(msMODULE, sSource, Erl, , True) Then
        Stop
        Resume
    Else
        Resume ProcedureExit
    End If

End Sub

Open in new window


Many thanks in advance
Ricky
Ronniel Allan CastanitoIT ManagerAsked:
Who is Participating?
 
Ronniel Allan CastanitoConnect With a Mentor IT ManagerAuthor Commented:
I resolved this by unprotecting the add-in's workbook before the installation process.
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi Ricky,

Do either of the explicit paths used exist on the Windows 7-based machine?

C:\Documents and Settings\{UserName}\Application Data\Microsoft\AddIns\
&/or
C:\Users\{UserName}\AppData\Roaming\Microsoft\AddIns\


Is the required Add-in file present in either location?

Also, is the "UDT_Name" configuration setting (still) present on that machine?

BFN,

fp.
0
 
Ronniel Allan CastanitoIT ManagerAuthor Commented:
Hi fp,

Thanks for your reply. To answer your questions, I tested the directory and it works fine. The UDT_Name is saved on the a AppSettings worksheet of the addin.

I was able to get the following codes to work, but when I exit and restart excel it gives me an error saying:  

...."Excel cannot open the file because the file format or the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."...

Why is this the case when the format is in .XLAM and worksheet property ISADDIN set to true?


'Private Sub Workbook_Open()
    Dim ai As Excel.AddIn, NewAi As Excel.AddIn
    Dim M As String
    Dim Ans As Integer
    Dim sUser As String
    Dim sComputer As String
    Dim lpBuff As String * 1024
    Dim oXl As Object
    Dim sAddinFullName As String
    Dim sUDT As String
    Dim DirTest As Variant
    Dim strDir As String
    Dim wb As Workbook
     
    Stop
    
    'Was just installed using the Add-Ins dialog box?
    If InstalledProperly Then Exit Sub
    
    'If in Debug mdoe then just exit
    If gbDEBUG_MODE Then Exit Sub
    
    
    sUDT = GetApplicationConfigSetting("UDT_Name")
    
   'Is it in the AddIns collection?
   For Each ai In AddIns
      If ai.Name = ThisWorkbook.Name Then
         If ai.Installed Then
                Exit Sub
             Exit Sub
         End If
      End If
   Next ai


    'Get the Login User Name
    GetUserName lpBuff, Len(lpBuff)
    sUser = Left$(lpBuff, (InStr(1, lpBuff, vbNullChar)) - 1)
    lpBuff = ""
      
    'It's not in AddIns collection, prompt user.
    M = "You just opened an add-in. Do you want to install it?"
    M = M & vbNewLine
    M = M & vbNewLine & "Yes - Install the add-in. "
    M = M & vbNewLine & "No - Open it, but don't install it."
    M = M & vbNewLine & "Cancel - Close the add-in"
    Ans = MsgBox(M, vbQuestion + vbYesNoCancel, ThisWorkbook.Name)
    Select Case Ans
        Case vbYes
        
            Application.DisplayAlerts = False
            
               'Is it in the AddIns collection?
                For Each ai In AddIns
                   If Left(ai.Name, Len(sUDT)) = sUDT Then
                      ai.Installed = False
                   End If
                Next ai
                
                'Set oXl = CreateObject("Excel.Application")
                'oXl.Workbooks.Add
                Set wb = Application.Workbooks.Add
                
                
                'Test if computer is XP
                strDir = "C:\Documents and Settings\" & sUser & "\Application Data\Microsoft\AddIns\"
                DirTest = Dir$(strDir, vbDirectory)
                
                'iSpace if DirTest return vbnullstring
                'Change directory to iSpace format.
                If Len(DirTest) < 2 Then
                    strDir = "C:\Users\" & sUser & "\AppData\Roaming\Microsoft\AddIns\"
                End If
                
                'Get the addin full name
                sAddinFullName = strDir & sUDT & ".xlam"
                
                ThisWorkbook.SaveAs sAddinFullName, vbReadOnly
                'Install Addin
                Set NewAi = Application.AddIns.Add(ThisWorkbook.FullName, True)
                NewAi.Installed = True
                
                MsgBox "Installation Successful!", vbInformation

                'Set oXl = Nothing
                wb.Close False
                Set wb = Nothing
                
            Application.DisplayAlerts = True
            
        Case vbNo
            'no action, leave it open
        Case vbCancel
            ThisWorkbook.Close
    End Select

End Sub

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Ronniel Allan CastanitoIT ManagerAuthor Commented:
Sorry found the error. Line 79 is incorrect and shouldn't have vbReadOnly. It should be xlOpenXMLAddIn.

But another error comes up. Its saying the .XLAM file is not a valid Add-In. How would it know whether the file is a valid Add-In or not?
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi Ricky,

Please can you attach the .XLAM file you are referring to, then I can try to reproduce your problem locally in order to reach a solution for you?

Thanks.

BFN,

fp.
0
 
[ fanpages ]IT Services ConsultantCommented:
Thanks for coming back to the question over a month after we may have been able to assist further.
0
 
Ronniel Allan CastanitoIT ManagerAuthor Commented:
Resolved my error.
0
All Courses

From novice to tech pro — start learning today.