Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Excel OLE Automation
Message
From
28/06/2002 07:21:29
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00672838
Message ID:
00673204
Views:
24
>Hi,
>
>Rick Strahl had a very interesting article on Web charting, but I could not download the Microsoft class required for it - temporary unavailable.......
>So I trying the "good old" Excel automation for charting.
>
>A collegue of mine created a complicated Excel Workbook template with a few tabs with charts.
>
>Then, I created a routine from my appication which creates an Excel Workbook.
>
>The first step of the OLE automation is to copy a worksheet from one workbook to another:
>***************************
>#DEFINE True .T.
>#DEFINE False .F.
>LOCAL lo1Excel, lo2Excel, lcOldError
>
>lcOldError = ON("ERROR")
>ON ERROR lo1Excel = .NULL.
>lo1Excel = GetObject("C:\ecr_rpt.xls","Excel.Application")
>
>IF ISNULL(lo1Excel)
>lo1Excel = CreateObject("Excel.Application")
>lo1Excel.Application.Workbooks.Open("C:\ecr_rpt.xls")
>ENDIF
>
>WITH lo1Excel.Application
>.Visible = .T.
>.Windows("ecr_rpt.xls").Activate
>.WorkSheets("ecr_rpt").Select
>.ActiveSheet.Copy
>ENDWITH
>
>lo2Excel = GetObject(gcDrv+":\cfg\ECR_Report.xls")
>RELEASE lo1Excel
>WITH lo2Excel.Application
>.Windows("ECR_Report.xls").Activate
>.WorkSheets("ecr_rpt").Select
>.ActiveSheet.Paste
>ENDWITH
>
>ON ERROR &lcOldError
>*****************************
>
>The above did not do what it was supposed to do and also left an instance of OLE hanging.
>
>Any ideas?
>
>
>Thanks, Jonathan

Jonathan,
There are few things to note. If we start with your leaving an instance, lo2excel is instantiated but never made visible nor quited.

>ON ERROR lo1Excel = .NULL.
>lo1Excel = GetObject("C:\ecr_rpt.xls","Excel.Application")
>
>IF ISNULL(lo1Excel)
>lo1Excel = CreateObject("Excel.Application")
>lo1Excel.Application.Workbooks.Open("C:\ecr_rpt.xls")
>ENDIF

FWIW never instantiate excel with getobject. Though getobject exists and valid in cases, it's open to inconsistencies.

>.WorkSheets("ecr_rpt").Select
>.ActiveSheet.Copy

Selecting a sheet doesn't necessarily make it the active one. Use activate instead.

You could achieve what you want in multiple ways :
ie :
lcXLS1 = "C:\ecr_rpt.xls"
lcXLS2 = gcDrv+":\cfg\ECR_Report.xls"

LOCAL loExcel
loExcel = CreateObject("Excel.Application")
* Method 1
with loExcel
 .Workbooks.Open(lcXLS1)
 .Workbooks('ecr_rpt.xls').WorkSheets("ecr_rpt").UsedRange.Copy
 .Workbooks.Open(lcXLS2)
 .Workbooks('ecr_report.xls').WorkSheets("ecr_rpt").Range('A1').PasteSpecial()
 .Visible = .T.
ENDWITH

*Method 2
with loExcel
 .Workbooks.Open(lcXLS1)
 .Workbooks.Open(lcXLS2)
 .Workbooks('ecr_rpt.xls').WorkSheets("ecr_rpt").UsedRange.Copy( ;
   .Workbooks('ecr_report.xls').WorkSheets("ecr_rpt").Range('A1') )
 .Visible = .T.
ENDWITH
There are other longer ways too.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform