我的目標: 創建一個新選項卡並將其命名爲函數。在函數中創建時無法命名Excel選項卡
我的問題: 當我在函數內部創建新選項卡時,我無法命名該選項卡。
備註: 當我在該函數的外部創建選項卡時,它會正確命名。
我的代碼:
Function Function_CreateWorksheets($Worksheets) {
ForEach ($Worksheet in $Worksheets) {
$Excel_Count_Worksheet++
If ($Excel_Count_Worksheet -gt 3) {$Script:Excel.Worksheets.Add() |Out-Null}
$Script:Excel.Worksheets.Item($Excel_Count_Worksheet).Name = $Worksheet
}
}
#Load-Module -Module grouppolicy -Reload
Get-Variable Excel_* |Remove-Variable -Force
#Create Excel Com Object
$Excel = New-Object -com Excel.Application
# Make the Excel Application Visible to the end user
$Excel.visible = $True
# Create a WorkBook inside the Excel application
# that we can start manipulating.
$Excel_Workbook = $Excel.Workbooks.Add()
$Action_CreateWorksheet =
#=======================================================
# Now that we have a workbook we need to create some
# additional worksheets (Tabs) beyond that initial 3
# that are created when the workbook is opened.
#=======================================================
#$Temp_MakeWorkSheet = $Excel.Worksheets.Add()
#=======================================================
# Once all the sheets are created each of the worksheets
# need to be assigned to a variable so they can be
# manipulated.
#=======================================================
Function_CreateWorksheets -Worksheets "System Summary","Test1","Test2","Test3","Test4"
其中一個最好,最有答案我得到了,非常感謝你!在一個側面說明我從來沒有新的命令記錄像以前那樣。希望我能用它來阻止我在未來提出類似的問題。 – user1451070
請注意更新 - 我忘記了在循環之前禁用警報以刪除默認工作表,以避免提示您確認三次,然後重新啓用它們。 –