Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Number Speller COM-ponent

0.00/5 (No votes)
18 Feb 2009 1  
Component used to convert a numeric value into English/Romanian words in Excel

The original article: COM Number Speller.

Introduction

Some time ago, I needed to print numbers as text in Excel (Office 2003 as far as I remember). Searching the internet, I found a solution for English language, but not for Romanian language.

So, I created a reusable piece of code that may be easily used in as many environments as possible. I chose COM and ATL to be the solution to this problem. This is also a good programming exercise for my rusty COM / C++ skills and I plan to use it as a template for all my future COM objects.

Using the Code  

To install the COM object, just simply run Install.bat in NumberSpeller.zip archive.
Here is the Excel macro that makes use of the NumberSpeller COM object:

Function Spell(n As Currency) As String
'Create the speller object.
Dim s As NumberSpellerLib.speller
Set s = New NumberSpellerLib.speller

Dim o As Object
Set o = s
o.Language = "ro"
Spell = o.Translate(n)
End Function

Here is another example of using the component from WSH script:

// Create the speller object.
var speller = new ActiveXObject("NumberSpeller.Speller");

// Set Romanian language
speller.language = "en";

////////////////////////////////////////////
// Modify the  number constant below.
var numberToTranslate = 101001;

// Translate number to text.
try
{
    var textNumber = speller.Translate(numberToTranslate);
    WScript.Echo(textNumber);
}
catch (e)
{
    // Display details about any thrown exceptions.
    WScript.Echo(e.name + ": " + e.description + "  " + e.number);
}

Points of Interest

  • Error info support by implementing IErrorInfo interface.
  • Help in CHM format and context identifiers specified in MIDL source file.
  • BSTR manipulation using CComBSTR class provided by ATL
  • IDispatch support, so the component can be used from scripting environments.
  • Spelling implementation itself and support for multiple languages (only Romanian and English for now).  

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here