首页 > 解决方案 > 打开具有特定扩展名的文件

问题描述

使用 PowerShell,我想使用 * 而不是文件名打开一个 excel。下面的代码在第 2 行失败。正在寻找扩展名为 *.xlsx 的文件

$excel = New-Object -comobject Excel.Application
$FilePath_Excel = "C:\Users\Documents\FILES\*.xlsx"
$workbook2 = $excel.Workbooks.Open($FilePath_Excel)
$app = $excel.Application
$app.Run("Jan.xlsm!EOL")
$Excel.Workbooks.Close()
$excel.Quit()

出现以下错误

Sorry, we couldn't find C:\Users\Documents\FILES\*.xlsx. Is it possible it was moved, renamed or 
deleted?
At C:\Users\Documents\FILES\Untitled1.ps1:7 char:1
+ $workbook2 = $excel.Workbooks.Open($FilePath_Excel)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

标签: excelvbapowershellglob

解决方案


推荐阅读