unit DMsOffAutoMain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons; type TDlgMsOffAutoMain = class(TForm) btnExcel: TButton; btnWord: TButton; Label1: TLabel; edPath: TEdit; SpeedButton1: TSpeedButton; OpenDialog: TOpenDialog; procedure btnExcelClick(Sender: TObject); procedure btnWordClick(Sender: TObject); procedure SpeedButton1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var DlgMsOffAutoMain: TDlgMsOffAutoMain; implementation uses System.Win.ComObj, Tocsg.Path; {$R *.dfm} procedure TDlgMsOffAutoMain.btnExcelClick(Sender: TObject); var sPath, sLogo: String; ExcelApp, ExcelBook, ExcelSheet: Variant; i: Integer; begin sPath := Trim(edPath.Text); // 'C:\Users\kku\Desktop\»õ Microsoft Excel ¿öÅ©½ÃÆ®.xlsx'; if sPath = '' then begin MessageBox(Handle, PChar('ÆÄÀÏ °æ·Î¸¦ ÀÔ·ÂÇØ ÁֽʽÿÀ.'), PChar(Caption), MB_ICONWARNING or MB_OK); edPath.SetFocus; exit; end; if not FileExists(sPath) then begin MessageBox(Handle, PChar('Á¸ÀçÇÏÁö ¾Ê´Â ÆÄÀÏÀÔ´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); edPath.SetFocus; exit; end; sLogo := GetRunExePathDir + 'tocsg_logo.png'; if not FileExists(sLogo) then begin MessageBox(Handle, PChar('¿öÅ͸¶Å© ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; ExcelApp := Null; ExcelBook := Null; ExcelSheet := Null; try ExcelApp := CreateOleObject('Excel.Application'); except ExcelApp := Null; end; if VarIsNull(ExcelApp) then begin MessageBox(Handle, PChar('Excel ÇÁ·Î±×·¥À» ãÀ» ¼ö ¾ø½À´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; try try // ExcelApp.Visible := true; ExcelApp.DisplayAlerts := false; try ExcelBook := ExcelApp.Workbooks.Open(sPath); except ExcelBook := Null; end; if VarIsNull(ExcelBook) then begin MessageBox(Handle, PChar('Excel ÆÄÀÏÀ» ¿©´Â Áß ½ÇÆÐÇß½À´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; for i := 1 to ExcelBook.WorkSheets.Count do begin try ExcelSheet := ExcelBook.WorkSheets[i]; except ExcelSheet := Null; continue; end; if not VarIsNull(ExcelSheet) then begin ExcelSheet.Select; ExcelSheet.PageSetup.CenterHeader := '&G'; ExcelSheet.PageSetup.CenterHeaderPicture.FileName := sLogo; // ExcelSheet.PageSetup.CenterHeaderPicture.Height := 450; // ExcelSheet.PageSetup.CenterHeaderPicture.Width := 300; ExcelSheet.PageSetup.CenterHeaderPicture.ColorType := 4; ExcelSheet.PageSetup.CenterHeaderPicture.CropTop := -300; // ExcelSheet.PageSetup.LeftHeader := 'Text for Left Header'; // ExcelSheet.PageSetup.CenterHeader := 'Text for Center Header'; // ExcelSheet.PageSetup.RightHeader := 'Text for Right Header'; // // ExcelSheet.PageSetup.LeftFooter := 'Text for Left Footer'; // ExcelSheet.PageSetup.CenterFooter := 'Text for Center Footer'; // ExcelSheet.PageSetup.RightFooter := 'Text for Right Footer'; // ExcelSheet.Cells[1,1] := 'We just added header and footer text!'; //[row,column], in this case, text is added to Cell A1 // ExcelSheet.Cells[2,1].Select; ExcelBook.SaveAs(ExtractFilePath(sPath) + 'make_' + ExtractFileName(sPath)); end; end; MessageBox(Handle, PChar('ÀÛ¾÷À» ¿Ï·áÇß½À´Ï´Ù.'), PChar(Caption), MB_ICONINFORMATION or MB_OK); except MessageBox(Handle, PChar('ÆÄÀÏÀ» º¯°æ, ÀúÀå Áß ¿À·ù°¡ ¹ß»ýÇß½À´Ï´Ù.'+#13+#10+ 'ÆÄÀÏÀÌ »ç¿ëÁßÀÎÁö È®ÀÎ ÈÄ ´Ù½Ã ½ÃµµÇØ ÁֽʽÿÀ.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; finally ExcelApp.Workbooks.Close; ExcelApp.DisplayAlerts := true; ExcelApp.Quit; ExcelApp := Unassigned; ExcelBook := Unassigned; ExcelSheet := Unassigned; end; end; procedure TDlgMsOffAutoMain.btnWordClick(Sender: TObject); var sPath, sLogo: String; WordApp, WordFile: Variant; begin sPath := 'C:\Users\kku\Desktop\Readme document_v2.0_220401.docx'; if not FileExists(sPath) then begin MessageBox(Handle, PChar('Á¸ÀçÇÏÁö ¾Ê´Â ÆÄÀÏÀÔ´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; sLogo := GetRunExePathDir + 'tocsg_logo.png'; if not FileExists(sLogo) then begin MessageBox(Handle, PChar('¿öÅ͸¶Å© ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; WordApp := Null; WordFile := Null; try WordApp := CreateOleObject('Word.Application'); except WordApp := Null; end; if VarIsNull(WordApp) then begin MessageBox(Handle, PChar('Word ÇÁ·Î±×·¥À» ãÀ» ¼ö ¾ø½À´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; try try // WordApp.Visible := true; WordApp.DisplayAlerts := false; try WordFile := WordApp.Documents.Open(sPath); except WordFile := Null; end; if VarIsNull(WordFile) then begin MessageBox(Handle, PChar('Word ÆÄÀÏÀ» ¿©´Â Áß ½ÇÆÐÇß½À´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; if not VarIsNull(WordFile) then begin WordFile.Shapes.AddPicture('C:\taskToCSG\Tocsg.Module\MsOffAuto\OUT_Debug - Win32\tocsg_logo.png'); WordFile.SaveAs(ExtractFilePath(sPath) + 'make_' + ExtractFileName(sPath)); end; except MessageBox(Handle, PChar('ÆÄÀÏÀ» º¯°æ, ÀúÀå Áß ¿À·ù°¡ ¹ß»ýÇß½À´Ï´Ù.'+#13+#10+ 'ÆÄÀÏÀÌ »ç¿ëÁßÀÎÁö È®ÀÎ ÈÄ ´Ù½Ã ½ÃµµÇØ ÁֽʽÿÀ.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; finally WordFile.Close; WordApp.DisplayAlerts := true; WordApp.Quit; WordApp := Unassigned; WordFile := Unassigned; end; end; procedure TDlgMsOffAutoMain.SpeedButton1Click(Sender: TObject); begin if OpenDialog.Execute(Handle) then edPath.Text := OpenDialog.FileName; end; end.