我找到了几段代码,允许我从命令行将参数传递给excel。
以下代码放在名为parameters:
的新模块中Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)
Public CmdLineToStr() As String
'
' Returns the command line in the call to Excel
'
Dim Buffer() As Byte
Dim StrLen As Long
Dim CmdPtr As Long
CmdPtr = GetCommandLineW()
If CmdPtr > 0 Then
StrLen = lstrlenW(CmdPtr) * 2
If StrLen > 0 Then
ReDim Buffer(0 To (StrLen - 1)) As Byte
CopyMemory Buffer(0), ByVal CmdPtr, StrLen
CmdLineToStr = Buffer
End If
End If
End Sub
然后在本作业中我称这个代码为
Sub workBook_open()
MsgBox Parameters.CmdLineToStr
End Sub
它失败了GetCommandLine
函数,由于链接dll库的问题导致错误,或者这是因为我在personal.xlsb中存储了一些宏?
我使用以下行从命令行调用excel表:
C:\Users\kim\Desktop>start excel Parameters.xlsm /e/nmbnmbmnb
我收到此错误:
答案 0 :(得分:3)
更改IntStreamEx.iterate(0, i -> i+1).boxed().limit(100)
.peek((i) -> {try {Thread.sleep(50);} catch (InterruptedException e) {}})
.groupRuns((a, b) -> a/10 == b/10)
.forEach(System.out::println);
到Public CmdLineToStr() As String
Public Function CmdLineToStr() As String
不是一个程序,您需要放置Public CmdLineToStr() As String
或Sub
这样的程序。因此,错误消息“无效的外部过程”,因为您正好在一个过程之外。
答案 1 :(得分:3)
经过纠正,经过测试的过程:(上面的版本有两个错误。)
Option Explicit
Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(MyDest As Any, MySource As Any, ByVal MySize As Long)
Public Function CmdLineToStr() As String
'Returns the command line used to open Excel
Dim Buffer() As Byte, StrLen As Long, CmdPtr As Long
CmdPtr = GetCommandLine()
If CmdPtr > 0 Then
StrLen = lstrlenW(CmdPtr) * 2
If StrLen > 0 Then
ReDim Buffer(0 To (StrLen - 1)) As Byte
CopyMemory Buffer(0), ByVal CmdPtr, StrLen
CmdLineToStr = Buffer
End If
End If
End Function
...将代码放入新的常规模块中,然后调用CmdLineToStr
以获取用于打开Excel的命令行,例如:
excel.exe /x "C:\Users\someone\Desktop\myTest.xlsm"