简体   繁体   中英

MS Word Ole Automation, ADO and foreign characters

I'm trying to export WideString texts from the database (ADO / MS Access) to the MS Word document (Delphi 7), but foreign characters are not correctly transferred (ie " " instead of " "): ”而不是“ ”):

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

I've also tried to use CreateOleObject() directly, but no difference.

What am I missing?

Thanks!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM