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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting rich text into Word

Status
Not open for further replies.

MagicFrisbee

Programmer
Feb 22, 2006
74
US
For a few versions of Delphi, I've been able to move rich text format code into Word using the following code, where ocxWordDoc is a TWordComponent on the form:

Code:
var
  sRTF: String;
  lRTFFormat, hGlobal: THandle;
  lpString: Pointer;
begin
  {...}
  sRTF := qryComments.FieldByName( 'DPCOMMENT' ).AsString;
  lRTFFormat := RegisterClipboardFormat( CF_RTF );
  hGlobal := GlobalAlloc( GMEM_MOVEABLE or GMEM_DDESHARE,
    Length( sRTF ) + 1 );
  lpString := GlobalLock( hGlobal );
  StrCopy( lpString, PAnsiChar(sRTF) );
  GlobalUnlock( hGlobal );
  Clipboard.Open;
  Clipboard.SetAsHandle( lRTFFormat, hGlobal );
  Clipboard.Close;
  ocxWordDoc.Paragraphs.Last.Range.Paste;

Now, before I get all jumped over, I realize it's a Unicode issue. I've completely read Chapter 2 of Cantu's 2009 Handbook and I'm still stuck. None of my fixes produce the correct output in the Word document. It might be because of my failed understanding of clipboard formats in Delphi. Can someone help me?

Also, I'd rather NOT use the Clipboard if I can help it, but the rest of the Word document is normal text, and this Rich Text comes from a database.

GIS Programmer
City of Orem, UT
 
In the end, I used the AsAnsiString property of TField and kept the rest of the code the same. It's not an international application (being in-house), so I'm not worried about losing Unicode-specific characters.

GIS Programmer
City of Orem, UT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top