【发布时间】:2018-05-15 21:21:17
【问题描述】:
这可能看起来很啰嗦,但希望有人能明白我的意思。
我的团队使用 Macro Express Pro 构建宏。几年前,我们使用 VBScript 开发了宏跟踪,以查看是否正在使用宏、由谁和多少人使用,并查看收集某些数据以查看他们是否正确支付索赔。这将由一个单独的宏调用。唯一的苦差事是接受调用宏作为参数发送给它的内容,然后调用 VBScript 并将参数作为 VBScript 将拾取和处理的参数发送。从那时起,VBScript 不断发展壮大,并真正向我们展示了我们正在寻找的数据。
最近,我们中的一些人一直在使用 VBA 在 Excel 中构建最终用户工具。我们的领导层要求这些工具也使用相同的 VBScript 进行跟踪。由于调用 Macro Express Pro 宏将数据发送到 VBScript 是没有意义的,因此我们尝试将直接从 VBA 代码收集的数据发送到 VBScript。
以下是 Macro Express Pro 跟踪宏在向 VBScript 发送数据时的作用:
以下是 VBScript 捕获该信息的方式:
' Sets up the object variables.
Option Explicit
Dim objFSO, objTextFile, conn
' Sets up the all the string variables for the program.
Dim Arg, UserID, MyDocs, strTextFile, strTextLine, strDataLine, Macro_Name, User_ID
Dim Run_Time, Claim_Number, strSQLData(28), dbHeader, dbSQLData, strConnect, message
Dim value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11
Dim value12, value13, value14, value15, value16, value17, value18, value19, value20
Dim StartTime, EndTime, Macro_FileName, Ver, strSQL, strTrackVar
Dim strcount, i
'This creates the required Objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Arg = Wscript.Arguments
strcount = Wscript.Arguments.Count
For i = 0 to strcount - 1
If strTextLine = "" Then
strTextLine = Arg.item(i)
Else
strTextLine = strTextLine & "," & Arg.item(i)
End If
Next
'------------------Calls up the Process Tracking Submission sub-------------------
ProcessTrackingSubmission
Sub ProcessTrackingSubmission()
' Set up the origin and destination files
on error resume next
' Read the entire origin file and store as an array usable variables
strDataLine = Split(strTextLine, ",")
Macro_FileName = strDataLine(0)
Macro_FileName= objFSO.GetFile(Macro_FileName).Name
Macro_Name = strDataLine(1)
User_ID = strDataLine(2)
StartTime = strDataLine(3)
EndTime = strDataLine(4)
Claim_Number = strDataLine(5)
If strDataLine(5) <> "" Then
value1 = strDataLine(6)
End If
If strDataLine(6) <> "" Then
value2 = strDataLine(7)
End If
If strDataLine(7) <> "" Then
value3 = strDataLine(8)
End If
If strDataLine(8) <> "" Then
value4 = strDataLine(9)
End If
If strDataLine(9) <> "" Then
value5 = strDataLine(10)
End If
If strDataLine(10) <> "" Then
value6 = strDataLine(11)
End If
If strDataLine(11) <> "" Then
value7 = strDataLine(12)
End If
If strDataLine(12) <> "" Then
value8 = strDataLine(13)
End If
If strDataLine(13) <> "" Then
value9 = strDataLine(14)
End If
If strDataLine(14) <> "" Then
value10 = strDataLine(15)
End If
If strDataLine(15) <> "" Then
value11 = strDataLine(16)
End If
If strDataLine(16) <> "" Then
value12 = strDataLine(17)
End If
If strDataLine(17) <> "" Then
value13 = strDataLine(18)
End If
If strDataLine(18) <> "" Then
value14 = strDataLine(19)
End If
If strDataLine(19) <> "" Then
value15 = strDataLine(20)
End If
If strDataLine(20) <> "" Then
value16 = strDataLine(21)
End If
If strDataLine(21) <> "" Then
value17 = strDataLine(22)
End If
If strDataLine(22) <> "" Then
value18 = strDataLine(23)
End If
If strDataLine(23) <> "" Then
value19 = strDataLine(24)
End If
If strDataLine(24) <> "" Then
value20 = strDataLine(25)
End If
InsertIntoDatabase()
End Sub
已编辑
我们正在寻找一种类似于以下内容的方法,它调用外部程序并将数据存储在 Windows 剪贴板上:
' This is used for tracking purposes
StartTime = Now
Set wshShell = CreateObject("Wscript.Shell")
MacroFile = "Stand-alone Tool"
CurrentMacro = "VBA Tracking Test.xlsm"
UserId = Environ("UserName")
ScriptPath = Environ("UserProfile") & "\Documents\Insight Software\Macro Express\Macro Logs\"
ScriptFile = ScriptPath & "SMT_SP_v3.vbs"
ClaimNum = "1234567890"
EndTime = Now
wshShell.Run """" & ScriptFile & """" & MacroFile & """ """ & CurrentMacro & """ """ & UserId & """ """ & StartTime & """ """ & _
EndTime & """ """ & ClaimNum & """ """ & Value1 & """ """ & Value2 & """ """ & Value3 & """ """ & Value4 & """ """ & Value5 & """ """ & _
Value6 & """ """ & Value7 & """ """ & Value8 & """ """ & Value9 & """ """ & Value10 & """ """ & Value11 & """ """ & Value12 & """ """ & _
Value13 & """ """ & Value14 & """ """ & Value15 & """ """ & Value16 & """ """ & Value17 & """ """ & Value18 & """ """ & Value19 & """ """ & Value20
然后使用以下方法收集该数据:
Set Arg = Wscript.Arguments
strcount = Wscript.Arguments.Count
For i = 0 to strcount - 1
If strTextLine = "" Then
strTextLine = Arg.item(i)
Else
strTextLine = strTextLine & "," & Arg.item(i)
End If
Next
希望这不会太令人困惑。有人对如何做到这一点有任何建议吗?我没有在网上找到任何相关的东西,而且我已经在 SO 上搜索了可能的重复项。我们正在尝试的解决方案是 passing argument from vba to vbs 的变体,但它不起作用。
【问题讨论】:
-
VBScript 旨在复制并粘贴到 VBA 中。
-
您可以直接通过 vba 调用 vbscript 并传递任何参数。 This answer 会有所帮助。
-
好吧,试试
wshShell.Run """" & SFilename & """ """ & parameter1 & """ """ & parameter2 & """" -
@Lou 修正代码:
...ScriptFile & """" & MacroFile...应该是...ScriptFile & """ """ & MacroFile...,最后一个...Value20应该是...Value20 & """"。该行将错误的语法传递给.Run方法,奇怪的是运行它时没有错误。代码中有On Error Resume Next语句吗?添加MsgBox,它将在运行调试之前显示连接的字符串。并在第二个代码中添加MsgBox WScript.Arguments.Count以检查是否传递了参数。