是的,基本上最简单的方法是将“mymacro”的内容移动到 ThisWorkBook
Private Sub Workbook_Open()
出于安全考虑,除非您希望无人看管,否则用户可能仍需单击启用宏按钮。如果要将参数传递到打开的工作簿中,则可以通过解析命令行来执行此操作。您可以在 Google 上搜索“excel GetCommandLineW”的代码示例
Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)
Function CmdToSTr(cmd As Long) As String
Dim Buffer() As Byte
Dim StrLen As Long
If cmd Then
StrLen = lstrlenW(cmd) * 2
If StrLen Then
ReDim Buffer(0 To (StrLen - 1)) As Byte
CopyMemory Buffer(0), ByVal cmd, StrLen
CmdToSTr = Buffer
End If
End If
End Function
Private Sub Workbook_Open()
Dim CmdRaw As Long
Dim CmdLine As String
CmdRaw = GetCommandLine
CmdLine = CmdToSTr(CmdRaw)
' From here you can parse the CmdLine
' ...snip...