Solved

Insert new sheet into an existing excel file

Posted on 2012-03-14
4
1,709 Views
Last Modified: 2012-03-22
Hello,

Excel version 2010

I am preparing a script to insert a new sheet into an existing Excel file. The code is such:

$xl=New-Object -Com Excel.Application
$xl.Visible=$true
$wb=$xl.Workbooks.Open('c:\preparacion\prueba.xlsx')
$worksheet = $wb.worksheets.add()

I am not able to insert the new sheet in the FIRST POSITION. When I try add(1) I get an error.

Any idea to achieve my goal?
0
Comment
Question by:projectja
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 37724603
The default behaviour for me is that it creates the sheet in index position 1


This works for me .

#excel tabs
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
$testfile = "C:\Joek\Scripts\Experts-exchange\testfile.xlsx"
$xl=New-Object -Com Excel.Application
$xl.Visible=$true
$wb=$xl.Workbooks.Open($testfile)
$worksheet = $wb.worksheets.add()
#$worksheet.move($worksheet.sheets.item(1) )  # move to index position 1
$wb.sheets.item($worksheet.name).move($wb.sheets.item(1) ) #  move "sheet3" to position 1
$worksheet.name="Now in Index 1"
$wb.WorkSheets.Item("Now in Index 1").Tab.Color = [System.Drawing.Color]::Black.ToArgb() 
$xl.workbooks.save
$xl.visible=$false
$xl.quit()

Open in new window


Let me know how you get on
Joe
0
 

Accepted Solution

by:
projectja earned 0 total points
ID: 37732363
Thanks very much. It is very useful
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 37732380
if this doent work post the results to allow me to fix it ,   this has been tested and works fine.
0
 

Author Closing Comment

by:projectja
ID: 37751414
It does work and I can apply to my enviroment
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question