Just to stres out once more how objcts are addressed (in any language, that's not a VFP specific thing):
There are root objects, like THISFORM, THIS, or a variable containing an object reference, like loExcel once you created the automation object with loExcel = CreateObject('Excel.Application')
You address propertis of such objects via -> or th dot, most languages now us the shorter dot, -> is very speciic ti C and C++.
You can addreess propreties of an object with thing like
Code:
THISFORM[highlight #FCE94F].Caption[/highlight] = 'My Window Caption'
Mssagebox(THIS[highlight #FCE94F].Name[/highlight])
And you can call methods of objects the same way
Code:
THISFORM[highlight #FCE94F].Release()[/highlight]
For nested objects (for example a textbox in a page of a pageframe on a form you can have multiple dots, like this:
Code:
THISFORM.PageFrame1.Page2.Text1.Value = 'Hello, World'
It's the same in the Object model of Excel automation, you have he root Application object, and in case of Excel that has a collection of Workbooks, which each have a collection of Sheets, etc.
You don't want to write code like
Code:
loRange1 = loExcel.ActiveWorkbook.ActiveSheet.Range('A1:C3')
loRange2 = loExcel.ActiveWorkbook.ActiveSheet.Range('D1:F3')
WITH helps to reduce what you need to write out:
Code:
WITH loExcel.ActiveWorkbook.ActiveSheet
loRange1 = .Range('A1:C3')
loRange2 = .Range('D1:F3')
ENDWITH
And this can also be nested. you coul want to addresss both the Activesheet (to add something there) and then the ActiveWorkbook, to save it, so you'd nest the WITH statements like this:
Code:
WITH loExcel.ActiveWorkbook
WITH .ActiveSheet
loRange1 = .Range('A1:C3')
loRange2 = .Range('D1:F3')
...do something with the ranges
ENDWITH
.SaveAs('MyWorkbook.xlsx')
ENDWITH
If you never need to address another object than the ActiveSheet, you can also jump right in to the Sheet object in one WITH:
Code:
WITH loExcel.ActiveWorkbook..ActiveSheet
loRange1 = .Range('A1:C3')
loRange2 = .Range('D1:F3')
...do something with the ranges
ENDWITH
Your code snippet can't be working in itself, it must be embedded in another WITH, as ActivWorkbook is no root object. At least not to VFP. For Excel automation, the only root object is the application object, anything else is subordinated to it.
Chriss