gpt4 book ai didi

vba - 如何以编程方式更改 VBA 项目的条件编译属性

转载 作者:行者123 更新时间:2023-12-03 14:42:20 28 4
gpt4 key购买 nike

我目前正在开发一个 VBA 代码生成器/注入(inject)器,它通过使用 VBA 可扩展性将 VBA 功能添加到 Excel 工作簿。这一切都很好。

但是,注入(inject)的原始代码使用条件编译,指的是一些全局条件编译参数:

enter image description here

有什么方法可以以编程方式修改/添加 VBA 项目的条件编译参数?

我检查了 VBProject 的所有属性,但找不到任何东西。

最佳答案

灵感来自 this approach ,由 SiddharthRout 显示,我设法使用 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

关于vba - 如何以编程方式更改 VBA 项目的条件编译属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726791/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com