Search code examples
excelcellmaxscript

Maxscript and Excel: retrieving/changing a cell's value


I'm trying to access a single cell's value via maxscript and I've been having quite a bit of difficulty figuring out what exactly is keeping me from doing so. Here's what I have in maxscript:

-- Startup Ops
-- Generate a filename
excelFile = "<path string>\testbook.xlsx"

-- Start an Excel OLE Object
x = CreateOLEObject "Excel.Application"

-- Create a new workbook in the new excel document
x.application.Workbooks.open(excelFile)

-- This makes Excel Visible
x.visible = true



--Here's where I'm having trouble
-- Retrieve the contents of Cell A3
contents = x.ActiveSheet.Cells(3, 1).Value

-- Change the contents of Cell A3 to Hello
x.ActiveSheet.Cells(3, 1).Value = "Hello"



-- Cleanup Ops
-- Close the spreadsheet
x.application.ActiveWorkbook.Close

-- quit excel
x.quit()

-- Release the OLE Object
releaseOLEObject x

-- Release ALL OLE Objects, just in case
releaseAllOLEObjects()

Here's the msdn resource I used to for reference. As far as I know, I followed everything to a T. If anyone could help out, I'd really appreciate it.


Solution

  • I don't have Excel at hand but I believe it should rather be (x.ActiveSheet.Cells X Y).Value.

    Either that or you can take a different route, use .NET and excellibrary.