如何使用 Delphi 7 从 MSWord 2003 读取/写入本地字符?
我的表单上有 ListView,其中包含姓名和数字,我必须提供打印 MSWord 文档,并将这些数据填充到文档的表格中。英语字符一切正常,但当我尝试发送一些东欧或俄语字符时,它在文档中显示为“?”或者一些“垃圾”。我也无法将这些字符从文档读回应用程序。
我的问题是:
- 如何将
"ЉЊĐŠŽČ"
等字符发送到 Word 文档? - 如何将这些字符从 MSWord 读回应用程序?
简而言之,代码如下所示:
word := CreateOleObject('Word.Application');
word.Visible := true;
doc := word.documents.Open(ExtractFilePath(Application.ExeName) + '\tpl.doc');
table := word.ActiveDocument.Tables.Item(1);
table.Cell(1,2).Range.Text := 'MY TEXT';
word.ActiveDocument.Close;
word.Quit;
word := UnAssigned;
doc := UnAssigned;
table := UnAssigned;
我可以更改字体的 name
、size
和 color
属性,但不能使用 charset 做到这一点
属性。
有人吗?
安装的软件:
- Windows XP Professional
- Microsoft Word 2003
- Delphi 7 Enterprise Edition
I have ListView on my form containing names and numbers and I have to provide printing MSWord document with those data filled into document's tables. Everything works fine with english characters but when I try to send some eastern European or Russian characters it is visible in document as "?" or some "trash". Also I can't read those characters from document back to application.
My questions are:
- How to send characters like
"ЉЊĐŠŽČ"
to Word document? - How to read these characters from MSWord back to application?
In short, code looks like this:
word := CreateOleObject('Word.Application');
word.Visible := true;
doc := word.documents.Open(ExtractFilePath(Application.ExeName) + '\tpl.doc');
table := word.ActiveDocument.Tables.Item(1);
table.Cell(1,2).Range.Text := 'MY TEXT';
word.ActiveDocument.Close;
word.Quit;
word := UnAssigned;
doc := UnAssigned;
table := UnAssigned;
I can change font's name
, size
and color
properties but can't do that with charset
property.
Anybody?
Software installed:
- Windows XP Professional
- Microsoft Word 2003
- Delphi 7 Enterprise Edition
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题源于您使用后期绑定通过 OLE 自动化调用 Word。
因此,在 Delphi 7 下,
Range.Text
不被认为是期望 WideString (Unicode) 内容的方法,而是纯 ASCII 文本。第一个解决方案可能是使用 Delphi 2009 及更高版本。新的
string
类型使这种 Unicode 分配变得透明。在 Delphi 7 下,如何强制类型转换为 WideString:
或使用临时变量,如下所示:
另一种可能性是不使用后期绑定,而是直接声明 Office 的 OLE 接口,导入“Microsoft Word ??? 对象库”,来自 IDE 的“项目”菜单。
您将在导入的接口中拥有宽字符串类型,例如:
因此您将不再遇到任何 Ansi 字符集问题。
The issue comes from the fact that you're calling Word via OLE Automation using late binding.
So
Range.Text
is not known as a method expecting a WideString (Unicode) content, but plain ASCII text, under Delphi 7.First solution could be to use Delphi 2009 and later. The new
string
type made such Unicode assignment transparent.Under Delphi 7, what about forcing the type cast to WideString:
or using a temporary variable, like this:
Another possibility could be to use not late-binding, but direct declaration of the OLE interface of Office, importing the "Microsoft Word ??? Object library" from the "Project" menu of the IDE.
You'll have widestring types in the imported interfaces, e.g:
So you won't have any issue with Ansi charset any more.