我过去曾使用过这个power shell代码将excel工作簿拆分成多个工作簿。代码已经过去了。但是,我现在收到一条错误消息。
cmdlet命令管道位置1处的新对象提供以下参数的值:TypeName:
这个脚本有什么问题?
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!
当我运行代码时,我收到以下错误消息。我需要做什么来修复脚本?
删除New-Object
和System.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需要更多地了解你正在创建的新对象。
我可以看到它需要在代码中的两个函数中修复。