【问题标题】:Powershell Run Multiple vba scripts in excel workbook across multiple worksheetsPowershell在Excel工作簿中跨多个工作表运行多个vba脚本
【发布时间】:2014-02-10 16:28:52
【问题描述】:

您好,我需要在同一个工作簿的不同工作表上运行不同的 vba 脚本。基本上,每个工作表都有自己的 vba 脚本,它触发 ODBC 连接,然后从数据库更新工作表。我已经能够让一个 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 脚本时,它根本不起作用。

【问题讨论】:

标签: excel powershell worksheet vba


【解决方案1】:

在离开这个问题一会儿之后,再喝点咖啡并应用一些逻辑,就OK了。我得到了工作。因此,以防万一您需要一个脚本来执行我所追求的工作并在同一工作簿中的特定工作表上运行特定宏,就在这里。

$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()

【讨论】:

  • 顺便说一句,如果您愿意,您可以在调用时实际使用电子表格名称/标签。 "$ws1=$workbook.worksheets.item("Sheet1")" 比数很多时更容易。
  • 有时保存 xlsm 可能是一个问题,您可能会收到警告,例如无法使用此扩展名保存文件,以克服从文件名中删除扩展名并将文件类型指定为第二个参数的问题,例如:$workbook .saveas($indexTemplate,52) ,这里 52 是启用宏的文件类型,即 xlsm
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多