2016-08-02 83 views
-1

以下程序在文件夾位置「c:\ mfolder」中的一組文件上運行PowerShell中名爲「Macro1」的Excel VBA宏。我怎樣才能複製它的Word VBA宏?Word VBA宏通過Powershell

***** runexcel.ps1 ******

$excel = new-object -comobject excel.application 
$excelFiles = Get-ChildItem -Path C:\mfolder -Include *.xls -Recurse 
Foreach($file in $excelFiles) 
{ 
    $workbook = $excel.workbooks.open($file.fullname) 
    $worksheet = $workbook.worksheets.item(1) 
    $excel.Run("Macro1") 
    $workbook.save() 
    $workbook.close() 
} 
$excel.quit() 
+0

請閱讀此:http://stackoverflow.com/help/how-to-ask –

回答

1

要通過PowerShell的打開微軟的Word,請使用以下命令:

$word = new-object –comobject Word.Application 

在你的循環,使用此打開每個文件:

$doc = $word.documents.open($file.fullname) 

您應該能夠根據您提供的腳本調整其餘部分。