Entering Text From Delphi To Word

delphi, delphi-xe2, ms-word

Solution

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.

Problem

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.

Original source