Pass parameters to an application

2019-09-09 21:53发布

I'm attempting to use ghostscript to set a specific page size for scanned PDF files. I can run the script when it resides in the same folder as the executable but when I move the .vbs file out of the directory I can't get it to work. The .run line is where I'm having problems. The triple quotes for exe path without arguments opens the exe but i can't figure out how to pass the parameters. I'm sure you can see that I'm pretty new to this.

strInput = InputBox ("Enter 1 for Landscape or 2 for portrait:")
Set objShell = WScript.CreateObject("WScript.Shell")

If strInput=1 then  
    Call LandScape 
ElseIf strInput=2 Then
    Call Portrait
Else
    MsgBox "Your entry is invalid.  Click OK to exit"
End If

Landscape sub-routine:

Sub LandScape
    MsgBox "Your images are Landscape"
    objShell.Run """c:\Program Files\gs\gs9.04\bin\gswin64c.exe""& "-dQUIET"&" -dNOPAUSE"&" -dBATCH"&" -dDEVICEWIDTHPOINTS=2592"&" -dDEVICEHEIGHTPOINTS=1728"&" -dFIXEDMEDIA"&" -sDEVICE=pdfwrite"&" -sOutputFile=OUTPUT.pdf"&" INPUT.pdf""

    'This line works when in same directory: gswin64c -dQUIET -dNOPAUSE -dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT.pdf
End Sub

2条回答
走好不送
2楼-- · 2019-09-09 22:22

The 1st parameter to WScript.Shell.Run() is a full command line. So you need to

  1. Add spaces between arguments;
  2. Quote arguments that contain embedded spaces. In VB/VBScript, two consecutive double quotes inside a string literal are interpreted as a literal quote.

Can you now deduce the correct syntax for the objShell.Run line?

查看更多
做个烂人
3楼-- · 2019-09-09 22:23

The quotes can get confusing so it sometimes helps to use Chr(34) instead and to keep the executable separate from its params.

strExe    = "c:\Program Files\gs\gs9.04\bin\gswin64c.exe"
strParams = "-dQUIET -dNOPAUSE -dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT.pdf"

objShell.Run Chr(34) & strExe & Chr(34) & " " & strParams
查看更多
登录 后发表回答