从Access 2013中的VBA代码,我试图运行一个接受1个参数的PowerShell脚本。此参数是从Access(VBA)内部提供(和设置)的。
尽管我注意到该脚本并未完全以正确的方式被谴责。它运行一个将一些PDF文件合并在一起的应用程序。提供的参数使应用程序知道应该合并哪个文件夹(包含PDF文件)。
虽然没有考虑该参数。
我的VBA脚本如下:
Err = Shell("powershell.exe -ExecutionPolicy Unrestricted -File G:\OPSPDF\MergeFiles.ps1 -path """ & MergedFolder & """ ", 1)
MergedFolder参数是文件夹的字符串路径,该文件夹包含要合并
("O:\documents\somefiles")
的PDF文件我的powershell脚本如下:
param([String] $inputfolder)
$PDFtk = "D:\Test\pdftk.exe"
$outputfile = "MergedFile.pdf"
dir $inputfolder -r -include *.pdf -exclude $outputfile | group DirectoryName |
% {& $PDFtk $_.group CAT OUTPUT "$($_.Name)\$outputfile"}
cmd /c pause | out-null
PDFtk
是应在其中启动exe
的位置。暂停是内置的,以查看出了什么问题。因为它无法正常工作,所以我尝试在powershell脚本中从VBA回显我的给定参数,如下所示:
param([String] $inputfolder)
echo $inputfolder
$PDFtk = "D:\Test\pdftk.exe"
$outputfile = "MergedFile.pdf"
dir $inputfolder -r -include *.pdf -exclude $outputfile | group DirectoryName |
% {& $PDFtk $_.group CAT OUTPUT "$($_.Name)\$outputfile"}
cmd /c pause | out-null
没有回声,所以让我相信VBA中作为参数给出的变量可能出了问题(它包含一些特殊字符,如
\
),所以我将MergedFolder
变量替换为这样的硬编码字符串:Err = Shell("powershell.exe -ExecutionPolicy Unrestricted -File G:\OPSPDF\MergeFiles.ps1 -path "" TestString "" ", 1)
但是它仍然没有回音...我在这里做错了什么?
最佳答案
inputputfolder
)中有$inputfolder
参数,但是您在VBA代码中传递了-path
参数。 Err = Shell("powershell.exe -ExecutionPolicy Unrestricted -File G:\OPSPDF\MergeFiles.ps1 -inputfolder TestString", 1)
关于vba - 从VBA向Powershell提供参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33438203/