El complemento de Excel no se carga al abrir Excel desde VBA

El complemento de Excel no se carga al abrir Excel desde VBA

estoy usando excelcomplemento reg-exy funciona bien, el único problema es que no se carga cuando VBA inicia Excel.

Tengo una macro en Word para recopilar algunos datos en Word y copiarlos a Excel, este inicia Excel, el resultado está bien, pero necesito reiniciar Excel para poder usar el complemento (está habilitado en las opciones). , simplemente no se carga).

Probé con otra macro: simplemente abrí Excel y creé un nuevo libro y tiene el mismo problema.

Solo tengo este complemento de terceros, por lo que no puedo compararlo con otros.

¿Alguna idea?

Respuesta1

Así es como se supone que debe funcionar.

De documentos COM.

      Component Automation  
Exposing the Application Object  

 Language Filter: All Language Filter: Multiple Language Filter: Visual Basic Language Filter: C# Language Filter: C++ Language Filter: J# Language Filter: JScript  
 Visual Basic (Declaration) 
 Visual Basic (Usage) 
 C# 
 C++ 
 J# 
 JScript 

Any document-based, user-interactive applications that expose ActiveX objects should have one top-level object named the Application object. This object is initialized as the active object when an application starts.

The Application object identifies the application and provides a way for ActiveX clients to bind to and navigate the application's exposed objects. All other exposed objects are subordinate to the Application object; it is the root-level object in the object hierarchy.

The names of the Application object's members are part of the global name space, so ActiveX clients do not need to qualify them. For example, if MyApplication is the name of the Application object, a Visual Basic program can refer to a method of MyApplication as MyApplication.MyMethod or simply MyMethod. However, you should be careful not to overload the Application object with too many members because it can cause ambiguity and decrease performance. A large, complicated application with many members should be organized hierarchically, with a few generalized objects at the top, branching out into smaller, more specialized objects. 

The following chart shows how applications should expose their Application and Document objects.

Command line  Multiple-document interface application  Single-document interface application  
/Embedding 
 Expose class factories for document classes, but not for the application.

Call RegisterActiveObject for the Application object.
 Expose class factories for document class, but not for the application.

Call RegisterActiveObject for the Application object.

/Automation 
 Expose class factories for document classes.

Expose class factory for the application using RegisterClassObject.

Call RegisterActiveObject for the Application object.
 Do not expose class factory for document class.

Expose class factory for the Application object using RegisterClassObject.

Call RegisterActiveObject for the Application object.

No OLE switches 
 Expose class factories for document classes, but not for the application.

Call RegisterActiveObject for the Application object.
 Call RegisterActiveObject for the Application object. 


The call to RegisterActiveObject enters the Application object in OLE's running object table (ROT), so ActiveX clients can retrieve the active object instead of creating a new instance. Visual Basic applications can use the GetObject statement to access an existing object.

 © Microsoft Corporation. All rights reserved. 

Entonces /a o /embedding no cargarán nada más que la aplicación básica (y /a también es un buen paso para solucionar problemas).

Inicie su aplicación, luego GetObject en ella.

Además, hay pocas razones para utilizar un complemento, ya que puede hacer exactamente lo que hace el complemento. Esto es vbs (que se puede pegar en VBA) usando el mismo motor RegEx que su complemento (Word también tiene su propio motor RegEx llamado Use Wildcards). Establecer una referencia a Microsoft VBScript Regular Expression 5.5.

Set regEx1 = New RegExp
If Instr(LCase(Arg(1)), "i") > 0 then
    regEx1.IgnoreCase = True
Else
    regEx1.IgnoreCase = False
End If 
If Instr(LCase(Arg(1)), "v") > 0 then
    IncExc = False
Else
    IncExc = True
End If 
regEx1.Global = False
regEx1.Pattern = Pttn 
Do Until Inp.AtEndOfStream
    Line=Inp.readline
    If RegEx1.Test(Line) = IncExc then
        outp.writeline Line
    End If
Loop

Respuesta2

Lo resolví según la respuesta publicada a una pregunta similar en stackoverflow:https://stackoverflow.com/a/806720/4721734

Investigué este problema nuevamente y la colección Application.Addins parece tener todos los complementos enumerados en el menú Herramientas->Complementos, con un valor booleano que indica si un complemento está instalado o no. Entonces, lo que parece funcionar para mí ahora es recorrer todos los complementos y si .Installed = true entonces configuro .Installed en False y vuelvo a True, y eso parece cargar correctamente mis complementos.

Function ReloadXLAddins(TheXLApp As Excel.Application) As Boolean

    Dim CurrAddin As Excel.AddIn

    For Each CurrAddin In TheXLApp.AddIns
        If CurrAddin.Installed Then
            CurrAddin.Installed = False
            CurrAddin.Installed = True
        End If
    Next CurrAddin

End Function

información relacionada