Board index » delphi » Insert RTF text into a word document.

Insert RTF text into a word document.


2003-11-07 06:22:44 PM
delphi266
I have a formated rtf string in a variable. I like to insert this formated
text into a worddocument.
Is that possible?
 
 

Re:Insert RTF text into a word document.

Quote
I have a formated rtf string in a variable. I like to insert this formated
text into a worddocument.
Is that possible?
I once did the same via a temp file:
var
Word, Doc, Range: olevariant;
begin
// Save your string to an rtf file...
try
Word := CreateOleObject('Word.Application');
try
Word.Visible := True;
Doc := Word.Documents.Add;
Range := Doc.Range;
Range.InsertFile('tempFilename.rtf');
finally
Word := Unassigned;
end;
finally
DeleteFile(ExtractFilePath('tempFilename.rtf');
end;
tom
 

Re:Insert RTF text into a word document.

Hi,
Quote
I have a formated rtf string in a variable. I like to insert this formated
text into a worddocument.
Is that possible?
I'm just trying the same thing, will give you my solution.
But this replaces the whole document with the RTF, not a real insert.
I want to do an insert, but didn't figured it out till now.
Regards
Holger
procedure TForm1.WriteText(const RTFText: String);
var
S: WordSelection;
VarStyle: OleVariant;
bReplaceSelection: boolean;
pRTF : PChar;
DataObj : IDataObject;
Formats : IEnumFormatEtc;
TempFormat, RTFFormat: TFormatEtc;
Medium : TStgMedium;
found: boolean;
pFormatName : PChar;
pGlobal : Pointer;
Release : bool;
error : integer;
begin
if not Assigned( FWordApplication) then
FWordApplication := CoWordApplication.Create;
if not Assigned( FWordDocument) then
FWordDocument := FWordApplication.Documents.Add(EmptyParam, EmptyParam);
FWordApplication.Visible := true;
OleCheck(FWordDocument.QueryInterface(IDataObject,DataObj));
OleCheck(DataObj.EnumFormatEtc(DATADIR_GET,Formats));
Found := False;
PFormatname := Allocmem(255);
try
FillChar(pFormatName^,255,0);
while Formats.Next(1,TempFormat,nil) = S_OK do
begin
GetclipBoardFormatName(Tempformat.cfFormat, pFormatName , 254);
if SameText(String(pFormatName), 'Rich Text Format') then
begin
RTFFormat := TempFormat;
Found := True;
break;
end;
end;
finally
FreeMem(pFormatName);
end;
if Found then
begin
FillChar(Medium,SizeOf(Medium),0);
Medium.tymed := RTFFormat.tymed;
Medium.hGlobal := GlobalAlloc(GMEM_MOVEABLE, Length(RTFText)+1);
try
pGlobal := GlobalLock(Medium.hGlobal);
CopyMemory(PGlobal,PChar(RTFText),Length(RTFText)+1);
GlobalUnlock(Medium.hGlobal);
Release := False;
OleCheck(DataOBJ.SetData(RTFFormat,Medium,Release));
finally
GlobalFree(Medium.hGlobal);
end;
end;
end;
 

Re:Insert RTF text into a word document.

Hi,
This is easily done using XpertDoc, an alternative to the MS Word
Merge functionality.
XpertDoc supports merge fields of type "RTF" that you can place
anywhere on your document. No programming required!
You can check it out at www.xpertdoc.com
"Holger Sachs" <XXXX@XXXXX.COM>writes news:<3fb1fce1$XXXX@XXXXX.COM>...
Quote
Hi,

>I have a formated rtf string in a variable. I like to insert this formated
>text into a worddocument.
>Is that possible?


I'm just trying the same thing, will give you my solution.
But this replaces the whole document with the RTF, not a real insert.

I want to do an insert, but didn't figured it out till now.

Regards
Holger

procedure TForm1.WriteText(const RTFText: String);
var
S: WordSelection;
VarStyle: OleVariant;
bReplaceSelection: boolean;
pRTF : PChar;
DataObj : IDataObject;
Formats : IEnumFormatEtc;
TempFormat, RTFFormat: TFormatEtc;
Medium : TStgMedium;
found: boolean;
pFormatName : PChar;
pGlobal : Pointer;
Release : bool;
error : integer;
begin
if not Assigned( FWordApplication) then
FWordApplication := CoWordApplication.Create;
if not Assigned( FWordDocument) then
FWordDocument := FWordApplication.Documents.Add(EmptyParam, EmptyParam);

FWordApplication.Visible := true;

OleCheck(FWordDocument.QueryInterface(IDataObject,DataObj));
OleCheck(DataObj.EnumFormatEtc(DATADIR_GET,Formats));

Found := False;
PFormatname := Allocmem(255);
try
FillChar(pFormatName^,255,0);
while Formats.Next(1,TempFormat,nil) = S_OK do
begin
GetclipBoardFormatName(Tempformat.cfFormat, pFormatName , 254);
if SameText(String(pFormatName), 'Rich Text Format') then
begin
RTFFormat := TempFormat;
Found := True;
break;
end;
end;
finally
FreeMem(pFormatName);
end;

if Found then
begin
FillChar(Medium,SizeOf(Medium),0);
Medium.tymed := RTFFormat.tymed;
Medium.hGlobal := GlobalAlloc(GMEM_MOVEABLE, Length(RTFText)+1);
try
pGlobal := GlobalLock(Medium.hGlobal);
CopyMemory(PGlobal,PChar(RTFText),Length(RTFText)+1);
GlobalUnlock(Medium.hGlobal);

Release := False;

OleCheck(DataOBJ.SetData(RTFFormat,Medium,Release));

finally
GlobalFree(Medium.hGlobal);
end;
end;
end;