Solved

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

Posted on 2013-05-16
7
907 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
Question by:RiCzN
[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:RiCzN
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:RiCzN
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
Industry Leaders: 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:
RiCzN 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:RiCzN
ID: 39296030
Resolved my error.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

738 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