0

This may seem long-winded, but hopefully someone will get my point.

My team builds macros with Macro Express Pro. A couple years ago, we developed macro tracking using VBScript to see if a macro was being used, by who and how many people, and to see gather certain pieces of data to see if they were paying claims correctly. This would be called by a separate macro. It's sole chore is to accept what the calling macro sent to it as parameters, then call the VBScript and send the parameters as arguments the VBScript would pick up and process. Since then, the VBScript has grown and has really shown us the data we were looking for.

Recently, some of us have been building end-user tools in Excel using VBA. Our leadership has requested that these tools utilize the same VBScript for tracking purposes as well. Since it would make no sense to call the Macro Express Pro macro to send the data to the VBScript, we're trying to send the data collected straight from the VBA code to the VBScript.

Here's what the Macro Express Pro tracking macro does when sending data to the VBScript:

enter image description here

Here's how the VBScript captures that info:

' 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

EDITED

What we're looking for is a way to do something similar to the following, which calls an external program and stores data on the Windows clipboard:

' 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

And then collect that data by using the following:

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

Hopefully that's not too confusing. Anyone have any advice on how this could be done? I'm not finding anything relevant online and I've already searched through the possible duplicates here on SO. The solution we're trying is a variation of passing argument from vba to vbs, but it's not working.

Lou
  • 389
  • 3
  • 20
  • 38
  • VBScript is designed to be copied and pasted into VBA. – ACatInLove Dec 01 '17 at 18:12
  • you can call the vbscript directly through vba and pass any arguments as well. [This answer](https://stackoverflow.com/questions/18376588/passing-argument-from-vba-to-vbs) will help. – Scott Holtzman Dec 01 '17 at 18:13
  • 2
    Possible duplicate of [passing argument from vba to vbs](https://stackoverflow.com/questions/18376588/passing-argument-from-vba-to-vbs) – Scott Holtzman Dec 01 '17 at 18:15
  • @Scott Holtzman Ok, so, using the code in the possible duplicate, Would you say I should call the VBScript with: `SFilename = DesktopPath & "SMT_SP_v3.vbs"` `' Run VBScript file Set wshShell = CreateObject("Wscript.Shell") wshShell.Run """" & SFilename & """"`? If so, how do I add all the arguments? I need to send over 20 possible pieces of data to the VBScript. – Lou Dec 01 '17 at 18:33
  • @ACatInLove I'm aware of that. I don't want to copy the whole VBScript into the project because, if there happen to be changes to how the data is handled in the VBScript file, I would have to update it in the VBA as well. Think of the VBScript as an INI or TXT...better yet, more like a CGI file as it takes data like a CGI would from an HTML form and sends it to a database. – Lou Dec 01 '17 at 18:37
  • You may try to use process environment variables like [here](https://stackoverflow.com/a/34321609/2165759) or to connect directly to script global variable scope like [here](https://stackoverflow.com/a/32302212/2165759). – omegastripes Dec 01 '17 at 19:13
  • @omegastripes I'm not sure I'm seeing passing parameters from VBA and accepting them as arguments in VBScript in either example. I was hoping somewhere online would have something like `wshShell.Run SFilename & "," & parameter1 & "," & parameter2` and so on. I know it's not as simple of a task as that, but everything I've seen so far doesn't look like what I'm looking for. Maybe I'm complicating things more than they need to be? – Lou Dec 01 '17 at 19:31
  • 1
    OK then, try `wshShell.Run """" & SFilename & """ """ & parameter1 & """ """ & parameter2 & """"` – omegastripes Dec 01 '17 at 19:34
  • I apologize. I don't want to use `wshShell.Run`. Is there a way to make it like this: `x = Shell(DesktopPath & SFilename & """ & parameter1 & """ """ & parameter2 & """", vbMinimizedNoFocus)`. Is something like this possible? I already use the `Shell()` function to run another program, but it's an EXE, not a VBS. – Lou Dec 01 '17 at 19:49
  • VBS is an exe too. Wscript.exe. – ACatInLove Dec 01 '17 at 21:28
  • Ok...so, am I able to use code similar to what's in my last comment to call the VBS and send it parameters? If so, how do I modify it? I haven't been able to find an answer online, so I thought I'd come here where everyone has been extremely helpful in the past. – Lou Dec 02 '17 at 00:44
  • VBScript is hosted by various programs (Internet Explorer, Windows Scripting Host, and other programs - its trivial to add vbscript support to a program). Windows Scripting Host (wscript.exe and cscript.exe) runs vbs files. It's object model has command line parameters. VBScript's help includes WSH documentation as well http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe. – ACatInLove Dec 02 '17 at 01:37
  • @ACatInLove I'm not trying to add VBScript to a program. The VBScript is already it's own entity...like I said before, as a CGI (Common Gateway Interface). I have a VBA that is collecting data to send to a database. The VBScript is the CGI between the VBA and the database. I'm looking for a way to send data as parameters from the VBA, through the VBScript as arguments, and off to the database. – Lou Dec 11 '17 at 15:48
  • When you want your problem solved you'll do what I say. – ACatInLove Dec 12 '17 at 00:18
  • @ACatInLove I'm sorry. It's not that I don't want help with this. I'm just trying to get you to explain in terms I can understand. If you say "When you want your problem solved you'll do what I say.", and I've read what you've said and none of it makes sense, then you've either explained it in terms that only you understand, or you haven't answered my question at all. I appreciate any advice, but "do what I say" and then not clearly say it doesn't help. You've given me links to things you feel will help, but nothing yet has brought me close to an answer. Can anyone else please advise me? – Lou Dec 12 '17 at 14:01
  • Read the documentation. I gave a link to it. – ACatInLove Dec 12 '17 at 17:34
  • Ok...I work for a company that has a lot of security on each computer. The documentation is something I have to download and run. I don't have administrative permission to do that. I'm not asking anyone to code the whole project for me. If you look at the initial question, I've done all that. I have ONE thing I need help with. If you're not willing to offer advice one can use, please don't advise. As I stated in the initial question: "I'm not finding anything relevant online and I've already searched through the possible duplicates here on SO." – Lou Dec 12 '17 at 18:01
  • @omegastripes I've decided to try your solution. I'm not receiving any errors, but I'm also not seeing any entries for my attempts in the database. Is there something I'm doing wrong? Any advice? – Lou Dec 12 '17 at 18:21
  • 1
    @Lou fix the code: `...ScriptFile & """" & MacroFile...` should be `...ScriptFile & """ """ & MacroFile...`, and the last `...Value20` should be `...Value20 & """" `. That line passes wrong syntax to `.Run` method, that is strange that there is no errors when you run it. Is there any `On Error Resume Next` statement in the code? Add `MsgBox` that will show the concatenated string before run for debug. And also add `MsgBox WScript.Arguments.Count` in the second code to check if arguments are passed. – omegastripes Dec 12 '17 at 18:55
  • If you won't read the documentation then you shouldn't program in it. I was quite precise. *It's object model has command line parameters* – ACatInLove Dec 12 '17 at 19:42
  • @ACatInLove ...1) I CAN'T read the documentation as I don't have admin access to download it, 2) if YOU aren't going to read anything I post, then you're not going to understand what I'm looking for, and 3) if you don't want to help the way I asked, please don't reply! – Lou Dec 12 '17 at 20:22
  • There's a thing called the internet. And a search engine called google. www.google.com.au/search?vbscript+documentation+msdn – ACatInLove Dec 12 '17 at 20:39
  • @ACatInLove Why are you still going on? You obviously didn't want to help, someone else did...get over it. I know how to code, I know what I know from the internet and people here in stack overflow, I looked on the internet before coming here for an answer, and I got it. If you don't want to read my questions and just want to waste your time and mine by not offering a solution, don't reply...simple as that. I'm going to let it go as I've been given a solution. I suggest you do the same. :) – Lou Dec 12 '17 at 20:52

1 Answers1

0

Thanks to omegastripes, I'm now able to complete the task. While this "may look like other examples", I was looking for something specific and omegastripes came through. Here's the code:

' 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 & """"

I was able to confirm by looking in the database that all the data was tracked perfectly. Thanks again.

Lou
  • 389
  • 3
  • 20
  • 38