关于word保存到数据库的问题(100分)

  • 主题发起人 主题发起人 ligwood
  • 开始时间 开始时间
L

ligwood

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在做一个小的文件管理系统
我用wordapplication,worddocument等控件调用word 现在的问题是我把数据库的内容读出来存成doc文件 让后用通过wordapplication控件打开word 对文件进行编辑后 我想在关闭word的时候能给我提示把word保存的数据库 或者在word在不关闭word情况下得把word存入数据库 或者在word上添加个宏 按钮保存到数据库
我关闭word时能保存的数据库 但在打开状态下不能存入呀
希望大家帮忙 谢谢大家
 
以二进制文件格式保存进数据库,把你存入数据库的代码贴一下。
 
Target_Name:= ExtractFileDir(Application.ExeName) + '/temp/AlFile.doc';
MemoryStream:=TMemoryStream.Create;
MemoryStream.LoadFromFile(Target_Name);
adoquery_main.Edit;
adoquery_maingzzjnr.LoadFromStream(MemoryStream);
adoquery_main.Post;
 
procedure SaveAs(sPath: String);//先保存为文件
var
FileName, FileFormat,
LockComments, Password, saveChanges,Originalformat,routidocument,
AddToRecentFiles, WritePassword,
ReadOnlyRecommended, EmbedTrueTypeFonts,
SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter : OleVariant;
begin
if FileExists(sPath) then DeleteFile(sPath);
if sPath<>'' then
begin
try
Screen.Cursor:=62;
FileName:=sPath;
FileFormat := wdNormalTemplate;// 101;
LockComments := False;
Password := '';
AddToRecentFiles := True;
WritePassword := '';
ReadOnlyRecommended := False;
EmbedTrueTypeFonts := False;
SaveNativePictureFormat := False;
SaveFormsData := False;
SaveAsAOCELetter := False;

SaveChanges := false;
RoutiDocument := 0;
OriginalFormat := 0;

wordApp.ActiveDocument.SaveAs(FileName,FileFormat,LockComments,Password,AddToRecentFiles,
WritePassword,ReadOnlyRecommended,EmbedTrueTypeFonts,SaveNativePictureFormat,
SaveFormsData,SaveAsAOCELetter);
finally
Screen.Cursor:=crDefault;
end;
end;
SaveTo(sPath);
end;

procedure SaveTo(tmpFileName: oleVariant);
var
fileStream: TFileStream;
filename: oleVariant;
TempPath:String;
begin
fileStream:=TFileStream.Create(tmpFileName, fmOpenRead or fmShareDenyNone);
try
Screen.Cursor:=62;
//ReturnTemplet;
try
pic := VarArrayCreate ([0,FileStream.Size - 1], varByte);
P := VarArrayLock (Pic);
FileStream.Position := 0;
FileStream.Read (P^, FileStream.Size); //保存为 OleVariant
VarArrayUnlock (Pic);
try
(dmPub.HTTPRIO1 as IPwSoapDm).UpdateImage(pic,ssql,outpara);//我的webservices中的服务端保存方法
except
raise exception.Create('保存失败');
end;
//bSave:=True;
finally
Screen.Cursor:=crDefault;
end;
finally
fileStream.Free;
end;
end;
 
