Paste a HTML Table onto a clipboard lazarus / 31-LCL ---------------------------------------------------------------------- Mike.Cornflake · 2010-11-23 09:20 G'day, My environment is Windows XP; Lazarus 0.9.29 SVN27722; FPC 2.4.0 I'm trying to paste a HTML onto the clipboard on the scant chance that it'll paste correctly into Word and Excel (might not work, but hey - worth the effort). Problem is, I'm having difficulties registering the 'text\html' mime type with the Clipboard.   I've never done this before and can't seem to find sample code in the usual places. Here's the code Code: [Select]  oStrings := TStringList.Create;   Try     AppendDataset(oStrings, oDataset);     cfFormat := Clipboard.FindFormatID('text/html');     If cfFormat=0 Then       cfFormat := RegisterClipboardFormat('text/html');     If Clipboard.HasFormat(cfFormat) Then     begin       // hmmm, we never enter here with mimetype 'text/html'       oStream:=TMemoryStream.Create;       try         oStrings.SaveToStream(oStream);         oStream.Position:=0;         Clipboard.SetFormat(cfFormat, oStream);       finally         oStream.Free;       end;     end;   finally     oStrings.Free;   end; If I use 'text/plain' then the above code executes just fine, and the table is on the clipboard, but as text. If I use 'text/html' then the Clipboard.HasFormat always returns false, and by stepping over the code I can confirm that the RegisterClipboardFormat call appears to work and returns a result in the high 40000's. If I comment out the HasFormat reference, then the code executes fine, Clipboard.SetFormat executes without error, but neither notepad/word/excel recognise that there is anything on the clipboard (paste disabled). Anyone any ideas what I'm doing wrong?  Simply incorrect Mimetype, am I misunderstanding RegisterClipboardFormat, or something else? Oh, if anyone has any other ideas on how to get a table onto the clipboard in a format that both word and excel will accept as tables, I'll accept those gratefully :-) Many thanks Mike ---------------------------------------------------------------------- theo · 2010-11-23 09:49 Afaik HasFormat returns whether there is data of a specified format on the CB, not if the format is registered in your app. So before calling AddFormat or SetFormat, it should return false. ---------------------------------------------------------------------- Mike.Cornflake · 2010-11-23 10:00 QuoteAfaik HasFormat returns whether there is data of a specified format on the CB, not if the format is registered in your app. Ahh, that explains that.  Many thanks.  I've finally stumbled across http://msdn.microsoft.com/en- us/library/aa767917%28VS.85%29.aspx which pretty much tells the rest of the story.  Required mimetype is 'html format' and it looks like I need to have a specifically formatted header as well... Many thanks again :-) ---------------------------------------------------------------------- Mike.Cornflake · 2010-11-23 11:26 Finally got it all working.  Found some code in SynExportHTML that helped greatly.  The code to put HTML onto the clipboard is included below in case anyone is interested.  I've never worked with TMemoryStream before, so I used a TStringList to do all the work for me.  That throws in extra #13#10's in case anyone's wondering what all the +2's are for... Code: [Select]procedure PasteDatasetToClipboard(oDataset: TDataset); Var   oStrings : TStringList;   cfFormat : TClipboardFormat;   oStream: TMemoryStream;   iStartHTML, iEndHTML, iStartFragment, iEndFragment: Integer; Const   HTML_MIME = 'HTML Format';   HEADER = '';   FOOTER1 = '';   FOOTER2 = '';   NATIVEHEADER = 'Version:0.9'#13#10 +                  'StartHTML:%.10d'#13#10 +                  'EndHTML:%.10d'#13#10 +                  'StartFragment:%.10d'#13#10 +                  'EndFragment:%.10d'; begin   oStrings := TStringList.Create;   Try     // Load the dataset into the string list     AppendDataset(oStrings, oDataset);     iStartHTML := 105;  // Big thanks to the SynEdit team for their solution to the variable header length problem     iStartFragment := iStartHTML + Length(HEADER) + 2;     iEndFragment := iStartFragment + Length(oStrings.Text) + Length(FOOTER1) + 2;     iEndHTML := iEndFragment + LENGTH(FOOTER2) + 2;     // insert the native header and opening html tags at the start of the string     oStrings.Insert(0, Format(NATIVEHEADER, [iStartHTML, iEndHTML, iStartFragment, iEndFragment]));     oStrings.Insert(1, HEADER);     // append the closing tags to the end of the string list     oStrings.Add(FOOTER1);     oStrings.Add(FOOTER2);     cfFormat := Clipboard.FindFormatID(HTML_MIME);     If cfFormat=0 Then       cfFormat := RegisterClipboardFormat(HTML_MIME);     oStream:=TMemoryStream.Create;     try       oStrings.SaveToStream(oStream);       oStream.Position:=0;       Clipboard.AddFormat(cfFormat, oStream);     finally       oStream.Free;     end;   finally     oStrings.Free;   end; end;  procedure AppendDataset(oDest: TStringList; oDataset: TDataset);   Function Validate(sIn: String): String;   Begin     sIn := FindReplace(sIn, '<', '<');     sIn := FindReplace(sIn, '>', '>');     Result := sIn;   End; Var   iCol:   Integer;   oField: TField;   oBookmark : TBookmark; Begin   oBookmark := oDataset.GetBookmark;   oDataset.DisableControls;   Try     oDest.Add(' 
');     // Add the header     oDest.Add('    ');     For iCol := 0 To oDataset.FieldCount - 1 Do     Begin       oField := oDataset.Fields[iCol];       If oField.Visible Then         oDest.Add('      ');     End;     oDest.Add('    ');     // Add each row     oDataset.First;     While Not oDataset.EOF Do     Begin       oDest.Add('    ');       For iCol := 0 To oDataset.FieldCount - 1 Do       Begin         oField := oDataset.Fields[iCol];         If oField.Visible Then           oDest.Add('      ');       End;       oDest.Add('    ');       oDataset.Next;     End;     oDest.Add(' 
' + Validate(oField.DisplayName) + '
' + Validate(oField.DisplayText) + '
');   Finally     oDataset.GotoBookmark(oBookmark);     oDataset.FreeBookmark(oBookmark);     oDataset.EnableControls;   End; End; ---------------------------------------------------------------------- Mike.Cornflake · 2014-06-30 08:38 Just revisited this code after many years.   Recent changes mainly to do with the CSS.  * now pastes with identical font/size and borders into both Word and Excel (previously Excel looked...  strange). * now preserves carriage returns in the HTML text.  Previously these were stripped.  * now pastes both HTML (for pasting into Word/Excel) and tab separated text (for Notepad).  CR's stripped from the text version... * not tried with Libre/Open office Code: [Select]Interface // Clipboard routines Procedure PasteDatasetIntoClipboard(oDataset: TDataset; bOnlyActiveRow: Boolean = False; bIncludeHeader: Boolean = True); Procedure PasteRowIntoClipboard(oDataset: TDataset; bIncludeHeader: Boolean = True); Implementation Procedure AppendDatasetAsTabSeparatedText(oDest: TStringList; oDataset: TDataset;   bOnlyActiveRow: Boolean; bIncludeHeader: Boolean);   Function Validate(sIn: String): String;   Begin     sIn := FindReplace(sIn, #13, '');     sIn := FindReplace(sIn, #10, '');     sIn := FindReplace(sIn, #09, '');     Result := sIn;   End;   Procedure AddRow;   Var     iCol: Integer;     oField: TField;     sTemp: String;   Begin     sTemp := '';     For iCol := 0 To oDataset.FieldCount - 1 Do     Begin       oField := oDataset.Fields[iCol];       If oField.Visible Then         sTemp := sTemp + Validate(oField.DisplayText) + #9;     End;     oDest.Add(sTemp);   End; Var   iCol: Integer;   oField: TField;   oBookmark: TBookmark;   sTemp: String; Begin   If oDataset.Active Then   Begin     oDataset.DisableControls;     Try       If bIncludeHeader Then       Begin         sTemp := '';         For iCol := 0 To oDataset.FieldCount - 1 Do         Begin           oField := oDataset.Fields[iCol];           If oField.Visible Then             sTemp := sTemp + Validate(oField.DisplayName) + #9;         End;         oDest.Add(sTemp);       End;       If bOnlyActiveRow Then         AddRow       Else       Begin         // Add all rows         oBookmark := oDataset.GetBookmark;         Try           oDataset.First;           While Not oDataset.EOF Do           Begin             AddRow;             oDataset.Next;           End;         Finally           oDataset.GotoBookmark(oBookmark);           oDataset.FreeBookmark(oBookmark);         End;       End;     Finally       oDataset.EnableControls;     End;   End; End; Procedure AppendDatasetAsHTML(oDest: TStringList; oDataset: TDataset;   bOnlyActiveRow: Boolean = False; bIncludeHeader: Boolean = True);   Function Validate(const sIn: String): String;   Begin     Result := FindReplace(sIn, '<', '<');     Result := FindReplace(Result, '>', '>');     Result := FindReplace(Result, #13, '
' + #13);     Result := FindReplace(Result, #10, '');   End;   Procedure AddRow;   Var     iCol: Integer;     oField: TField;   Begin     oDest.Add('    ');     For iCol := 0 To oDataset.FieldCount - 1 Do     Begin       oField := oDataset.Fields[iCol];       If oField.Visible Then         If oField.DataType=ftString Then           oDest.Add('     

