Nicolas Klebusits
Nicolas Klebusits

Reputation: 55

passing full name from batch file to vbs file and then run a word macro with it

I have a batch file that fetches the full name characteristic of an ad user and sets it as variable n. I need to take that variable and pass it into a vbs file and then run a word macro from that vbscript. Here's what I have:

Batch:

@echo off
for /f "tokens=*" %%i in ('NET USER "username" /DOMAIN ^| FIND /I "Full name"') do set n=%%i
cscript //NoLogo H:\firstRotation\mgmtSSnD\CMMR\fullName.vbs %1

vbs:

fullName = WScript.Arguments(0)
Set objWord = GetObject(,"Word.Application")
Set x = objWord.Documents.Open("mypath\my.docm")
objWord.Run "test", fullName

vba:

Private sub test(ByVal x As String)
Msgbox (x)
End sub

As of right now, I keep getting a VBScript "subscript out of range error" so my problem has to do with passing the variable from batch to vbs.

Upvotes: 0

Views: 121

Answers (1)

user6811411
user6811411

Reputation:

The variable n would contain Full Name _spaces_ User Name but you don't use it to pass it to the vbs.

This batch ignores Full Name and passes the result in %%B directly to the vbscript.

@echo off
for /f "tokens=2*" %%A in (
    'NET USER "username" /DOMAIN ^| FIND /I "Full name"'
) do cscript //NoLogo H:\firstRotation\mgmtSSnD\CMMR\fullName.vbs "%%B"

The quotes passed from the batch get stripped in the vbs, you have to requote.
I assume the error message comes from the vba.

fullName = WScript.Arguments(0)
Set objWord = GetObject(,"Word.Application")
Set x = objWord.Documents.Open("mypath\my.docm")
objWord.Run "test", """fullName"""

Parsing net user output with default delimiter space (successive count as 1):

        "Full Name                    Anthoni B. Caesar"
Tokens     1   2                        3     4    5
Delims       _    ____________________       _  _
For-Var  %%A  %%B                      %%C    %%D %%E

Upvotes: 1

Related Questions