BSOne.SFC/Tocsg.Module/MsOffAuto/DMsOffAutoMain.pas

233 lines
6.1 KiB
Plaintext

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.