The Final Piece of the Puzzle π§©
Welcome to the final part of our Uniface & Excel series! Part 1 - We have launched Excel, Part 2 - we have read data, and now it is time to write changes back and ensure we don't leave any "ghost" Excel processes running in the background. π»
Step 1: Writing Data to a Cell βοΈ
Writing is just as straightforward as reading. Once you have your Range handle (the cell), you simply call the appropriate method to set its value.
In the sample project, they use SET_FORMULAR1C1, which is great for setting formulas or raw values.
variables
handle vActiveSheet
handle vCellRange
string vNewValue
endvariables
vNewValue = "Updated from Uniface! π"
; 1. Get the cell range (e.g., "B5")
vActiveSheet->GET_RANGE(vCellRange, "B5", -)
; 2. Write the value
; We use SET_FORMULAR1C1 to set the content of the cell.
vCellRange->SET_FORMULAR1C1(vNewValue)
Note: Depending on your generated COM signature, you might also see SET_VALUE or SET_VALUE2. They often do similar things, but FormulaR1C1 is very robust because it avoids locale-specific formatting issues (like comma vs. dot decimal separators).
Step 2: Saving & Closing the Workbook πΎ
You have made your changes. Now, you need to save them. The Close method on the Workbook handle is your friend here.
; Close the workbook
; Parameter 1: SaveChanges (1 = Yes, 0 = No)
; Parameter 2: Filename (Optional, used if 'SaveAs')
; Parameter 3: RouteWorkbook (Legacy, usually ignored)
vActiveWorkBook->Close(1, -, 1)
By passing 1 as the first parameter, Excel automatically saves your changes to the original file before closing it.
Step 3: The Clean Exit (Crucial!) π§Ή
If you don't clean up properly, your Task Manager will eventually look like a graveyard of EXCEL.EXE processes. Always follow this cleanup routine:
- Release Child Objects: Delete handles for ranges, sheets, and workbooks first.
- Quit the Application: Tell Excel to shut down.
- Release the Main Handle: Delete the application instance.
; 1. Clean up specific object handles
deleteinstance vCellRange
deleteinstance vActiveSheet
deleteinstance vActiveWorkBook
deleteinstance vWorkBooks
; 2. Tell Excel to Quit
vExcelHandle->Quit()
; 3. Kill the Uniface handle to the application
deleteinstance vExcelHandle
Conclusion π
And thatβs it! You now have a full cycle:
- β Start Excel
- β Read Data
- β Write Data
- β Save & Quit
COM automation is a powerful tool in your Uniface arsenal. Whether you are generating monthly reports or importing massive datasets, these patterns will serve you well.
Resources
Thanks for following along this series! Happy coding! π©βπ»π¨βπ»
Top comments (0)