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 forYis 89.So, you are passing a
Charrather than astring. When Word needs to get a string representation of that integer it simply converts the integer89to its textual representation, the string'89'.To get around the problem you can do this:
The idea is that we ensure that we pass a string to
InsertAfter()rather than a character. Remember thatInsertAfter()receives a variant parameter and so you do need to be careful about the type of the payload stored in the variant.