我试图使用一个变量来设置一个带有Powershell的excel工作表名,但是得到了以下错误:
Exception from HRESULT: 0x800A03EC
At line:14 char:1
+ $ws.Name = $RequestTitle
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException下面是代码的一个片段:
# create new excel object
$excel = New-Object -ComObject excel.application
# show the excel object
$excel.visible = $True
# add a workbook
$workbook = $excel.Workbooks.Add()
# create a variable referencing the first sheet of the wb
$ws= $workbook.Worksheets.Item(1)
# change the name of the sheet
$ws.Name = $NewSheetName但是,当我做这样的事情:$ws.Name = "some string",它工作良好,没有错误。我也尝试过使用$NewSheetName.ToString()方法,但没有结果。
引用变量来命名工作表的最佳方法是什么?任何帮助都将不胜感激!
发布于 2020-02-13 19:38:25
使用这样的语句就可以解决这个问题。将引号放在变量名的周围。
# change the name of the sheet
$ws.Name = "$NewSheetName"我不知道为什么,但是excel工作表名需要一个引号中的字符串,即使使用一个明显是字符串的变量。
Note
使用powershell v5和Office 2013进行测试,Office 365,没有引用的$variable代码工作良好。不确定是什么导致了需要在变量名周围使用double quotes的问题。
https://stackoverflow.com/questions/60215091
复制相似问题