Solved

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

Posted on 2013-05-16
7
883 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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 will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now