簡體   English   中英

MS Word Ole Automation,ADO和外來字符

[英]MS Word Ole Automation, ADO and foreign characters

我正在嘗試將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() ,但沒有區別。

我想念什么?

謝謝!

我認為這不是Word的問題,而是字符串在數據庫中存儲方式的問題。 它們可能保存為Ansi字符串,而不是Unicode / WideString字符串。 如果是這樣,則它們將以某種編碼保存,您必須知道是否希望它們正確解碼。

這是一個示例應用程序,演示如何將Ansi字符串轉換為WideString並將其保存在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.

上面的代碼使用實用程序庫v.2.0.18中的免費軟件單元CodecUtilsWin32.pas

因此,我建議使用TStringField而不是TWideStringField並將字符串轉換為WideStrings,如上面的示例所示。

您是否嘗試過使用

WordApplication1.Selection.TypeText(ADOQuery1Text.AsWideString); 

簡而言之,我知道Delphi 2009具有更好的Unicode處理能力(整個VCL現在直接支持它),很可能會解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM