这个问题比较容易,
一、通过 TBlobField 的 LoadFromFile 方法将 WORD 文件写入字段中
二、读出时用 TBlobField 的 SaveToFile 方法存成 WORD 文件
三、建立 WORD Application 对象,打开文件
四、启动一个计时器,去判断 WORD 是否已经退出
五、如果 WORD 已经退出,运行第二点的保存方法
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBTables, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Query1: TQuery;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
WordApp: Variant;
WordCap: string;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
ComObj;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
// 保存代码
TBlobField(Query1.FieldByName('ygxxb_word')).LoadFromFile('c:/XXX员工档案.DOC');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// 取出文件代码
TBlobField(Query1.FieldByName('ygxxb_word')).SaveToFile('c:/XXX员工档案.DOC');
end;
procedure TForm1.Button3Click(Sender: TObject);
var
FileName: string;
begin
// 打开文件代码
FileName := 'c:/XXX员工档案.DOC';
WordApp := CreateOleObject('Word.Application');
WordApp.Documents.Open(FileName);
WordCap := ExtractFileName(FileName) + ' - ' + String(WordApp.Caption);
WordApp.Visible := true;
{ 启动计时器监视 WORD 是否被关闭 }
Timer1.Enabled := true;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
WordHandle: Integer;
begin
{ 查找当前打开的 WORD 文档窗口 }
Timer1.Enabled := False;
WordHandle := FindWindow(nil, PChar(WordCap));
{ 如果未关闭,则关闭它 }
if IsWindow(WordHandle) then
begin
try
if not WordApp.ActiveDocument.ReadOnly then
WordApp.Documents.Save;
WordApp.Documents.Close;
WordApp.Quit;
except
SendMessage(WordHandle, WM_QUIT, 0, 0);
end;
end;
WordApp := Unassigned;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
WordHandle: Integer;
begin
Timer1.Enabled := False;
Timer1.Tag := Timer1.Tag + 1;
{ 查找当前打开的 WORD 文档窗口 }
WordHandle := FindWindow(nil, PChar(WordCap));
{ 如果未关闭,每隔 2 秒钟存盘一次,
主要是为了用户的修改能及时保存 }
if IsWindow(WordHandle) then
begin
try
if not WordApp.ActiveDocument.ReadOnly then
if (Timer1.Tag mod 4) = 0 then WordApp.Documents.Save;
except end;
end
else { 用户关闭了 WORD }
begin
FormDestroy(Sender);
Exit;
end;
Timer1.Enabled := True;
end;
end.
随便写一下,请多包含