Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Writing to an excel file
Message
De
27/05/2011 13:10:38
 
 
À
12/05/2011 09:15:43
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Database:
Visual FoxPro
Divers
Thread ID:
01510189
Message ID:
01512074
Vues:
43
>>>>>>Hi All:
>>>>>>
>>>>>>I'm trying to populate an Excel file with values using automation, but nothing gets written. What am I doing wrong?
>>>>>>
>>>>>>This is the code:
>>>>>>
>>>>>>
>>>>>>LOCAL loExcel AS Excel.APPLICATION
>>>>>>
>>>>>>loExcel = CREATEOBJECT("Excel.application")
>>>>>>loExcel.VISIBLE = .F.
>>>>>>
>>>>>>loWorkBook = loExcel.Workbooks.OPEN('P:\Flock Information.xls')
>>>>>>loExcel.displayalerts = .F.
>>>>>>
>>>>>>loWorkBook.Sheets("28 Day").SELECT
>>>>>>
>>>>>>FOR i = 288 TO 300
>>>>>>	legRow = 'M' + ALLTRIM(STR(i))
>>>>>>	loWorkBook.ActiveSheet.RANGE("&legRow").select
>>>>>>	loExcel.ActiveCell.Value = ALLTRIM(STR(i))   &&<--- This doesn't work
>>>>>>*!*		loWorkBook.ActiveSheet.RANGE("&legRow").Value = ALLTRIM(STR(i))  &&<---- This doesn't work either
>>>>>>NEXT
>>>>>>
>>>>>>
>>>>>>*  Hardcoding the range also does not work
>>>>>>loWorkBook.ActiveSheet.RANGE("M288").select
>>>>>>loExcel.ActiveCell.Value = "Hello"	
>>>>>>
>>>>>>* Execute this code just to make sure there is a connection, and IT WORKS
>>>>>>lcTest = loWorkBook.ActiveSheet.RANGE("L288").VALUE
>>>>>>? lcTest
>>>>>>	
>>>>>>loExcel.displayalerts = .F.
>>>>>>loExcel.QUIT()
>>>>>>RELEASE loExcel
>>>>>>loExcel = NULL
>>>>>>
>>>>>
>>>>>
>>>>>You don't need to SELECT anything in Excel to refer to it, nor do you need to use character strings to refer to rows and columns. You can use code like:
>>>>>
>>>>>
>>>>>loWorkbook.Sheets("28 Day").Cell(m.i, m.j).Value = ALLTRIM(STR(i))
>>>>>
>>>>>
>>>>>Tamar
>>>>
>>>>Tamar:
>>>>
>>>>I tried this but got 'Invalid Name':
>>>>
>>>>
>>>>lcSheet = "28 Day"
>>>>lcSheet = "Sheet1"
>>>>
>>>>lnColumn = 14
>>>>FOR i = 288 TO 300
>>>>	*legRow = 'M' + ALLTRIM(STR(i))
>>>>	loWorkbook.Sheets(lcSheet).Cell(i, lnColumn).Value = ALLTRIM(STR(i))
>>>>	*loWorkBook.ActiveSheet.RANGE("&legRow").select
>>>>	*loExcel.ActiveCell.Value = ALLTRIM(STR(i))   &&<--- This doesn't work
>>>>*!*		loWorkBook.ActiveSheet.RANGE("&legRow").Value = ALLTRIM(STR(i))  &&<---- This doesn't work either
>>>>NEXT
>>>>
>>>>
>>>>My question is (rather than you spoonfeeding me the solution), whats the best way to get the right syntax, using your line of code as an example? I tried with intellisense, but got lost.
>>>
>>>Try recording a macro in Excel to see what properties and methods it uses. I'm not a big fan of directly translating macros to VFP code, but macros can be very handy to see what objects and PEMs are being used.
>>>
>>>In your case, I'd probably store a reference to the right sheet in a variable and then refer to that:
>>>
>>>
>>>loSheet = loWorkbook.Sheets("28 Day")
>>>
>>>
>>>Then work with loSheet.
>>>
>>>Tamar
>>
>>Tamar:
>>
>>As far as I can see, the problem is with the cell() syntax, since this works:
>>
>>
>>loWorkbook.Sheets(lcSheet).Range("A1").Value = ALLTRIM(STR(i))
>>
>>
>>Both of these get an 'Invalid Name' error:
>>
>>
>>loWorkbook.Sheets(lcSheet).Cell(i, lnColumn).Value = ALLTRIM(STR(i))
>>loWorkbook.Sheets(lcSheet).Tamar(i, lnColumn).Value = ALLTRIM(STR(i))
>>
>>
>>
>>As far as macros, it doesn't use the cell() syntax.
>
>That's what I get for doing this off the top of my head without testing. Try Cells rather than Cell.
>
>Tamar

Tamar:

Yup, that did it.

But more importantly, my question is where would I have gone in some documentation to figure this out for myself?

Thanks a loot,

Yossi
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform