{"id":431,"date":"2023-03-22T10:09:33","date_gmt":"2023-03-22T10:09:33","guid":{"rendered":"http:\/\/basicwebsitesolutions.com\/blog\/?p=431"},"modified":"2023-03-22T10:09:34","modified_gmt":"2023-03-22T10:09:34","slug":"delphi-excel-ole-manipulation-change-font-masking-and-more","status":"publish","type":"post","link":"https:\/\/basicwebsitesolutions.com\/blog\/2023\/03\/22\/delphi-excel-ole-manipulation-change-font-masking-and-more\/","title":{"rendered":"Delphi + Excel OLE Manipulation (Change Font, Masking and More)"},"content":{"rendered":"\n<p>Thought of saving this one here. For those who still use delphi in 2023, here are the features you can access. Just in case you have questions or issues encountered, just drop me a line<\/p>\n\n\n\n<p>Coffee Cup<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">...control Excel with OLE?\r\nAutor: Thomas Stutz\r\n[ Print tip ]\t \t \r\n\r\nTip Rating (503):\t \r\n     \r\n\r\nuses\r\n  ComObj;\r\n\r\nvar\r\n  ExcelApp: OleVariant;\r\n\r\nimplementation\r\n\r\n\r\nprocedure TForm1.Button1Click(Sender: TObject);\r\nconst\r\n  \/\/ SheetType\r\n  xlChart = -4109;\r\n  xlWorksheet = -4167;\r\n  \/\/ WBATemplate\r\n  xlWBATWorksheet = -4167;\r\n  xlWBATChart = -4109;\r\n  \/\/ Page Setup\r\n  xlPortrait = 1;\r\n  xlLandscape = 2;\r\n  xlPaperA4 = 9;\r\n  \/\/ Format Cells\r\n  xlBottom = -4107;\r\n  xlLeft = -4131;\r\n  xlRight = -4152;\r\n  xlTop = -4160;\r\n  \/\/ Text Alignment\r\n  xlHAlignCenter = -4108;\r\n  xlVAlignCenter = -4108;\r\n  \/\/ Cell Borders\r\n  xlThick = 4;\r\n  xlThin = 2;\r\nvar\r\n  ColumnRange: OleVariant;\r\n\r\n  \/\/ Function to get the number of Rows in a Certain column\r\n\r\n  function GetLastLine(AColumn: Integer): Integer;\r\n  const\r\n    xlUp = 3;\r\n  begin\r\n    Result := ExcelApp.Range[Char(96 + AColumn) + IntToStr(65536)].end[xlUp].Rows.Row;\r\n  end;\r\n\r\nbegin\r\n  { Start Excel }\r\n\r\n  \/\/ By using GetActiveOleObject, you use an instance of Word that's already running,\r\n  \/\/ if there is one.\r\n  try\r\n    ExcelApp := GetActiveOleObject('Excel.Application');\r\n  except\r\n    try\r\n      \/\/ If no instance of Word is running, try to Create a new Excel Object\r\n      ExcelApp := CreateOleObject('Excel.Application');\r\n    except\r\n      ShowMessage('Cannot start Excel\/Excel not installed ?');\r\n      Exit;\r\n    end;\r\n  end;\r\n\r\n  \/\/ Add a new Workbook, Neue Arbeitsmappe ?ffnen\r\n  ExcelApp.Workbooks.Add(xlWBatWorkSheet);\r\n\r\n  \/\/ Open a Workbook, Arbeitsmappe ?ffnen\r\n  ExcelApp.Workbooks.Open('c:\\YourFileName.xls');\r\n\r\n\r\n  \/\/ Rename the active Sheet\r\n  ExcelApp.ActiveSheet.Name := 'This is Sheet 1';\r\n\r\n  \/\/ Rename\r\n  ExcelApp.Workbooks[1].WorkSheets[1].Name := 'This is Sheet 1';\r\n\r\n  \/\/ Insert some Text in some Cells[Row,Col]\r\n  ExcelApp.Cells[1, 1].Value := 'SwissDelphiCenter.ch';\r\n  ExcelApp.Cells[2, 1].Value := 'http:\/\/www.swissdelphicenter.ch';\r\n  ExcelApp.Cells[3, 1].Value := FormatDateTime('dd-mmm-yyyy', Now);\r\n\r\n  \/\/ Setting a row of data with one call\r\n  ExcelApp.Range['A2', 'D2'].Value := VarArrayOf([1, 10, 100, 1000]);\r\n\r\n  \/\/ Setting a formula\r\n  ExcelApp.Range['A11', 'A11'].Formula := '=Sum(A1:A10)';\r\n\r\n  \/\/ Change Cell Alignement\r\n  ExcelApp.Cells[2, 1].HorizontalAlignment := xlright;\r\n\r\n  \/\/ Change the Column Width.\r\n  ColumnRange := ExcelApp.Workbooks[1].WorkSheets[1].Columns;\r\n  ColumnRange.Columns[1].ColumnWidth := 20;\r\n  ColumnRange.Columns[2].ColumnWidth := 40;\r\n\r\n  \/\/ Change Rowheight \/ Zeilenh?he ?ndern:\r\n  ExcelApp.Rows[1].RowHeight := 15.75;\r\n\r\n  \/\/ Merge cells, Zellen verbinden:\r\n  ExcelApp.Range['B3:D3'].Mergecells := True;\r\n\r\n  \/\/ Apply borders to cells, Zellen umrahmen:\r\n  ExcelApp.Range['A14:M14'].Borders.Weight := xlThick; \/\/ Think line\/ Dicke Linie\r\n  ExcelApp.Range['A14:M14'].Borders.Weight := xlThin;  \/\/ Thin line D\u90a6nne Linie\r\n\r\n  \/\/ Set Bold Font in cells, Fettdruck in den Zellen\r\n\r\n  ExcelApp.Range['B16:M26'].Font.Bold := True;\r\n\r\n  \/\/ Set Font Size, Schriftgr??e setzen\r\n  ExcelApp.Range['B16:M26'].Font.Size := 12;\r\n\r\n  \/\/right-aligned Text, rechtsb\u90a6ndige Textausrichtung\r\n  ExcelApp.Cells[9, 6].HorizontalAlignment := xlright;\r\n\r\n  \/\/ horizontal-aligned text, horizontale Zentrierung\r\n  ExcelApp.Range['B14:M26'].HorizontalAlignment := xlHAlignCenter;\r\n\r\n  \/\/ left-aligned Text, vertikale Zentrierung\r\n  ExcelApp.Range['B14:M26'].VerticallyAlignment := xlVAlignCenter;\r\n\r\n\r\n  { Page Setup }\r\n\r\n  ExcelApp.ActiveSheet.PageSetup.Orientation := xlLandscape;\r\n\r\n  \/\/ Left, Right Margin (Seitenr?nder)\r\n  ExcelApp.ActiveSheet.PageSetup.LeftMargin  := 35;\r\n  ExcelApp.ActiveSheet.PageSetup.RightMargin := -15;\r\n\r\n  \/\/ Set Footer Margin\r\n  ExcelApp.ActiveSheet.PageSetup.FooterMargin := ExcelApp.InchesToPoints(0);\r\n\r\n  \/\/ Fit to X page(s) wide by Y tall\r\n  ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1;  \/\/ Y\r\n  ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 3; \/\/ Y\r\n\r\n  \/\/ Zoom\r\n  ExcelApp.ActiveSheet.PageSetup.Zoom := 95;\r\n\r\n  \/\/ Set Paper Size:\r\n  ExcelApp.PageSetup.PaperSize := xlPaperA4;\r\n\r\n  \/\/ Show\/Hide Gridlines:\r\n  ExcelApp.ActiveWindow.DisplayGridlines := False;\r\n\r\n  \/\/ Set Black &amp; White\r\n  ExcelApp.ActiveSheet.PageSetup.BlackAndWhite := False;\r\n\r\n  \/\/ footers\r\n  ExcelApp.ActiveSheet.PageSetup.RightFooter := 'Right Footer \/ Rechte Fu?zeile';\r\n  ExcelApp.ActiveSheet.PageSetup.LeftFooter  := 'Left Footer \/ Linke Fu?zeile';\r\n\r\n  \/\/ Show Excel Version:\r\n  ShowMessage(Format('Excel Version %s: ', [ExcelApp.Version]));\r\n\r\n  \/\/ Show Excel:\r\n  ExcelApp.Visible := True;\r\n\r\n  \/\/ Save the Workbook\r\n  ExcelApp.SaveAs('c:\\filename.xls');\r\n\r\n  \/\/ Save the active Workbook:\r\n  ExcelApp.ActiveWorkBook.SaveAs('c:\\filename.xls');\r\n\r\nend;\r\n\r\nprocedure TForm1.FormDestroy(Sender: TObject);\r\nbegin\r\n  \/\/ Quit Excel\r\n  if not VarIsEmpty(ExcelApp) then\r\n  begin\r\n    ExcelApp.DisplayAlerts := False;  \/\/ Discard unsaved files....\r\n    ExcelApp.Quit;\r\n  end;\r\nend;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Delphi + Excel OLE Manipulation (Change Font, Masking and More)<\/p>\n","protected":false},"author":1,"featured_media":432,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3,1],"tags":[36,77,240,241],"class_list":["post-431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-day-to-day-life","category-delphi-and-interbase","category-life-as-a-programmer","tag-delphi","tag-excel","tag-masking-font","tag-ole"],"_links":{"self":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=431"}],"version-history":[{"count":1,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":433,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/431\/revisions\/433"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/media\/432"}],"wp:attachment":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}