2014-02-10 114 views
0

嗨我需要在同一工作簿中的不同工作表上運行不同的vba腳本。基本上,每個工作表都有自己的vba腳本,它觸發ODBC連接,然後從數據庫更新工作表。我已經能夠獲得一個vba腳本在一張紙上運行並保存爲...沒問題,但不能再運行一個。這裏是我使用的代碼Powershell跨多個工作表在Excel工作簿中運行多個vba腳本

$excel = new-object -comobject excel.application 
$excelFiles = Get-ChildItem -Path C:\test\Daily_update.xlsm 
$Date = (Get-Date -Format dd-MM-yy) 
Foreach($file in $excelFiles) 
{ 
$workbook = $excel.workbooks.open($file.fullname) 
$worksheet = $workbook.worksheets.item(2) 
$excel.Run("Test_Refresh") 

$workbook.saveAs("C:\test\Daily_update_$Date.xlsm") 
$workbook.close() 
} 
$excel.quit() 

當我嘗試添加其他工作表和vba腳本它根本不起作用。

+0

基於此信息(http://blogs.technet.com/b/heyscriptingguy/archive/2009/01/13/how-do-i-run-an-office-excel-macro- on-multiple-workbooks.aspx),這應該有效。您可能需要向我們提供更多信息:您看到了什麼錯誤?在什麼時候? –

回答

1

確定一段時間後,走出問題一點,多一點咖啡,並應用一些邏輯。我有事要做。因此,如果您需要腳本來完成我之後的工作,並在同一工作簿中的特定工作表上運行特定的宏,那麼就是這樣。

$excel = new-object -comobject excel.application 
$excelFiles = Get-ChildItem -Path C:\Test\Daily_update.xlsm 
$Date = (Get-Date -Format dd-MM-yy) 

$workbook = $excel.workbooks.open($excelfiles.fullname) 
$WS2 = $workbook.worksheets.item(2) 
$WS2.Activate() 
$excel.Run("Test_Refresh") 

$WS3 = $workbook.worksheets.item(3) 
$WS3.Activate() 
$excel.Run("test_Refresh_2") 

$WS4 = $workbook.worksheets.item(4) 
$WS4.Activate() 
$excel.Run("test_Refresh_3") 

$workbook.saveas("C:\Test\SQL\Daily_update_$Date.xlsm") 
$workbook.close() 

$excel.quit() 

只是爲了增加一點點。經過一些試驗和錯誤之後,我發現下面的代碼在運行上比上面的要高效得多。另外我注意到,如果只有3張工作表,那很好,但是更多的時候開始出現錯誤,尤其是在調用工作表時。但是,當工作簿打開並可見時,此問題就消失了。

#Call the application 
$excel = new-object -comobject excel.application 
#Now we select the file and path 
$excelFiles = Get-ChildItem -Path "\\Server\Test\Daily_refresh.xlsm" 
#The next variable speeds the script up by not calling the comobject as often 
$app = $excel.Application 
#Get system date and time and format it to comply with the final filename format 
$Date = (Get-Date -Format dd-MM-yy) 
#And again for the year folder 
$Year = (Get-Date -Format yyyy) 
#Test if folder exists 
$DestYearFolder = "\\Server\Test\Daily_Refresh_Output\Test\$Year" 
if (!(Test-Path -path $DestYearFolder)) {New-Item $DestYearFolder -Type Directory} 
#Same as above only for the month folder 
$Month = (Get-Date -Format MMM) 
#Test if folder exists 
$DestMonthFolder = "\\Server\Test\Daily_Refresh_Output\Test\$Year\$Month" 
if (!(Test-Path -path $DestMonthFolder)) {New-Item $DestMonthFolder -Type Directory} 
#Now we open the Excel file and activate the macro enabled content 
$workbook = $app.workbooks.open($excelfiles) 
#The next command makes Excel visible 
$app.Visible = $true 
$workbook.Activate() 
#Now we run all the Macros that need to be run. 
$app.Run("Macro_1") 
$app.Run("Macro_2") 
$app.Run("Macro_3") 
$app.Run("Macro_4") 
$app.Run("Macro_5") 
$app.Run("Macro_6") 
$app.Run("Macro_7") 
$app.Run("Macro_8") 
$app.Run("Macro_9") 
$app.Run("Macro_10") 

#Now we save the workbook in the standard daily format and the close Excel 
$workbook.saveas("\\Server\Test\Daily_Refresh_Output\Test\$Year\$Month\Daily_Refresh_test_$Date.xlsm") 
$workbook.close() 

$excel.quit() 
+0

順便說一句,如果你想你可以實際使用電子表格名稱/標籤時,你打電話。 「$ ws1 = $ workbook.worksheets.item(」Sheet1「)」當你有很多人時比計算更容易。 – Wynn

+0

有時候保存xlsm可能是一個問題,您可能會收到類似文件的警告,無法使用此擴展名保存,以克服從文件名中刪除擴展名並將文件類型指定爲第二個參數的情況,例如:$ workbook.saveas($ indexTemplate,52 ),這裏52是啓用宏的文件類型,即xlsm –

相關問題