VBA에서 Excel을 열 때 Excel 추가 기능이 로드되지 않습니다.

VBA에서 Excel을 열 때 Excel 추가 기능이 로드되지 않습니다.

저는 엑셀을 사용하고 있어요정규식 추가 기능잘 작동하지만 VBA에서 Excel을 시작할 때 로드되지 않는 유일한 문제가 있습니다.

Word에서 일부 데이터를 수집하여 Excel에 복사하는 매크로가 있습니다. 이것은 Excel을 시작하고 출력은 괜찮지만 추가 기능을 사용하려면 Excel을 다시 시작해야 합니다(옵션에서 활성화되어 있음). , 로드되지 않음).

다른 매크로를 시도했습니다. Excel을 열고 새 통합 문서를 만들면 동일한 문제가 발생합니다.

저는 이 타사 추가 기능 하나만 갖고 있어서 다른 추가 기능과 비교할 수 없습니다.

어떤 아이디어?

답변1

그것이 작동하는 방식입니다.

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. 

따라서 /a 또는 /embedding은 기본 앱 외에는 아무것도 로드하지 않습니다(그리고 /a는 좋은 문제 해결 단계이기도 합니다).

앱을 시작한 다음 GetObject를 시작하세요.

또한 추가 기능이 수행하는 작업을 정확하게 수행할 수 있으므로 추가 기능과 같은 사용할 이유가 거의 없습니다. 이것은 추가 기능과 동일한 RegEx 엔진을 사용하는 vbs(VBA에 붙여 넣을 수 있음)입니다(Word에는 이라는 자체 RegEx 엔진도 있습니다 Use Wildcards). 에 대한 참조를 설정합니다 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

답변2

stackoverflow의 비슷한 질문에 게시된 답변을 기반으로 문제를 해결했습니다.https://stackoverflow.com/a/806720/4721734

이 문제를 다시 조사해 본 결과, Application.Addins 컬렉션에는 도구->추가 기능 메뉴에 나열된 모든 추가 기능이 있고 추가 기능 설치 여부를 나타내는 부울 값이 있는 것 같습니다. 따라서 지금 저에게 효과적인 방법은 모든 추가 기능을 반복하고 .Installed = true인 경우 .Installed를 False로 설정했다가 다시 True로 설정하면 추가 기능이 제대로 로드되는 것 같습니다.

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

관련 정보