How to add append new Sheet in Excel?

Post Reply
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

How to add append new Sheet in Excel?

Post by hua »

Guys, by default Excel creates 3 empty sheets, how do I add 2 more to the end of them so I end with Sheet1, Sheet2, Sheet3, Sheet4 and Sheet5 in sequence?

TIA
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
User avatar
Enrico Maria Giordano
Posts: 8753
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: How to add append new Sheet in Excel?

Post by Enrico Maria Giordano »

A sample:

Code: Select all | Expand

FUNCTION MAIN()

    LOCAL oExcel := CREATEOBJECT( "Excel.Application" )

    LOCAL oSheet

    oExcel:Workbooks:Add()

    oSheet = oExcel:Sheets:Add( oExcel:Sheets[ oExcel:Sheets:Count ] )

    oExcel:Sheets[ oExcel:Sheets:Count ]:Move( oSheet )

    oSheet:Name = "Added"

    oExcel:Visible = .T.

    RETURN NIL


EMG
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Re: How to add append new Sheet in Excel?

Post by hua »

Thanks for the prompt response Enrico. Your solution has helped me.

Out of curiosity, can't we pass a parameter to :add() to indicate where the new sheet should be added?
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Re: How to add append new Sheet in Excel?

Post by hua »

Based on the link you've given (thanks!) and some tutorial here I try coding this way,

Code: Select all | Expand

   oExcel:Sheets:Add(, oExcel:Sheets[ oExcel:Sheets:Count() ], 2 )


Seems to generate runtime error on my PC though. Couldn't get it to work
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Re: How to add append new Sheet in Excel?

Post by hua »

Found an old post of yours Enrico - https://groups.google.com/forum/?fromgr ... %5B1-25%5D

Is OleDefaultArg() available in both Harbour and xHarbour?
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
User avatar
Enrico Maria Giordano
Posts: 8753
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: How to add append new Sheet in Excel?

Post by Enrico Maria Giordano »

xHarbour, for sure. Harbour, don't know, sorry.

EMG
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Re: How to add append new Sheet in Excel?

Post by hua »

No problem. Thanks for your help and time
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
Post Reply