Using Powershell to loop through Excel files and check if Spreadsheet name exists

后端 未结 2 565
情话喂你
情话喂你 2021-01-19 05:09

I\'m trying to write a powershell script that will loop through each excel file in the given directory, check the file for a specifically named worksheet, and then copy that

2条回答
  •  灰色年华
    2021-01-19 05:55

    You don't need this line:

    [void][reflection.assembly]::Loadwithpartialname("microsoft.office.excel")
    

    ($Excel = New-Object -ComObject Excel.Application is sufficient here)

    I don't think you're referencing the full path to your Excel files. Try modifying this line:

    $WorkBook = $Excel.Workbooks.Open($file)
    

    Amend to:

    $WorkBook = $Excel.Workbooks.Open($file.Fullname)
    

    Additionally, consider adding a filter to your Get-ChildItem command, if there are sub-directories or non-Excel files, they will cause errors:

    $files = Get-ChildItem C:\Test -filter "*.xls"
    

提交回复
热议问题