Page 1 of 1 1
Topic Options
#92842 - 2003-09-11 02:27 PM Excel.Application - Am too dumb to get my worksheets added AFTER Active Sheet
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Guys,

I need to log centrally some file monitorings from different Servers therefore need to add tons of worksheets to my workbook.

well, the default behavior is to add BEFORE the active sheet (simply stunning [Mad] ).
Allthough I found a help topic :

quote:
expression.Add(Before, After, Count, Type)
After Optional Variant. An object that specifies the sheet after which the new sheet is added.

I failed to get that straight [Embarrassed]

here a sample of how I don't what it to work :


break on

$file = @scriptdir + '\test.xls'
if exist($file)
del $file
endif

$xl = createobject ( "excel.application" )
$ = $xl.workbooks.add

for $i = 1 to 7
if $i > 3
$ = $xl.worksheets.add ;<--- this needs to be corrected
endif
$ = $xl.WorkSheets("Sheet"+$i).Activate
$xl.ActiveSheet.name = "A" + $i
$xl.range("C3").value = "writetosheet$i"
next

$ = $xl.ActiveWorkbook.SaveAs($file)
$xl.UserControl = 1
$ = $xl.quit


[ 11. September 2003, 14:32: Message edited by: Jochen ]
_________________________



Top
#92843 - 2003-09-11 02:30 PM Re: Excel.Application - Am too dumb to get my worksheets added AFTER Active Sheet
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Do I need an object reference to the Worksheet I want to add after ?
Completely lost allthough I know the solution must be simple...

[Frown] [Embarrassed] [Frown]
_________________________



Top
#92844 - 2003-09-11 02:56 PM Re: Excel.Application - Am too dumb to get my worksheets added AFTER Active Sheet
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
tried this:


$xl.worksheets.add(,$xl.ActiveSheet,,)


It doesn't even add sheets anymore :'(

[ 11. September 2003, 14:57: Message edited by: Jochen ]
_________________________



Top
#92845 - 2003-09-11 03:03 PM Re: Excel.Application - Am too dumb to get my worksheets added AFTER Active Sheet
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Here is my stab at it cappy. Plus, deleted those three default sheets at the end of script. What a sheet-head I am ja ?

code:
break on

$=setoption("wrapateol","on")

$file = @scriptdir + '\test.xls'
if exist($file)
del $file
endif

$xl = createobject ( "excel.application" )
$xl.UserControl = 1
$xl.DisplayAlerts = 0

$= $xl.workbooks.add

for $i = 1 to 7
if $s
$s = $xl.worksheets.add(,$s)
else
$s = $xl.worksheets.add()
endif
$xl.ActiveSheet.name = "A" + $i
$xl.range("C3").value = "writetosheet$i"
next

for $i = 1 to 3
$= $xl.ActiveWorkbook.Sheets("Sheet"+$i).Delete()
next

$ = $xl.ActiveWorkbook.SaveAs($file)
$ = $xl.quit


Top
#92846 - 2003-09-11 03:10 PM Re: Excel.Application - Am too dumb to get my worksheets added AFTER Active Sheet
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
So leaving out the last 2 optional options did that too ... [Eek!] [Roll Eyes] [Mad]

Big thanks Shawn (again!)

here's my approach which works too :


break on

$file = @scriptdir + '\test.xls'
if exist($file)
del $file
endif

$xl = createobject ( "excel.application" )
$ = $xl.workbooks.add

for $i = 1 to 7
if $i > 3
$_ = $xl.worksheets.add(,$xl.ActiveSheet)
endif
$_ = $xl.WorkSheets("Sheet"+$i).Activate
$xl.ActiveSheet.name = "A" + $i
$xl.range("C3").value = "writetosheet$i"
next

$ = $xl.ActiveWorkbook.SaveAs($file)
$xl.UserControl = 1
$ = $xl.quit
_________________________



Top
#92847 - 2003-09-11 08:26 PM Re: Excel.Application - Am too dumb to get my worksheets added AFTER Active Sheet
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
You could also use this:

$ = $oXl.WorkBooks.Add(-4167) ; Create a New workbook containing only 1 sheet

-Erik

Top
#92848 - 2003-09-11 09:31 PM Re: Excel.Application - Am too dumb to get my worksheets added AFTER Active Sheet
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Nice one!

Thanx Erik, will keep that in mind for future projects (Stuff already in production)
_________________________



Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 811 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.056 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org