EXCEL.INTEROP working with Range and Cells

CGaraldeCGaralde Member Posts: 19

For grizzled veterans, this may be a very trivial question but I am having problems working on Ranges and Cells on Excel.Interop.

Basically, I have the following code:

FileName := 'C:\Book1.xlsx';

XlApp := XlApp.ApplicationClass;

XlWrkBk := XlApp.Workbooks.Open(FileName,0,TRUE,5,'','',TRUE,2,'\t',FALSE,FALSE,0,TRUE,1,0);

XlWrkSht := XlWrkBk.Sheets.Item(1);

XlRange := XlWrkSht.Range('A1','A10');

How do I work with the cells in the defined XlRange? I searched for it a lot but I could not seem to find the correct syntax. I tried the following:
XlWrkSht.Cells(3,1).Value2 := 'ABCDE';
XlRange.Cells(1,1).Value2 := 'XXXXX';
XlRange.Cells[1,1].Value2 := 'XXXXX';

And I get a "parameter 0" error or a "too many dimensions" error. I understand the errors but for the life of me, I do not know how else to manipulate the cells within a range. Any help is greatly appreciated. :smile:


  • CGaraldeCGaralde Member Posts: 19
    No takers? :)

    Anyway, I can write data to specific cells using:
    XlRange := XlWrkSht.Range('A1','A1');
    XlRange.Value2 := 'XXXXXXX';

    But it seems inefficient as I have to create XlRange each time I access a new cell. I was thinking of defining XlRange for multiple cells and just loop through it using CELLS but like I said, I cannot get the write syntax for it. :(
  • xStepaxStepa Member Posts: 106
    Hi, try to pass the cell address in one parameter:
    XlWrkSht.Cells('C1').Value := 'ABCDE'
  • CGaraldeCGaralde Member Posts: 19
    Hi xStepa,

    Thanks for taking the time but I have already tried that and I get the error that

    Microsoft Dynamics NAV Development Environment
    A maximum of 0 parameters must be used when calling the function. For example:

    MyFunc( .. , .. , .. )

Sign In or Register to comment.