Combine 6 .xlsx files to into 1 .xlsx file with 6 sheets

Hello

Is something like this possible to do with powershell? I'm using excel 2013. The script should create the 1 excel file by itself.

Thank you.

February 7th, 2015 3:14pm

If you search you will find many examples of how to copy worksheets between workbooks.

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 4:28pm

Here is one simlpe example:

$xl = new-object -c excel.application
$xl.displayAlerts = $false
$source = $xl.workbooks.open("$pwd\book1.xlsx")
$target = $xl.workbooks.open("$pwd\book2.xlsx")
$source.sheets.item('sheet1').copy($target.Worksheets.Item(1))
$source.close($false)
$target.close($true) 
$xl.quit()

February 7th, 2015 4:50pm

Hello

The problem with google this stuff.. it never works. And neither does that one :(

Exception getting "Item": "Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))"
At C:\Users\x\Documents\x\x\Data\xxx.ps1:177 char:1
+ $source.sheets.item('sheet1').copy($target.Worksheets.Item(1))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], GetValueInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterParameterizedPropertyGetValueTI

Also noticed it doesn't create target by itself.

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 6:21pm

I cannot help you to understand the code I can only show it to you.  If you don't know Excel or the object model then it is going to be very hard for you to write this script.

The script does not combine six arbitrary files. It copies one sheet named "sheet1" from one wookbook to another. The second workbook has to have one sheet minimum.

Example:

# get first sheet in source and rename then copy to target.
$source.sheets.item(1).Name='NewTest'
$source.sheets.item('NewTest').copy($target.Worksheets.Item(1))

If your workbooks do not conform then you will have an issue.

If you cannot understand how to use this information or the meanning of the errors then I recommend hiring a consultant to help you.

You are asking for a large script.  We don't write scripts on demand. Sorry.

February 7th, 2015 8:40pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics