Solved

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

Posted on 2013-05-16
7
894 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
  • 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Move data from one sheet to another 11 40
Select/Copy row and pasting it lower in sheet 7 23
Creating An Intelligent Dropdown 8 34
Formula help - MIN 7 18
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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.

825 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