[英]Powershell with argument/parameter
我需要使用Powershell执行word宏。 到目前为止,我有
$wd = new-object -comobject word.application
$wd.run("PrintAll")
$wd.quit()
我想做的是添加一个参数或参数,该参数或参数将指向网络上的特定文件夹,然后将该参数传递给宏。 宏开始如下:
Public sPath
Sub PrintAll()
Dim fs, f, fc, f1
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(sPath) ' Provide folder name where doc files are present
Set fc = f.Files
Dim FileList() As String
Dim Cnt As Integer
Cnt = 0
Dim myDoc As Word.Document
我需要指向大约70个不同的文件夹位置,目标是在Word中仅包含1个Sub(宏),但是要有指向每个文件夹的单独的Powershell脚本,并将它们传递给一个Sub(宏)。 我也希望Powershell中的参数将文件夹位置发送到宏中的变量sPath。
关于如何实现此目标有任何想法吗?
我试过了
$wd = new-object -comobject word.application
$wd.run('PrintAll', '\\serverX\g$\xxxxxxxx')
$wd.quit()
但是出现错误“参数:'2'应该是System.Management.Automation.PSReference。使用[ref]。”
好的,我已经测试过了。
1)请转到VBA环境并编写此代码(哦,天哪,这是vb6)
Public Function PrintAll(arg1 As String, arg2 As String) As String
MsgBox arg1 + arg2
PrintAll = arg1 + arg2
End Function
2)转到PowerShell并编写以下代码:
$word = New-Object -ComObject Word.Application
$doc = $word.Documents.Open("C:\Users\Ali\Desktop\Files\A.docx")
$arg1=[ref]"Hello"
$arg2=[ref]",Buddy"
$word.Run("PrintAll",$arg1,$arg2) # The returned value is Hello,Buddy
$doc.close()
$word.quit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.