?
Solved

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

Posted on 2013-05-16
7
Medium Priority
?
921 Views
Last Modified: 2013-07-03
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
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39170810
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
 

Author Comment

by:Ronniel Allan Castanito
ID: 39173502
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
 

Author Comment

by:Ronniel Allan Castanito
ID: 39173786
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39198639
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
 

Accepted Solution

by:
Ronniel Allan Castanito earned 0 total points
ID: 39286154
I resolved this by unprotecting the add-in's workbook before the installation process.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39287839
Thanks for coming back to the question over a month after we may have been able to assist further.
0
 

Author Closing Comment

by:Ronniel Allan Castanito
ID: 39296030
Resolved my error.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question