아래는 제가 워드를 띄우려고 시도해본 여러가지 경우입니다..
한번 보시고.. 수정사항을 체크해 주세요..ㅠ.ㅠ
3개의 버튼마다 다른 수행이 있습니다..(비슷하지만..)
unit word;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
word2000, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
Word: _Application;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
comobj, ActiveX;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Word: Variant;
Unknown: IUnknown;
Result: HResult;
begin
Result := GetActiveObject(CLASS_wordApplication, nil, Unknown);
if (Result <> 0) then
begin
Word := CreateOleObject('Word.Basic');
Word.FileNew;
Word.AppShow;
Word.Insert(Memo1.Lines.Text);
end
else begin
try
word.visible := true; // 여기서 에러
except
showmessage('예외발생');
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// 클로우즈때 워드도 닫고 폼도 닫고 하고 싶은데.. 안된다..
// word.quit; // 에러 발생..
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Word: _Application;
Unknown: IUnknown;
Result: HResult;
Template :OleVariant;
DocTemplate : OleVariant;
NewTemplate : OleVariant;
begin
Result := GetActiveObject(CLASS_WordApplication, nil, Unknown);
if (Result <> 0) then begin
Word := CoWordApplication.Create;
DocTemplate := Template;
NewTemplate := False;
// word.Documents.Add(DocTemplate, NewTemplate); //새문서를 추가 하고 싶은데 안된다..
Word.Visible := True;
end
else begin
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Word));
Word.Visible := True;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
//헐... 어떻게 해야 할까...
// word.Quit(SaveChanges, OriginalFormat, RouteDocument);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
WordApp : Variant ;
WinHandle : hwnd ;
Result : HResult;
Unknown: IUnknown;
begin
Result := GetActiveObject(CLASS_wordApplication, nil, Unknown);
if (Result = 0) then begin // 이미 워드가 실행중인지를 체크
WordApp.Visible := true; // <--- 에러 발생
//Project Pword.exe raised exception class EoleError with message 'Variant does
//Not reference an automation object. Process stopped. Use Step or Run to continue.
//에러 메시지가 위와 같이 나온다.
end //if문의 끝
else begin
try
WordApp := CreateOLEObject ( 'Word.Application') ;
SetForegroundWindow( WinHandle );
WordApp.Documents.Add ; //새문서 추가
WordApp.Insert(Memo1.Lines.Text); // 에러발생
except
Application.MessageBox ( 'MS Word를 열 수 없습니다' + chr(13) +
'MS Word가 설치되어있는지 확인하십시요' , '알림' , MB_OK ) ;
end; //try 문의 끝
WordApp.Visible := True ;
end; //esle문의 끝
end;
end.