0
点赞
收藏
分享

微信扫一扫

bat脚本打开VBA文件

王传学 2022-05-01 阅读 118

’ 32 位系统为
’ Declare Function GetCommandLine Lib “kernel32” Alias “GetCommandLineW” () As Long
’ Declare Sub CopyMemory Lib “kernel32” Alias “RtlMoveMemory” (MyDest As Any, MySource As Any, ByVal MySize As Long)
’ Declare Function lstrlenW Lib “kernel32” (ByVal lpString As Long) As Long
’ 64 位系统为
Private Declare PtrSafe Function GetCommandLine Lib “kernel32” Alias “GetCommandLineW” () As LongPtr
Private Declare PtrSafe Function lstrlenW Lib “kernel32” (ByVal lpString As LongPtr) As LongPtr
Private Declare PtrSafe Sub CopyMemory Lib “kernel32” Alias “RtlMoveMemory” (MyDest As Any, MySource As Any, ByVal MySize As LongPtr)

 
Private Sub Workbook_Open()
    
    Dim CmdRaw  As LongPtr
    Dim CmdLine As String
    Dim Msg     As String
 
    CmdRaw = GetCommandLine
    CmdLine = CmdToSTr(CmdRaw)
    Dim paraPos%
    On Error Resume Next ' 这句是必须的,防止非bat打开,下面代码会报错
    paraPos = WorksheetFunction.Search("/batOpen", CmdLine, 1) '检查打开方式
     If paraPos > 0 Then
        MsgBox "bat"
        '===============
         '===============
    Else
        MsgBox "man"
    End If
    
End Sub
 
' 被调用的子函数 , 用来将命令行参数转换成字符串类型:
Function CmdToSTr(Cmd As LongPtr) As String
    Dim Buffer() As Byte
    Dim StrLen   As LongPtr
    If Cmd Then
        StrLen = lstrlenW(Cmd) * 2
        If StrLen Then
            ReDim Buffer(0 To CInt(StrLen - 1)) As Byte
            CopyMemory Buffer(0), ByVal Cmd, StrLen
            CmdToSTr = Buffer
        End If
    End If
End Function
举报

相关推荐

0 条评论