1

我正在尝试将 WideString 文本从数据库(ADO / MS Access)导出到 MS Word 文档(Delphi 7),但未正确传输外来字符(即“ è ”而不是“ č ”):

while not ADOQuery1.Eof do
begin
  WordApplication1.Selection.TypeText(ADOQuery1Text.AsVariant); // TWideStringField
  WordApplication1.Selection.TypeParagraph;
  ADOQuery1.Next;
end;

我也尝试过CreateOleObject()直接使用,但没有区别。

我错过了什么?

谢谢!

4

2 回答 2

3

I think it's not a problem with Word but rather with the way the strings are stored in the database. They are probably saved as Ansi strings, not as Unicode/WideString strings. And if that is true, then they are saved in some encoding which you must know if you want them to be decoded correctly.

Here is a sample application demonstrating how to convert Ansi string into WideString and save it in Word:

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  ComObj,
  ActiveX,
  CodecUtilsWin32;

procedure Test();
var
  wordApp, wordDoc: Variant;
  ansiStr: string;
  codec: TUnicodeCodec;

  function str2WideStr(const s: string): WideString;
  var
    i: Integer;
  begin
    codec.DecodeStr(@s[1], Length(s), Result);
  end;

begin
  codec := TEncodingRepository.CreateCodecByAlias('ISO-8859-2');

  ansiStr := #$BF#$F3#$B3#$E6; //"zólc"

  wordApp := CreateOleObject('Word.Application'); 
  wordDoc := wordApp.Documents.Add;
  wordApp.Selection.TypeText(str2WideStr(ansiStr));
  wordDoc.SaveAs('C:\sample.doc');
  wordDoc.Close();
  wordApp.Quit(False);
end;

begin
  CoInitialize(nil);
  Test();
end.

The code above uses freeware unit CodecUtilsWin32.pas from Utility Library v.2.0.18

So I'd suggest using TStringField instead of TWideStringField and converting the strings to WideStrings as in the above example.

于 2009-06-05T21:51:49.420 回答
0

Have you tried using

WordApplication1.Selection.TypeText(ADOQuery1Text.AsWideString); 

Short of that, I know that Delphi 2009 has better handling of Unicode (entire VCL now supports it directly) that would most likely correct your problem.

于 2009-06-02T16:23:49.197 回答