?
Solved

Insert new sheet into an existing excel file

Posted on 2012-03-14
4
Medium Priority
?
1,787 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

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A brief introduction to what I consider to be the best editor for PowerShell.
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

752 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