cmdlet在命令管道位置1处的New-Object为以下参数提供值:TypeName:

时间:2019-04-17 04:23:44

标签: excel powershell

我过去曾使用此强大的Shell代码将excel工作簿拆分为多个工作簿。该代码已适用于过去。但是,我现在收到一条错误消息。

  

cmdlet在命令管道位置1处的New-Object提供以下值   以下参数:   类型名称:

此脚本有什么问题?

Function Get-FileName 
{ 
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null   
$SaveFileDialog = New-Object 
System.Windows.Forms.OpenFileDialog 
$SaveFileDialog.filter = "Excel Files (*.xls,*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*"
$SaveFileDialog.ShowDialog() | Out-Null 
$SaveFileDialog.filename 
} 
Function Get-FolderName() 
{ 
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null   
$OpenFileDialog = New-Object 
System.Windows.Forms.FolderBrowserDialog 
$OpenFileDialog.ShowDialog() | Out-Null 
$OpenFileDialog.SelectedPath 
}
#Get filename to save results to from dialog 
$filename = Get-FileName 
$outputfolder = Get-FolderName   
if($filename -eq "" -or $outputfolder -eq "") 
{
 break; 
 }   
 cls 
 Write-Host Parser running, please do not work with Excel while this is running...  
#Open Report 
$xl=New-Object -COM Excel.Application 
$wb = $xl.Workbooks.Open($filename) 
$xl.DisplayAlerts = $false 
$xl.Visible = $false   
$wsAct = $wb.worksheets.Item(1) 
$wsAct.activate()   
$RowCnt = $wsAct.UsedRange.Rows.Count   
$CurPerson = "" 
$PrevPerson = "prev" 
$NewWBOpen = $false   
for($index=2; $index -le $RowCnt; $index++) 
{ 
cls 
$out = "Processing Row " + $index + " of " + $RowCnt
$CurPerson = $wsAct.Cells($index,3).Value2 #3 references the column we want to index by. 
$out 
$CurPerson   
if($CurPerson -ne $PrevPerson) 
{
 #Determine if there was a WB open that needs saved 
 if($NewWBOpen -eq $true) 
 {
#Save OpenWB before creating a new one 
$fileName1 = $outputfolder + "\" + $svFileName   
$wb2.SaveAs($fileName1) 
$wb2.Close() 
}
#Create new WB with header row 
$wb2 = $xl.Workbooks.Add() 
$wsAct2 = $wb2.Worksheets.Item(1) 
#$wb2.Colors() = $wb.Colors()
$headerRange = $wsAct.Range("A1").EntireRow 
$headerRange.Copy()|out-null   
$HeaderRange = $wsAct2.Range("A1") 
$wsAct2.Paste($headerRange)  
#Add record to new WB 
$cellRange = $wsAct.Cells($index,1).EntireRow 
$cellRange.Copy()|out-null  
$CellRange = $wsAct2.Range("A2") 
$wsAct2.Paste($cellRange)   
$NewWBOpen = $true 
} 
else 
{ 
#Add to open WB 
$RowCnt1 = $wsAct2.UsedRange.Rows.Count + 1   
$cellRange = $wsAct.Cells($index,1).EntireRow 
$cellRange.Copy()|out-null   
$CellRange = $wsAct2.Range("A" + $RowCnt1) 
$wsAct2.Paste($cellRange) 
}   
$PrevPerson = $CurPerson 
$svFileName = $wsAct.Cells($index,2).Value2 
#will use this index to create the file name 
}
#Save OpenWB for last person 
$fileName1 = $outputfolder + "\" + $svFileName   
$wb2.SaveAs($fileName1) 
$wb2.Close() 
$wb.Close 
$xl.Quit()   
cls 
Write-Host Database Parsing Complete!

运行代码时,我收到以下错误消息。我需要做什么来修复脚本?

enter image description here

1 个答案:

答案 0 :(得分:0)

删除New-ObjectSystem.Windows.Forms.OpenFileDialog之间的新行:

Function Get-FileName 
{ 
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null   
$SaveFileDialog = New-Object System.Windows.Forms.OpenFileDialog 
$SaveFileDialog.filter = "Excel Files (*.xls,*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*"
$SaveFileDialog.ShowDialog() | Out-Null 
$SaveFileDialog.filename 
} 

New-Object始终是行中的最后一件事,这是一个问题,因为PowerShell需要更多地了解您正在创建的新对象。

我看到您的代码中的两个函数都需要对其进行修复。