Dealing with Word documents and database interconnection in Delphi
----At present, Delphi is more and more candidates as a front-desk tool in MIS system development. In Delphi as the foreground, some large-scale database for the background of MIS system, graphics processing is unavoidable; that is, from the foreground interface developed by Delphi input graphics, and saved to the corresponding database field. In this form of graphics processing, BMP file processing is relatively simple, because Delphi itself has image and dbimage components, with these components and the database can save the graphics of large field blob easier to exchange data. In this way, graphics processing has been applied in many MIS software, including personnel file system for processing personnel photos.
----However, BMP files are generally relatively large. And sometimes to input is their own computer drawing of the diagram, and accompanied by a large number of text description. This situation with the Win95 in the drawing board processing BMP File tool processing is more difficult. General applications like to use Word to paint and write descriptive text, and then save it to a database.
----After a period of groping, we solved this problem, and through the perfect, in the application of good operation. The procedure is as follows:
Procedure Tsampleform.opendocclick (Sender:tobject);
Var
Memsize:integer;
Buffer:pchar;
Myfile:tfilestream;
Stream:tblobstream;
Begin
opendialog1.filter:= ' Word document (*. DOC) |*. DOC '; {Select File from dialog window}
If Opendialog1.execute Then
Begin
Myfile:=tfilestream.create (Opendialog1.filename,fmopenread);
With Table1 does {' table1 ' is a table name with a BLOB field}
Begin
Open;
Edit;
Stream: = Tblobstream.create (Fieldbyname (' Doc ') as Tblobfield, bmwrite); {' Doc ' is a BLOB field name}
Memsize: = myfile.size;
INC (memsize); {make, 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 Tolecontainer widget name}
End
End
----The above is the program written to the database, the application from the dialog window out of the file in the Tolecontainer widget display in the same time into the database.
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, 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
----The above program to remove the Word document from the database and place it on the temporary file of Temp.doc and display it in the Tolecontainer widget.
----The other parts of the program should accurately control the table record pointers, so that access to the Word document occurs at the correct record location.
Dealing with Word documents and database interconnection in Delphi