I've got a powershell script that does a weekly compare of 2 large files, then divides the results up to create 9 or 10 CSV files. I want to take those files and import the data into an XLSX file in one step.
$Excel = New-Object -ComObject excel.application $Excel.visible = $true $workbooks = $excel.Workbooks.Add() $worksheets = $workbooks.worksheets $worksheets.add() $worksheets.add() $worksheets.add() $worksheets.add() $worksheets.add() $worksheets.add() $worksheets.add() $worksheets.add() $worksheets.add() $worksheet1 = $workbooks.sheets.Item(1) $worksheet1.Name = "All" $worksheet2 = $workbooks.sheets.Item(2) $worksheet2.Name = "UAC" $worksheet3 = $workbooks.sheets.Item(3) $worksheet3.Name = "AS" $worksheet4 = $workbooks.sheets.Item(4) $worksheet4.Name = "BT" $worksheet5 = $workbooks.sheets.Item(5) $worksheet5.Name = "CR" $worksheet6 = $workbooks.sheets.Item(6) $worksheet6.Name = "CS" $worksheet7 = $workbooks.sheets.Item(7) $worksheet7.Name = "CE" $worksheet8 = $workbooks.sheets.Item(8) $worksheet8.Name = "CN" $worksheet9 = $workbooks.sheets.Item(9) $worksheet9.Name = "PH" $worksheet10 = $workbooks.sheets.Item(10) $worksheet10.Name = "Other" #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportfileexcel) $CellRef = $worksheet1.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet1.QueryTables.add($TxtConnector,$CellRef) $worksheet1.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet1.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet1.QueryTables.item($Connector.name).Refresh() $worksheet1.QueryTables.item($Connector.name).delete() $worksheet1.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector2 = ("TEXT;" + $export00excel) $CellRef2 = $worksheet2.Range("A1") #Build, use and remove the text file connector $Connector2 = $worksheet2.QueryTables.add($TxtConnector2,$CellRef2) $worksheet2.QueryTables.item($Connector2.name).TextFileCommaDelimiter = $True $worksheet2.QueryTables.item($Connector2.name).TextFileParseType = 1 $worksheet2.QueryTables.item($Connector2.name).Refresh() $worksheet2.QueryTables.item($Connector2.name).delete() $worksheet2.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportASexcel) $CellRef = $worksheet3.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet3.QueryTables.add($TxtConnector,$CellRef) $worksheet3.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet3.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet3.QueryTables.item($Connector.name).Refresh() $worksheet3.QueryTables.item($Connector.name).delete() $worksheet3.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportBTexcel) $CellRef = $worksheet4.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet4.QueryTables.add($TxtConnector,$CellRef) $worksheet4.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet4.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet4.QueryTables.item($Connector.name).Refresh() $worksheet4.QueryTables.item($Connector.name).delete() $worksheet4.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportCRexcel) $CellRef = $worksheet5.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet5.QueryTables.add($TxtConnector,$CellRef) $worksheet5.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet5.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet5.QueryTables.item($Connector.name).Refresh() $worksheet5.QueryTables.item($Connector.name).delete() $worksheet5.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportCSexcel) $CellRef = $worksheet6.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet6.QueryTables.add($TxtConnector,$CellRef) $worksheet6.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet6.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet6.QueryTables.item($Connector.name).Refresh() $worksheet6.QueryTables.item($Connector.name).delete() $worksheet6.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportEDexcel) $CellRef = $worksheet7.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet7.QueryTables.add($TxtConnector,$CellRef) $worksheet7.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet7.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet7.QueryTables.item($Connector.name).Refresh() $worksheet7.QueryTables.item($Connector.name).delete() $worksheet7.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportNUexcel) $CellRef = $worksheet8.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet8.QueryTables.add($TxtConnector,$CellRef) $worksheet8.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet8.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet8.QueryTables.item($Connector.name).Refresh() $worksheet8.QueryTables.item($Connector.name).delete() $worksheet8.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportPUexcel) $CellRef = $worksheet9.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet9.QueryTables.add($TxtConnector,$CellRef) $worksheet9.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet9.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet9.QueryTables.item($Connector.name).Refresh() $worksheet9.QueryTables.item($Connector.name).delete() $worksheet9.UsedRange.EntireColumn.AutoFit() #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $exportOTexcel) $CellRef = $worksheet10.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet10.QueryTables.add($TxtConnector,$CellRef) $worksheet10.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet10.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet10.QueryTables.item($Connector.name).Refresh() $worksheet10.QueryTables.item($Connector.name).delete() $worksheet10.UsedRange.EntireColumn.AutoFit()
This script opens Excel and creates/renames all of the tabs, then populates the first worksheet with the correct data. However, the rest of the worksheets don't import and I get these errors:
"Exception calling "Add" with "2" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))"