Process the interconnection between word document and database in Delphi 1

Dealing with the interconnection between word document and database in 

Delphi----At present, Delphi is selected by more and more people as the front-end tool in the development of MIS system. In the MIS system with Delphi as the foreground and some large databases as the background, the processing of graphics is unavoidable; that is, graphics are input from the foreground interface developed with Delphi and saved in the corresponding database fields. In this form of graphics processing, the processing of BMP files is relatively simple, because Delphi itself has Image and DBImage components, which can easily exchange data with the large field BLOB that can save graphics in the database. Graphics processing in this way has been used in many MIS software, including personnel file systems that process personnel photos. 

---- However, BMP files are generally larger. And sometimes what you need to enter is a sketch drawn by yourself on the computer, accompanied by a large number of text descriptions. In this case, it is more difficult to handle BMP files with tools such as the drawing board in Win95. General application personnel like to use WORD to draw pictures and write captions, and then save them to the database. 

---- After a period of exploration, we solved this problem, and after perfecting it, it works well in the application. The procedure is as follows: 
procedure TsampleForm.OpenDOCClick(Sender: TObject);
var
  MemSize: Integer;
  Buffer: PChar;
  MyFile: TFileStream;
  Stream: TBlobStream;
begin
  OpenDialog1.Filter:='WORDDocument(*.DOC)|*.DOC'; {select file from dialog}
  if OpenDialog1.Execute then 
  begin
    MyFile:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
    with table1 do   {‘table1’为含BLOB字段的表名}
    begin
      Open;
      Edit;
      Stream := TBlobStream.Create(FieldByName('Doc') as TBlobField, bmWrite);{‘Doc’为BLOB字段名} 
      MemSize := MyFile.Size;
      Inc(MemSize); {Make room for the  buffer's null terminator.}
      Buffer := AllocMem(MemSize);       {Allocate the memory.} 
      try
        Stream.Seek(0, soFromBeginning);  {Seek 0 bytes from the stream's end point}
        MyFile.Read(Buffer^,MemSize);
        Stream.Write(Buffer^,MemSize);
      finally
        MyFile.Free;
        Stream.Free;
      end;
           try
            Post;
           except
            on E: EDatabaseError do
         if HandelException(E)< >0 then 
                        exit
        else
        raise;
           end;
     end;
     Doc_ole.CreateObjectFromFile(OpenDialog1.FileName,False);
     Doc_ole.Run;{Doc_ole is the ToleContainer component name}
   end;
end;


---- The above is the program written to the database. In the application, the file is taken out from the dialog window and displayed in the ToleContainer component and stored in the database at the same time. 

procedure TsampleForm.GetDocClick(Sender: TObject);
var
  MemSize: Integer;
  Buffer: PChar;
  MyFile: TFileStream;
  Stream: TBlobStream;
begin
    MyFile:=TFileStream.Create('c:\temp.tmp',fmCreate);
    with Query1 do
    begin
      Stream := TBlobStream.Create(FieldByName('Doc') as TBlobField, bmRead);
      MemSize := Stream.Size;
      Inc(MemSize); {Make room for the buffer's null terminator.}
      Buffer := AllocMem(MemSize);     {Allocate the memory.}
      try
        Stream.Read(Buffer^,MemSize);
        MyFile.Write(Buffer^,MemSize);
      finally
        MyFile.Free;
        Stream.Free;
      end;
    end;
      if FileExists('c:\temp.DOC') then 
 DeleteFile('c:\temp.DOC');
      if FileExists('c:\temp.tmp') then 
      begin
        RenameFile('c:\temp.tmp', 'c:\temp.DOC');
        Doc_ole.CreateObjectFromFile('c:\temp.DOC',False);
        Doc_ole.Run;
      end;
end;

---- above The procedure is to take out the WORD document from the database, put it on the temporary file of temp.doc and display it in the ToleContainer component. 

---- In other parts of the program, the record pointer of the table should be accurately controlled, so that the access of the WORD document occurs in the correct record position. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324985910&siteId=291194637