Navigation Menu Search PhysiologyWeb
PhysiologyWeb Logo Search PhysiologyWeb
   
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL
procedure TForm1.SetRowHeightAndColumnWidthInExcelWorksheet(Sender: TObject);
var
ExcelFileName: String;
ExcelApplication, ExcelWorkbook, ExcelWorksheet: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

ExcelFileName := 'C:\PhysiologyWeb\delphi_code_examples\excel_file.xlsx'; //replace file name with the name of your file

ExcelApplication := Null;
ExcelWorkbook := Null;
ExcelWorksheet := Null;

try
//create Excel OLE
ExcelApplication := CreateOleObject('Excel.Application');
except
ExcelApplication := Null;
//add error/exception handling code as desired
end;

If VarIsNull(ExcelApplication) = False then
begin
try
ExcelApplication.Visible := True; //set to False if you do not want to see the activity in the background
ExcelApplication.DisplayAlerts := False; //ensures message dialogs do not interrupt the flow of your automation process. May be helpful to set to True during testing and debugging.

//Open Excel Workbook
try
ExcelWorkbook := ExcelApplication.Workbooks.Open(ExcelFileName);
except
ExcelWorkbook := Null;
//add error/exception handling code as desired
end;

If VarIsNull(ExcelWorkbook) = False then
begin
//connect to Excel Worksheet using either the ExcelApplication or ExcelWorkbook handle
try
ExcelWorksheet := ExcelWorkbook.WorkSheets[1]; //[1] specifies the first worksheet
except
ExcelWorksheet := Null;
//add error/exception handling code as desired
end;

If VarIsNull(ExcelWorksheet) = False then
begin
ExcelWorksheet.Select;

ExcelWorksheet.Columns[1].ColumnWidth := 50; //sets Column A width
ExcelWorksheet.Columns[2].ColumnWidth := 50; //sets Column B width
ExcelWorksheet.Columns[3].ColumnWidth := 50; //sets Column C width
ExcelWorksheet.Columns[4].ColumnWidth := 50; //sets Column D width
ExcelWorksheet.Columns['G:G'].ColumnWidth := 50; //sets Column G width
ExcelWorksheet.Columns['J:S'].ColumnWidth := 50; //sets width for Columns J through S
//reference
//https://docs.microsoft.com/en-us/office/vba/api/excel.range.columnwidth

ExcelWorksheet.Rows[1].RowHeight := 50; //sets Row 1 height
ExcelWorksheet.Rows[2].RowHeight := 50; //sets Row 2 height
ExcelWorksheet.Rows[3].RowHeight := 50; //sets Row 3 height
ExcelWorksheet.Rows[4].RowHeight := 50; //sets Row 4 height
ExcelWorksheet.Rows['10:20'].RowHeight := 50; //sets height for Rows 10 through 20
//reference
//https://docs.microsoft.com/en-us/office/vba/api/excel.range.rowheight

ExcelWorksheet.Cells[1,1].Select; //selects Cell A1

ExcelWorkbook.Save;
end;
end;
finally
ExcelApplication.Workbooks.Close;
ExcelApplication.DisplayAlerts := True;
ExcelApplication.Quit;

ExcelWorksheet := Unassigned;
ExcelWorkbook := Unassigned;
ExcelApplication := Unassigned;
end;
end;
end;




Posted: Sunday, July 10, 2022
Last updated: Wednesday, March 19, 2025
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL