[英]Get active Excel worksheet name/index number using Powershell
I am trying to open an Excel file using Powershell, and then get the name (or index number) of the active worksheet.我正在尝试使用 Powershell 打开 Excel 文件,然后获取活动工作表的名称(或索引号)。
(The end goal is to print/export the active worksheet as a PDF.) (最终目标是将活动工作表打印/导出为 PDF。)
$ExcelFileName = 'C:\Users\Me\Desktop\graph.xlsx'
# creates path to excel file
$Excel = New-Object -COMObject Excel.Application
# creates an excel com object
$Excel.Workbooks.Open($ExcelFileName)
# opens the workbook file
$WorkSheetName = $excel.WorksheetView.Sheet
# supposedly returns the name of the active work sheet
$WorkSheetObject = $excel.Workbooks.Item($WorkSheetName)
# error happens here when trying to create worksheet object
$WorkSheetObject.ExportAsFixedFormat(0,[ref]$NewFileName)
# this code would export worksheet to pdf
This code produces an error (see below).此代码产生错误(见下文)。 The Workbooks.Item method seems to require the sheet name as a string or integer, which WorksheetView.Sheet is not returning.
Workbooks.Item 方法似乎需要工作表名称作为字符串或 integer,WorksheetView.Sheet 没有返回。 Maybe.
也许。
Invalid Index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
At line:9 char:5
+ $WorkSheetObject = $excel.Workbooks.Item($WorkSheetName))
I've also tried using these lines of code to return the active sheet name or index number, but I get the same error when I pass them as the $WorkSheetName (see below).我也尝试使用这些代码行来返回活动工作表名称或索引号,但是当我将它们作为 $WorkSheetName 传递时,我得到了同样的错误(见下文)。
$ActiveSheet = $excel.Workbook.ActiveSheet
$WorkSheetName = $ActiveSheet.name
$WorkSheetObject = $excel.Workbooks.Item($WorkSheetName)
I haven't found anything else to answer this in the Microsoft docs or on forums, so thanks for any help you can offer.我没有在 Microsoft 文档或论坛中找到任何其他可以回答此问题的内容,因此感谢您提供的任何帮助。
You have to mine for what you are after if you do not know the full namespace already.如果你还不知道完整的命名空间,你必须挖掘你想要的东西。 For Example:
例如:
$ExcelFileName = 'D:\temp\Test.xlsx'
$ExcelApp = New-Object -ComObject Excel.Application
$ExcelApp.Workbooks.Open($ExcelFileName)
# Results
<#
...
ActiveChart :
ActiveSheet : System.__ComObject
...
#>
$ExcelApp.Work
# Results
<#
Workbooks WorkbookAddinInstall WorkbookAfterXmlImport WorkbookBeforeXmlExport WorkbookNewChart WorkbookPivotTableOpenConnection
WorksheetFunction WorkbookAddinUninstall WorkbookBeforeClose WorkbookBeforeXmlImport WorkbookNewSheet WorkbookRowsetComplete
Worksheets WorkbookAfterSave WorkbookBeforePrint WorkbookDeactivate WorkbookOpen WorkbookSync
WorkbookActivate WorkbookAfterXmlExport WorkbookBeforeSave WorkbookModelChange WorkbookPivotTableCloseConnection
Microsoft.Office.Interop.Excel.Workbooks Workbooks {get;}
#>
<#
Note that there is no property called '$ExcelApp.WorksheetView.Sheet' at this level of the Excel COM.
So all the remaining code does not matter.
However, there is,
#>
$ExcelApp.Worksheets
# Results
<#
Worksheets
#>
# So,
$ExcelApp.Active
<#
ActiveCell ActiveDialog ActiveMenuBar ActiveProtectedViewWindow ActiveWindow ActivateMicrosoftApp
ActiveChart ActiveEncryptionSession ActivePrinter ActiveSheet ActiveWorkbook
Microsoft.Office.Interop.Excel.Range ActiveCell {get;}
#>
# Now, digging further
$ExcelApp.ActiveSheet.
# Results
<#
_AutoFilter EnableFormatConditionsCalculation Previous __PrintOut Drawings PrintOut
_CodeName EnableOutlining PrintedCommentPages __SaveAs DropDowns PrintPreview
_DisplayRightToLeft EnablePivotTable ProtectContents _CheckSpelling Equals Protect
_Sort EnableSelection ProtectDrawingObjects _Evaluate Evaluate Rectangles
Application FilterMode Protection _ExportAsFixedFormat ExportAsFixedFormat ResetAllPageBreaks
AutoFilter HPageBreaks ProtectionMode _PasteSpecial GetHashCode SaveAs
AutoFilterMode Hyperlinks ProtectScenarios _PrintOut GetLifetimeService Scenarios
Cells Index QueryTables _Protect GetType ScrollBars
CircularReference ListObjects Rows _SaveAs GroupBoxes Select
CodeName MailEnvelope Scripts Activate GroupObjects SetBackgroundPicture
Columns Name ScrollArea Arcs InitializeLifetimeService ShowAllData
Comments NamedSheetViews Shapes Buttons Labels ShowDataForm
CommentsThreaded Names SmartTags Calculate Lines Spinners
ConsolidationFunction Next Sort ChartObjects ListBoxes TextBoxes
ConsolidationOptions OnCalculate StandardHeight CheckBoxes Move ToString
ConsolidationSources OnData StandardWidth CheckSpelling OLEObjects Unprotect
Creator OnDoubleClick Tab CircleInvalid OptionButtons XmlDataQuery
CustomProperties OnEntry TransitionExpEval ClearArrows Ovals XmlMapQuery
DisplayAutomaticPageBreaks OnSheetActivate TransitionFormEntry ClearCircles Paste Range
DisplayPageBreaks OnSheetDeactivate Type Copy PasteSpecial
DisplayRightToLeft Outline UsedRange CreateObjRef Pictures
EnableAutoFilter PageSetup Visible Delete PivotTables
EnableCalculation Parent VPageBreaks DrawingObjects PivotTableWizard
AutoFilter _AutoFilter () {get}
#>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.