Hi Hilary, quick question for you regarding this spell number code. I've used it previously in a form and it worked like a charm however I'm trying to use it in a new form now so I copied and pasted the code out of the functioning form and into the new form. I'm now getting an error when I try to open the new form stating that there is a run-time error 'null' is null or not an object. The error points to line 127 of the code but I can't figure out any issue as it's identical to the code from the previous form!
Below is the code I'm using, (marked offending line in bold) I would really appreciate some insight into what I'm doing wrong!
/*
* This file contains functions for data validation and form-level events.
* Because the functions are referenced in the form definition (.xsf) file,
* it is recommended that you do not modify the name of the function,
* or the name and number of arguments.
*
*/
// The following line is created by Microsoft Office InfoPath to define the prefixes
// for all the known namespaces in the main XML data file.
// Any modification to the form files made outside of InfoPath
// will not be automatically updated.
//<namespacesDefinition>
XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-10-29T15:17:10" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"');
//</namespacesDefinition>
var c_positions = ["", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion"];
var c_digits = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
var c_tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
var c_teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
function spellNum(num, unitsName, hundredthsName)
{
var units = parseInt(num);
var hundredths = parseInt((num * 100 - parseInt(num) * 100));
var numberString = spellNumber(units) + " " + unitsName;
if(units != 1)
{
numberString += "s"
}
numberString += " and " + spellNumber(hundredths) + " " + hundredthsName;
if(hundredths != 1)
{
numberString += "";
}
return numberString;
}
function spellNumber(num)
{
var negative = false;
if (num == 0)
{
return "zero";
}
if (num < 0)
{
num = -num;
negative = true;
}
var numString = "";
var pos = 0;
while (num > 0)
{
if (num % 1000 != 0)
{
numString = spellThreeDigits(num % 1000) + c_positions[pos] + numString;
}
if (parseInt(num / 1000) != 0)
{
numString = ", " + numString;
}
num = parseInt(num / 1000);
pos++;
}
if (negative)
{
numString = "negative " + numString;
}
return numString;
}
function spellThreeDigits(threeDigits){
var hundredsDigit = parseInt(threeDigits / 100);
var tensDigit = parseInt(threeDigits % 100 / 10);
var onesDigit = threeDigits % 10;
var numberString = "";
if (hundredsDigit != 0)
{
numberString += c_digits[hundredsDigit] + " hundred";
if (threeDigits % 100 != 0)
{
numberString += " ";
}
}
return numberString + spellTwoDigits(tensDigit, onesDigit);
}
function spellTwoDigits(tensDigit, onesDigit){
if (tensDigit == 1)
{
return c_teens[onesDigit];
}
else
{
var numberString = c_tens[tensDigit];
if(tensDigit != 0 && onesDigit != 0)
{
numberString += " ";
}
numberString += c_digits[onesDigit];
return numberString;
}
}
//=======
// The following function handler is created by Microsoft Office InfoPath.
// Do not modify the name of the function, or the name and number of arguments.
// This function is associated with the following field or group (XPath): /my:myFields/my:ChequeAmount
// Note: Information in this comment is not updated after the function handler is created.
//=======
function msoxd_my_ChequeAmount::OnAfterChange(eventObj)
{
// Write code here to restore the global state.
if (eventObj.IsUndoRedo || eventObj.Operation != "Insert")
{
// An undo or redo operation has occurred and the DOM is read-only.
return;
}
var amountField = XDocument.DOM.selectSingleNode("/my:myFields/my:ChequeAmount");
var spelledField = XDocument.DOM.selectSingleNode("/my:myFields/my:AmountWords");
spelledField.text = spellNum(amountField.text, "Pound", "Pence");
}