programing

VBA 프로젝트의 조건부 컴파일 속성을 프로그래밍 방식으로 변경하는 방법

telebox 2023. 6. 11. 10:35
반응형

VBA 프로젝트의 조건부 컴파일 속성을 프로그래밍 방식으로 변경하는 방법

저는 현재 VBA 확장성을 사용하여 Excel 워크북에 VBA 기능을 추가하는 VBA 코드 생성기/인젝터를 개발하고 있습니다.이 모든 것이 잘 작동합니다.

그러나 주입된 원래 코드는 일부 전역 조건부 컴파일 인수를 참조하여 조건부 컴파일을 사용합니다.

여기에 이미지 설명 입력

VBA 프로젝트의 조건부 컴파일 인수를 프로그래밍 방식으로 수정/추가할 수 있는 방법이 있습니까?

나는 VBP 프로젝트의 모든 속성을 확인했지만 아무것도 찾을 수 없었습니다.

SiddharthRoute가 보여준 이 접근 방식에서 영감을 받아 다음 솔루션을 찾을 수 있었습니다.SendMessage그리고.FindWindow:

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindowTextLength Lib "user32" Alias _
"GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Const WM_SETTEXT = &HC
Const BM_CLICK = &HF5


Public Sub subSetconditionalCompilationArguments()
    Dim strArgument As String
    Dim xlApp As Object
    Dim wbTarget As Object

    Dim lngHWnd As Long, lngHDialog As Long
    Dim lngHEdit As Long, lngHButton As Long

    strArgument = "PACKAGE_1 = 1"

    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = False

    Set wbTarget = xlApp.Workbooks.Open("C:\Temp\Sample.xlsb")

    'Launch the VBA Project Properties Dialog
    xlApp.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute

    'Get the handle of the "VBAProject" Window
    lngHWnd = FindWindow("#32770", vbNullString)
    If lngHWnd = 0 Then
        MsgBox "VBAProject Property Window not found!"
        GoTo Finalize
    End If

    'Get the handle of the dialog
    lngHDialog = FindWindowEx(lngHWnd, ByVal 0&, "#32770", vbNullString)
    If lngHDialog = 0 Then
        MsgBox "VBAProject Property Window could not be accessed!"
        GoTo Finalize
    End If

    'Get the handle of the 5th edit box
    lngHEdit = fctLngGetHandle("Edit", lngHDialog, 5)
    If lngHEdit = 0 Then
        MsgBox "Conditional Compilation Arguments box could not be accessed!"
        GoTo Finalize
    End If

    'Enter new argument
    SendMessage lngHEdit, WM_SETTEXT, False, ByVal strArgument

    DoEvents

    'Get the handle of the second button box (=OK button)
    lngHButton = fctLngGetHandle("Button", lngHWnd)
    If lngHButton = 0 Then
        MsgBox "Could not find OK button!"
        GoTo Finalize
    End If

    'Click the OK Button
    SendMessage lngHButton, BM_CLICK, 0, vbNullString

Finalize:
    xlApp.Visible = True
    'Potentially save the file and close the app here
End Sub

Private Function fctLngGetHandle(strClass As String, lngHParent As Long, _
    Optional Nth As Integer = 1) As Long
    Dim lngHandle As Long
    Dim i As Integer

    lngHandle = FindWindowEx(lngHParent, ByVal 0&, strClass, vbNullString)
    If Nth = 1 Then GoTo Finalize

    For i = 2 To Nth
        lngHandle = FindWindowEx(lngHParent, lngHandle, strClass, vbNullString)
    Next
Finalize:
    fctLngGetHandle = lngHandle
End Function

사용한 액세스 2000의 경우:

Application.GetOption("Conditional Compilation Arguments")

잊어버리고,

Application.SetOption("Conditional Compilation Arguments", "<arguments>")

세팅을 위해서.

이상입니다.

그 대화 상자에 있는 모든 것에 영향을 미치는 유일한 방법은SendMessageAPI 함수 또는Application.SendKeys다음과 같이 코드로 상수를 선언하는 것이 좋습니다.

#Const PACKAGE_1 = 0

그런 다음 코드를 수정하도록 하십시오.CodeModule모든 VBA 구성 요소:

Dim comp As VBComponent
For Each comp In ThisWorkbook.VBProject.VBComponents
    With comp.CodeModule
        Dim i As Long
        For i = 1 To .CountOfLines
            If Left$(.Lines(i, 1), 18) = "#Const PACKAGE_1 =" Then
                .ReplaceLine i, "#Const PACKAGE_1 = 1"
            End If
        Next i
    End With
Next comp

다음은 2010년 이후 액세스에서 여러 인수를 가져오고 설정하는 방법입니다.

여기에 이미지 설명 입력

설정하는 코드는 다음과 같습니다.

application.SetOption "Conditional Compilation Arguments","A=4:B=10"

해당 항목을(를)

Application.GetOption("Conditional Compilation Arguments")

다음과 같이 인쇄됩니다.A = 4 : B = 10

이것이 바로 테스트 방법입니다.

Sub TestMe()

    #If A = 1 Then
        Debug.Print "a is 1"
    #Else
        Debug.Print "a is not 1"
    #End If

End Sub

언급URL : https://stackoverflow.com/questions/19726791/how-to-programmatically-change-conditional-compilation-properties-of-a-vba-proje

반응형