Recent

Author Topic: Clipboard formatted text  (Read 2684 times)

Paolo

  • Hero Member
  • *****
  • Posts: 510
Clipboard formatted text
« on: July 11, 2021, 07:16:53 pm »
The attempt is to copy text in clipboard with "Tab" separator for successive Ctrl+V in word document, then selecting it inside Word add a grid around that text.
I did the code below and this works

Code: Pascal  [Select][+][-]
  1. var
  2.   StrGrdExc : TStringGrid;
  3.  
  4. procedure TFrmMain.ToolButton4Click(Sender: TObject);
  5. var
  6.   iR, iC : integer;
  7.   Str1 : string;
  8. begin
  9.   Clipboard.AsText:='';
  10.   for iR:=1 to StrGrdExc.RowCount-1 do begin
  11.     Str1:='';
  12.     for iC:=1 to StrGrdExc.ColCount-1 do begin
  13.       Str1:=Str1+StrGrdExc.Cells[iC, iR];
  14.       if iC <> StrGrdExc.ColCount-1 then
  15.         Str1:=Str1+#9
  16.     end;
  17.     Clipboard.AsText:=Clipboard.AsText+Str1+#13#10;
  18.   end;
  19. end;
  20.  
  21.  

is that the right way to do that ?

(Windows 10, LAZ 2.0.12, FPC 3.0.2)

thank you in advance.


wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Clipboard formatted text
« Reply #1 on: July 11, 2021, 07:21:50 pm »
I would use a temporary string variable rather than repeatedly calling Clipboard.AsText.

Code: Pascal  [Select][+][-]
  1. procedure TFrmMain.ToolButton4Click(Sender: TObject);
  2. var
  3.   clipboardStr: String;
  4.   ...
  5. begin
  6.   clipboardStr := '';
  7.   for iR := 1 to StdGrdExc.RowCount-1 do begin
  8.     ...
  9.     clipboardStr := clipboardStr + Str1 + LineEnding;
  10.   end;
  11.   Clipboard.AsText := clipboardStr;
  12. end;

Paolo

  • Hero Member
  • *****
  • Posts: 510
Re: Clipboard formatted text
« Reply #2 on: July 11, 2021, 07:29:51 pm »
thanks, the updated code working perfectly.

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TFrmMain.ToolButton4Click(Sender: TObject);
  3. var
  4.   iR, iC : integer;
  5.   Str1 : string;
  6. begin
  7.   Str1:='';
  8.   for iR:=1 to StrGrdExc.RowCount-1 do begin
  9.     for iC:=1 to StrGrdExc.ColCount-1 do begin
  10.       Str1:=Str1+StrGrdExc.Cells[iC, iR];
  11.       if iC <> StrGrdExc.ColCount-1 then
  12.         Str1:=Str1+#9
  13.     end;
  14.     Str1:=Str1+LineEnding;
  15.   end;
  16.   ClipBoard.AsText:=Str1
  17. end;
  18.  

 

TinyPortal © 2005-2018