简体   繁体   中英

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.

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

Except in the document, instead of the letter Y, the number 89 appears.

Is the fault likely to be from my code or in the Word document? Any direction gratefully received.

Your literal 'Y' is a character literal rather than a string string literal. The ASCII code for Y is 89 .

So, you are passing a Char rather than a 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' .

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. 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.

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