简体   繁体   English

从Delphi输入文本到Word

[英]Entering Text From Delphi To Word

I'm using Delphi XE2 and use the following code to enter the letter Y into a bookmark in a Word (2010) template. 我正在使用Delphi XE2并使用以下代码将字母Y输入到Word(2010)模板中的书签中。

Doc.Bookmarks.Item('NS').Range.InsertAfter('Y');

Except in the document, instead of the letter Y, the number 89 appears. 除文件外,代替字母Y,出现数字89。

Is the fault likely to be from my code or in the Word document? 该错误可能来自我的代码或Word文档吗? Any direction gratefully received. 任何方向感激不尽。

Your literal 'Y' is a character literal rather than a string string literal. 您的文字'Y'是字符文字,而不是字符串字符串文字。 The ASCII code for Y is 89 . YASCII码是89

So, you are passing a Char rather than a string . 所以,你传递的是Char而不是string When Word needs to get a string representation of that integer it simply converts the integer 89 to its textual representation, the string '89' . 当Word需要获得该整数的字符串表示时,它只是将整数89转换为其文本表示,即字符串'89'

To get around the problem you can do this: 要解决此问题,您可以执行以下操作:

var
  Text: string;
....
Text := 'Y';
Doc.Bookmarks.Item('NS').Range.InsertAfter(Text);

The idea is that we ensure that we pass a string to InsertAfter() rather than a character. 我们的想法是确保将字符串传递给InsertAfter()而不是字符。 Remember that InsertAfter() receives a variant parameter and so you do need to be careful about the type of the payload stored in the variant. 请记住, InsertAfter()接收变量参数,因此您需要注意变量中存储的有效负载的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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