Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read contents in a ms word file?

Status
Not open for further replies.

edeaux

Programmer
Mar 7, 2003
16
0
0
PH
does anyone know how to read contents in a ms word file without opening ms word?
 
Don't know which version of Delphi you have, but on mine on the Servers component palette, there's one called WordDocument. Now - I've never done this before, so I'm not sure how well you are going to go. Without doing a search on google, there's no help available.

Add the component to your form, then in your code, type WordDocument1. (or whatever you called it) Typing the full stop should pull up the list of all available methods for the object. Scroll through (there's heaps) and you'll see some interesting ones.

I was able to work out how to compact an Access database using this method. Post back here if you would like more help, or especially if you manage to get it working - I'd sure like to know for future reference.
 
i have two versions of delphi, 5 and 7. i tried tworddocument based on the examples i found in google but same it opens up the ms word then get the contents of the document loaded. i tried converting it to rtf using the tworddocument and my problem is how to get the objects in the rtf like the image(s) on it and the text itself.
please do help me i really this badly.
 
Having a look on and found this


It's freeware - the blurb says "RTFapi.dll is a freeware-dll for converting common Text formats (DOC, HTML, XLS ...) to Richttext (RTF) and the other way around, based on the installed Text converters. Using TStream you can save the converted text as file or put it directly into TRichedit or TRxRichEdit. "

Sounds like it could be right for you.
 
Griff,

Thanks for posting the link.
I tried it man but it won't show up image on an opened rtf. One thing it uses trichedit so it is impossible to include image.

thanks

edwin
 
I found this recently on Torry's Delphi pages:

...insert a image into a TRxRichEdit?
Author: Thomas Stutz



uses
RichEdit;

// Stream Callback function
type
TEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte;
cb: Longint; var pcb: Longint): DWORD;
stdcall;

TEditStream = record
dwCookie: Longint;
dwError: Longint;
pfnCallback: TEditStreamCallBack;
end;

// RichEdit Type
type
TMyRichEdit = TRxRichEdit;

// EditStreamInCallback callback function
function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
cb: Longint; var pcb: Longint): DWORD; stdcall;
// by P. Below
var
theStream: TStream;
dataAvail: LongInt;
begin
theStream := TStream(dwCookie);
with theStream do
begin
dataAvail := Size - Position;
Result := 0;
if dataAvail <= cb then
begin
pcb := read(pbBuff^, dataAvail);
if pcb <> dataAvail then
Result := UINT(E_FAIL);
end
else
begin
pcb := read(pbBuff^, cb);
if pcb <> cb then
Result := UINT(E_FAIL);
end;
end;
end;

// Insert Stream into RichEdit
procedure PutRTFSelection(RichEdit: TMyRichEdit; SourceStream: TStream);
// by P. Below
var
EditStream: TEditStream;
begin
with EditStream do
begin
dwCookie := Longint(SourceStream);
dwError := 0;
pfnCallback := EditStreamInCallBack;
end;
RichEdit.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, Longint(@EditStream));
end;

// Convert Bitmap to RTF Code
function BitmapToRTF(pict: TBitmap): string;
// by D3k
var
bi, bb, rtf: string;
bis, bbs: Cardinal;
achar: ShortString;
hexpict: string;
I: Integer;
begin
GetDIBSizes(pict.Handle, bis, bbs);
SetLength(bi, bis);
SetLength(bb, bbs);
GetDIB(pict.Handle, pict.Palette, PChar(bi)^, PChar(bb)^);
rtf := '{\rtf1 {\pict\dibitmap ';
SetLength(hexpict, (Length(bb) + Length(bi)) * 2);
I := 2;
for bis := 1 to Length(bi) do
begin
achar := Format('%x', [Integer(bi[bis])]);
if Length(achar) = 1 then
achar := '0' + achar;
hexpict[I - 1] := achar[1];
hexpict := achar[2];
Inc(I, 2);
end;
for bbs := 1 to Length(bb) do
begin
achar := Format('%x', [Integer(bb[bbs])]);
if Length(achar) = 1 then
achar := '0' + achar;
hexpict[I - 1] := achar[1];
hexpict := achar[2];
Inc(I, 2);
end;
rtf := rtf + hexpict + ' }}';
Result := rtf;
end;


// Example to insert image from Image1 into RxRichEdit1
procedure TForm1.Button1Click(Sender: TObject);
var
SS: TStringStream;
BMP: TBitmap;
begin
BMP := TBitmap.Create;
BMP := Image1.Picture.Bitmap;
SS := TStringStream.Create(BitmapToRTF(BMP));
try
PutRTFSelection(RxRichEdit1, SS);
finally
SS.Free;
end;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top