Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How to open new version of excel (using 2003 and 2010) in Powershell and import txt data into excel file?

$
0
0

Hello,

I use the code below to import txt file into excel. The problem is that I use 2 versions of Office (2003 and 2010).

The command $Excel = New-Object -ComObject excel.application opens Excel 2003

I need to use Excel 2010 instead of 2003 and import new data.

My Excel 2010 is stored in C:\Program Files\Microsoft Office\Office14\excel.exe

 

Thank you for any help.

 

Steve

 

 

 

 

$CSVfile = "E:\test.txt"

    #Create a new Excel object and add a new workbook.
    #Delete the extra worksheets and rename the first worksheet.
    $Excel = New-Object -ComObject excel.application
     $Excel.visible = $true
    $workbooks = $excel.Workbooks.Add()
    $worksheets = $workbooks.worksheets
    $worksheets.Item(3).delete()
    $worksheets.Item(2).delete()
    $worksheet = $worksheets.Item(1)
    $worksheet.Name = "Name of Worksheet"
 
    #Define the connection string and where the data is supposed to go
    $TxtConnector = ("TEXT;" + $CSVfile)
    $CellRef = $worksheet.Range("A1")

    #Build, use and remove the text file connector
    $Connector = $worksheet.QueryTables.add($TxtConnector,$CellRef)
    $worksheet.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True
    $worksheet.QueryTables.item($Connector.name).TextFileParseType  = 1
    $worksheet.QueryTables.item($Connector.name).Refresh()
    $worksheet.QueryTables.item($Connector.name).delete()

    $worksheet.UsedRange.EntireColumn.AutoFit()


Viewing all articles
Browse latest Browse all 6937

Trending Articles