在Excel 2010中从命令行传递参数 [英] Passing arguments from command line in excel 2010
问题描述
我发现了几个代码段,这些代码段使我可以从命令行将参数传递给excel。
I have found several snippet of code out there that allows me to pass arguments into excel from command line.
下面的代码位于一个名为parameters的新模块中:
The code below is placed in a new module called 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
,然后在此工作簿中,我将此代码称为
and then in thisworkbook i call this code
Sub workBook_open()
MsgBox Parameters.CmdLineToStr
End Sub
使用 GetCommandLine
函数失败,因此由于链接问题导致错误dll库还是这是由于我在personal.xlsb中存储了一些宏这一事实?
It fails with the GetCommandLine
function, so is the error due to problems with linking the dll library or is this due to the fact that i have some macros stored in personal.xlsb?
我从命令行使用以下行调用excel工作表:
C:\Users\kim \Desktop>启动excel Parameters.xlsm / e / nmbnmbmnb
I call the excel sheet from the command line with this line:
C:\Users\kim\Desktop>start excel Parameters.xlsm /e/nmbnmbmnb
我得到这个错误:
推荐答案
经过纠正,经过测试的过程:(上面的版本有两个错误。)
The corrected, tested procedure: (The version above had two mistakes.)
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的命令行,例如:
...place the code in a new, regular module, then call CmdLineToStr
to get the command line that was used to open Excel, such as:
excel.exe /x "C:\Users\someone\Desktop\myTest.xlsm"
这篇关于在Excel 2010中从命令行传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!