그동안 귀찮게해드려 죄송하고여.. 완성했습니다..
여기저기 다니면서 껴마추기 하면서 많이 배웠습니다...(초보라)
제가 만든거는요 버튼 클릭하면 워드가 실행하고 두번이상 뜨지 않는거고여..
종료하면 워드랑 원래 프로그램이 모두 닫히는 거에여...
근데... 제가 원하는게 되긴 하는데..좀더 간단한 방법이 있을것 같아 소스를 올립니다..
많은 의견 주세여~~
그럼 아래는 소스입니다..
unit word;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
word2000, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button4: TButton;
Button3: TButton;
procedure FormDestroy(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
wordApp,Range : oleVariant ;
end;
var
Form1: TForm1;
implementation
uses
comobj, ActiveX;
{$R *.DFM}
procedure TForm1.FormDestroy(Sender: TObject);
{
begin
wordApp := FindWindow('opusapp', nil);
if wordApp > 0 then
SendMessage(Wnd, WM_CLOSE, 0, 0);
end;// 원하는 으로그램 종료 opusapp는 워드의 클래스명
}
var
VClose: OleVariant;
WordActivated: Boolean;
begin
WordActivated := False; // 왜 쓰는지 모름
VClose := findwindow('opusapp', nil);//워드의 클래스명이 opusapp이다
if VClose = 0 then
close
else begin
VClose := GetActiveOleObject('Word.Application');
VClose.Application.Quit;
WordActivated := True; // 왜 쓰는지 모름
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
WinHandle : hwnd ;
Result : HResult;
Unknown: IUnknown;
begin
Result := GetActiveObject(CLASS_wordApplication, nil, Unknown);
if (Result = 0) then begin // 이미 워드가 실행중인지를 체크
WinHandle := findwindow('opusapp',nil);
if (WinHandle <> 0 ) then
begin
BringWindowToTop(WinHandle);
ShowWindow(WinHandle, SW_RESTORE);
end;
end //if문의 끝
else begin
try
WordApp := CreateOLEObject('Word.Application') ;
WordApp.Documents.Add ; //새문서 추가
Range := WordApp.Documents.Item(1).Range;
Range.Text := memo1.text;
except
Application.MessageBox ( 'MS Word를 열 수 없습니다' + chr(13) +
'MS Word가 설치되어있는지 확인하십시요' , '알림' , MB_OK ) ;
end; //try 문의 끝
WordApp.Visible := True ;
end; //esle문의 끝
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
WordApp := CreateOleObject('Word.Application');
WordApp.Visible := True;
WordApp.Documents.Add;
Range := WordApp.Documents.Item(1).Range;
Range.Text := memo1.text;
end;
end.