Discussion:
Copy UTF8 Encoded string to Clipboard?
(too old to reply)
norberto
2006-04-08 10:29:49 UTC
Permalink
Hi,

i have an application which needs to have UTF8 encoded characters (Chinese)
copied from a MS Word document to the clipboard so the text can be used by other
procedures.

Does anyone know how to do this and, if so, can you provide some example code?
Currently, i can copy standard ansi text from a Word document to the clipboard,
but it won't return the UTF8-encoded characters properly.

Thanks in advance for any insights or other help.
danny heijl
2006-04-08 11:08:06 UTC
Permalink
Post by norberto
i have an application which needs to have UTF8 encoded characters
(Chinese) copied from a MS Word document to the clipboard so the text
can be used by other procedures.
This is how I copy unicode text to the clipboard for use in MSWord.

You could do the reverse and convert the resulting widestring to utf8.

Danny
---

var
l: TWideStrings;
s: WideString;
// clipboard
Data: THandle;
DataPtr: Pointer;
Size: integer;

....

// Copy Unicode text to Clipboard -- DH 20-09-2002 --
s := l.Text;
Size := Length(l.Text) * 2;
Clipboard.Open;
try
Data := GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, Size);
try
DataPtr := GlobalLock(Data);
try
Move(s[1], DataPtr^, Size);
Clipboard.Clear;
SetClipboardData(CF_UNICODETEXT, Data);
finally
GlobalUnlock(Data);
end;
except
GlobalFree(Data);
raise;
end;
finally
Clipboard.Close;
end;
norberto
2006-04-10 01:12:38 UTC
Permalink
Post by danny heijl
You could do the reverse and convert the resulting widestring to utf8.
Thank you Danny.

Since i'm not familiar with the clipboard, do you have any example code on how
to do this? i've tried:

function ReturnDocParagraphString: widestring;
var
Data: THandle;
Size: Integer;
const
{Constants for enum WdUnits }
wdCharacter = $00000001;
wdParagraph = $00000004;
{Constants for enum WdMovementType}
wdExtend = $00000001;
begin
wrdApp.Selection.EndOf(Unit:=wdParagraph, Extend:=wdExtend);
wrdApp.Selection.Copy;
Size := Length(ClipBoard.AsText) * 2;
Data := GlobalAlloc(GMEM_DDESHARE + GMEM_MOVEABLE, Size);
SetClipboardData(CF_UNICODETEXT, Data);
Result := UTF8Encode(ClipBoard.AsText);
end;


But this doesn't work. As you can see, i'm really shooting in the dark. i paste
the result of this function into a TTntMemo but all i see is question marks (eg:
??????...)

Any ideas? Thanks again for your help.

norberto
danny heijl
2006-04-10 18:17:44 UTC
Permalink
Post by norberto
function ReturnDocParagraphString: widestring;
var
Data: THandle;
pws: PWideChar;
ws: widestring;
l: integer;
Post by norberto
Size: Integer;
const
{Constants for enum WdUnits }
wdCharacter = $00000001;
wdParagraph = $00000004;
{Constants for enum WdMovementType}
wdExtend = $00000001;
begin
wrdApp.Selection.EndOf(Unit:=wdParagraph, Extend:=wdExtend);
wrdApp.Selection.Copy;
Data := GetClipBoardData(CF_UNICODETEXT);
pws := GlobalLock(Data);
l := WideStrLen(pws);
SetString(ws, pws, l);
GlobalUnlock(Data);
Result := UTF8Encode(ws);
Post by norberto
end;
oslt, and you may have to provide WideStrLen.

Danny
---
norberto
2006-04-11 09:00:04 UTC
Permalink
Post by danny heijl
Post by norberto
function ReturnDocParagraphString: widestring;
var
Data: THandle;
pws: PWideChar;
ws: widestring;
l: integer;
Post by norberto
Size: Integer;
const
{Constants for enum WdUnits }
wdCharacter = $00000001;
wdParagraph = $00000004;
{Constants for enum WdMovementType}
wdExtend = $00000001;
begin
wrdApp.Selection.EndOf(Unit:=wdParagraph, Extend:=wdExtend);
wrdApp.Selection.Copy;
Data := GetClipBoardData(CF_UNICODETEXT);
pws := GlobalLock(Data);
l := WideStrLen(pws);
SetString(ws, pws, l);
GlobalUnlock(Data);
Result := UTF8Encode(ws);
Post by norberto
end;
oslt, and you may have to provide WideStrLen.
Danny
---
Hi again Danny,

Thanks for your help and the code. i'm having a difficult time with the
WideStrLen function (making or finding one that works). Do you have a code
example of the WideStrLen function?

i've tried using Delphi's Length function, as well as:

function WideStrLen(CharsP:PWideChar):Cardinal;
begin
result := strlenW(CharsP);
end;

and

// code by Karl Waclawek under BSD License
function WideStrLen(CharsP: PWideChar): Integer;
var
EndCharP: PWideChar;
begin
EndCharP := CharsP;
while EndCharP^ <> WideChar(#0) do Inc(EndCharP);
Result := EndCharP - CharsP;
end;

but get errors all the time in execution of the WideStrLen function.

Sorry for being so bothersome.

Thanks again.

norberto
danny heijl
2006-04-12 20:28:52 UTC
Permalink
Post by norberto
but get errors all the time in execution of the WideStrLen function.
What errors? That code in WideStrLen is correct and works if you pass it
a null-terminated widechar pointer.


Danny
---

Loading...