用TShellChangeNotifier监控文件改变,改变了你就可以保存了
1.读
procedure TFrmCustomer.SavePictToServer(P:PFileCollection);
var str:Tmemorystream;
SQL:String;
begin
if SaveFileToServer=False then exit;
SQL:='Select * From CustomerSubDetail Where ParentID='+inttostr(P.ParentID);
SQL:=SQL+' And FileName='+quotedstr(P.FileName);
SQL:=SQL+' And FilePath='+quotedstr(P.DirPath)+' And InUse=1 And IsFile=1';
ExeSQL(ADOQuery1,SQL);
//保存文件到数据库
str:=Tmemorystream.Create;
str.LoadFromFile(P.DirPath+'/'+P.FileName);
str.Position:=0;
Tblobfield(ADOQuery1.FieldByName('FileInfo')).LoadFromStream(str);
try
ADOQuery1.Post;
finally
str.Free;
end;
end;
2.写
procedure TFrmCustomer.Button2Click(Sender: TObject);
var str:Tmemorystream;
begin
//写测试
str:=Tmemorystream.Create;
str.LoadFromFile('D:/Test.doc');
str.Position:=0;
adoquery1.Append;
Tblobfield(adoquery1.FieldByName('WordFile')).loadfromstream(str);
try
adoquery1.Post;
finally
str.Free;
end;
end;
或者WordApplication2Quit时保存
 
用richview容易的
 
谁能给一段比较完整的答案吗
我对操作word方面实在不熟悉
问题到现在还没有解决
 
谁帮下我 成功后我在送100分
 
大家帮我呀
 
-------------下面这个代码是Delphi自带的,可以提示你Word关闭事件,研究下估计还有会有类型AfterClose之类的东西,Borland/Delphi6/Demos/ActiveX/OleAuto/Word8, 既然你用WordApplication 的话也已经知道文件路径了,所有无所谓保存临时文件了,如果在打开过程中的话只有保存临时文件了

unit WordAuto;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
LaunchButton: TButton;
CloseButton: TButton;
Memo1: TMemo;
NewDocButton: TButton;
TypeTextButton: TButton;
WordText: TEdit;
Label1: TLabel;
procedure LaunchButtonClick(Sender: TObject);
procedure CloseButtonClick(Sender: TObject);
procedure NewDocButtonClick(Sender: TObject);
procedure TypeTextButtonClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private Declarations }
procedure AppQuit(Sender: TObject);
procedure AppDocumentChange(Sender: TObject);
procedure DocNew(Sender: TObject);
procedure DocOpen(Sender: TObject);
procedure DocClose(Sender: TObject);
public
{ Public Declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses
AutoImpl;

var
WordObject : TWordObject;

procedure TForm1.LaunchButtonClick(Sender: TObject);
begin
if not Assigned(WordObject) then begin
WordObject := TWordObject.Create;
with WordObject do begin
Caption := 'Delphi is RAD!';
Visible := True;
OnQuit := AppQuit;
OnDocumentChange := AppDocumentChange;
OnNewDocument := DocNew;
OnOpenDocument := DocOpen;
OnCloseDocument := DocClose;
end;
LaunchButton.Enabled := False;
CloseButton.Enabled := True;
NewDocButton.Enabled := True;
end;
end;

procedure TForm1.CloseButtonClick(Sender: TObject);
begin
if Assigned(WordObject) then begin
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;

procedure TForm1.AppDocumentChange(Sender: TObject);
begin
Memo1.Lines.Add('Document changed');
TypeTextButton.Enabled := WordObject.Application.Documents.Count > 0;
end;

procedure TForm1.AppQuit(Sender: TObject);
begin
Memo1.Lines.Add('Quitting Word');
WordObject.Free;
WordObject := nil;
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
end;

procedure TForm1.DocNew(Sender: TObject);
begin
Memo1.Lines.Add('New document');
end;

procedure TForm1.DocOpen(Sender: TObject);
begin
Memo1.Lines.Add('Document opened');
end;

procedure TForm1.DocClose(Sender: TObject);
begin
Memo1.Lines.Add('Document closed');
end;

procedure TForm1.NewDocButtonClick(Sender: TObject);
begin
try
WordObject.NewDoc('');
except
ShowMessage('It seems like somebody killed Word and didn''t tell me about it...');
if Assigned(WordObject) then begin
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;
end;

procedure TForm1.TypeTextButtonClick(Sender: TObject);
begin
WordObject.InsertText(WordText.Text);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(WordObject) then begin
WordObject.Free;
WordObject := nil;
end;
end;

end.
 
后退
顶部