' + Validate(oField.DisplayText) + '

')         Else           oDest.Add('     

' + Validate(oField.DisplayText) + '

');     End;     oDest.Add('    ');   End; Var   iCol: Integer;   oField: TField;   oBookmark: TBookmark; Begin   If oDataset.Active Then   Begin     oDataset.DisableControls;     Try       oDest.Add('  ');       If bIncludeHeader Then       Begin         oDest.Add('    ');         For iCol := 0 To oDataset.FieldCount - 1 Do         Begin           oField := oDataset.Fields[iCol];           If oField.Visible Then             oDest.Add('      ');         End;         oDest.Add('    ');       End;       If bOnlyActiveRow Then         AddRow       Else       Begin         // Add all rows         oBookmark := oDataset.GetBookmark;         Try           oDataset.First;           While Not oDataset.EOF Do           Begin             AddRow;             oDataset.Next;           End;         Finally           oDataset.GotoBookmark(oBookmark);           oDataset.FreeBookmark(oBookmark);         End;       End;       oDest.Add(' 

' + Validate(oField.DisplayName) + '

');     Finally       oDataset.EnableControls;     End;   End; End; Procedure PasteDatasetIntoClipboard(oDataset: TDataset; bOnlyActiveRow: Boolean = False;   bIncludeHeader: Boolean = True); Var   oHTML: TStringList;   oTSV: TStringList;   cfHTMLFormat: TClipboardFormat;   oHTMLStream: TMemoryStream;   iStartHTML, iEndHTML, iStartFragment, iEndFragment: Integer; Const   HTML_MIME = 'HTML Format';   HEADER = '' +     '' + '';   FOOTER1 = '';   FOOTER2 = '';   NATIVEHEADER = 'Version:0.9'#13#10 + 'StartHTML:%.10d'#13#10 + 'EndHTML:%.10d'#13#10 +     'StartFragment:%.10d'#13#10 + 'EndFragment:%.10d'; Begin   If oDataset.Active Then   Begin     Clipboard.Clear;     // Add a Tab Separated version to the clipboard for Notepad to use...     oTSV := TStringList.Create;     Try       AppendDatasetAsTabSeparatedText(oTSV, oDataset, bOnlyActiveRow, bIncludeHeader);       ClipBoard.AsText := oTSV.Text;     Finally       oTSV.Free;     End;     // Now append the HTML for Excel etc...     oHTML := TStringList.Create;     Try       // Load the dataset into the string list       AppendDatasetAsHTML(oHTML, oDataset, bOnlyActiveRow, bIncludeHeader);       iStartHTML := 105;       // Big thanks to the SynEdit team for their solution to the variable header length problem       iStartFragment := iStartHTML + Length(HEADER) + 2;       iEndFragment := iStartFragment + Length(oHTML.Text) + Length(FOOTER1) + 2;       iEndHTML := iEndFragment + LENGTH(FOOTER2) + 2;       // insert the native header and opening html tags at the start of the string       oHTML.Insert(0, Format(NATIVEHEADER, [iStartHTML, iEndHTML, iStartFragment,         iEndFragment]));       oHTML.Insert(1, HEADER);       // append the closing tags to the end of the string list       oHTML.Add(FOOTER1);       oHTML.Add(FOOTER2);       // Ensure the HTML mime type is registered       cfHTMLFormat := Clipboard.FindFormatID(HTML_MIME);       If cfHTMLFormat = 0 Then         cfHTMLFormat := RegisterClipboardFormat(HTML_MIME);       // Save the HTML to the clipboard       oHTMLStream := TMemoryStream.Create;       Try         oHTML.SaveToStream(oHTMLStream);         oHTMLStream.Position := 0;         Clipboard.AddFormat(cfHTMLFormat, oHTMLStream);       Finally         oHTMLStream.Free;       End;     Finally       oHTML.Free;     End;   End; End; Procedure PasteRowIntoClipboard(oDataset: TDataset; bIncludeHeader: Boolean); Begin   PasteDatasetIntoClipboard(oDataset, True, bIncludeHeader); End;