Spellnumber in InfoPath - InfoPath Dev
in

InfoPath Dev

Spellnumber in InfoPath

Last post 09-14-2015 07:20 AM by Hilary Stoupa. 44 replies.
Page 1 of 3 (45 items) 1 2 3 Next >
Sort Posts: Previous Next
  • 10-27-2008 04:10 AM

    Spellnumber in InfoPath

    Hello,

     

    Does anyone know if there is any way to convert a number to words within InfoPath 2003?  I am transferring a form that is currently in Excel into InfoPath that has 2 fields - an "amount" field and an "amount in words" field.  In Excel I've utilized "spellnumber" code I found on the web but can't find any reference anywhere to InfoPath being able to complete the same function.  I would assume code would be required but can't find anything on the web.  I'm not terribly familiar with writing code but if someone could provide a sample (or has a different suggestion), it would be hugely appreciated.

  • 10-27-2008 05:06 AM In reply to

    Re: Spellnumber in InfoPath

    I am not aware of such a function in InfoPath, but I've thrown together this C# that can convert any number from -10^16 - 1 to 10^16 - 1 into words.  Just pass the number into SpellNum and it will return the number in words as a string:

    http://www.infopathdev.com/blogs/jimmy/misc/SpellNum.txt

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 10-27-2008 05:19 AM In reply to

    Re: Spellnumber in InfoPath

    Hi Jimmy,  thanks for replying so quickly.  I'm not familiar with C+ at all, is this something I can put into InfoPath to use in my form?  If so, some guideance on how to do so would be much appreciated!

  • 10-27-2008 08:19 AM In reply to

    Re: Spellnumber in InfoPath

    Sure, I can give you a quick example of how to do this. Note that this example will only work with InfoPath 2007, so I hope you have that version :-)

     1. Make sure that you have Visual Studio Tools for Applications installed.  This is available as part of the Office 2007 installation but you might not have it installed yet.

    2. Create a form with two fields: my:number, and my:spelledNumber.  Represent my:number on your form as a text box, and my:spelledNumber as an expression box.

    3. Go to Tools->Form Options...->Programming and select C# for the form template programming language.

    4. Right-click my:number's text box, and under Programming, choose Changed event...  You might be prompted to save your template, and after that, Visual Studio Tools for Applications will open.

    5. Download the file I linked to in my previous post, save it anywhere, and change the extension so it is called "SpellNum.cs"

    6. Go back to Visual Studio Tools for Applications, and under the Project menu, select Add Existing Item... and add SpellNum.cs, which you just downloaded.

    7.  Go to the top of your FormCode.cs file, which should be the only one open, and under the several lines that begin with using, add the line:

    using Spell;

    This tells your FormCode.cs file that it should be able to access the functions inside SpellNum.cs

    8.  Now go to the number_Changed event handler at the bottom of FormCode.cs, and add code so that it ends up looking like this.  The green lines explain what each line of code does:

    public void number_Changed(object sender, XmlEventArgs e)
    {
      
    // create an object to navigate the main data source
       XPathNavigator nav = MainDataSource.CreateNavigator();
       // create an object to access the my:number node
       XPathNavigator number = nav.SelectSingleNode("/my:myFields/my:number", NamespaceManager);
       // create an object to access the my:spelledNumber node
       XPathNavigator spelledNumber = nav.SelectSingleNode("/my:myFields/my:spelledNumber", NamespaceManager);
       // enclose the number handling code in a try block, to catch any error from parsing the number
       try
       {
          // parse the value of the my:number node and store it in a variable
          long numVar = Int64.Parse(number.Value);
          // convert the number into its spelled form and store it in a variable
          string spelledNumVar = SpellNumbers.SpellNum(numVar);
          // store the value in the my:spelledNumber node
          spelledNumber.SetValue(spelledNumVar);
       }
       // catch any parsing error (will happen if my:number contains something other than an integer
       catch (FormatException)
       {
          // Show an error to the user
          MessageBox.Show("Please enter a valid number!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
    }

    It may look confusing now, but once you make sense of it, you can reuse it with little modification.

    9.  Now under the Debug menu of Visual Studio Tools for Applications, select Start Debugging.  This should bring up the InfoPath form.  Try typing a number into the text box and then clicking off the text box.  If all went well, the expression box should show the spelled number.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 10-27-2008 10:17 AM In reply to

    Re: Spellnumber in InfoPath

    Thanks Jimmy, unfortunately I'm only using IP 2003 at the moment but will hold onto this suggestion for future use.
  • 10-27-2008 08:38 PM In reply to

    Re: Spellnumber in InfoPath

    I see. In that case the procedure would be about the same, except that the code in step 8 would be more like this:

    public void number_OnAfterChange(DataDOMEvent e)
    {
         if (e.IsUndoRedo)
         {
              // An undo or redo operation has occurred and the DOM is read-only.
              return;
         }
         // retrieve an instance of the main DOM
         IXMLDOMDocument mainDom = thisXDocument.DOM;
         // store the my:number node in a variable
         IXMLDOMNode numberNode = mainDom.selectSingleNode("/my:myFields/my:number");
         // store the my:spelledNumber node in a variable
         IXMLDOMNode spelledNumberNode = mainDom.selectSingleNode("/my:myFields/my:spelledNumber");
         // enclose the number handling code in a try block, to catch any error from parsing the number
         try
         {
              // parse the value of the my:number node and store it in a variable
              long numVar = Int64.Parse(numberNode.text);
              // convert the number into its spelled form and store it in a variable
              string spelledNumVar = SpellNumbers.SpellNum(numVar);
              // store the value in the my:spelledNumber node
              spelledNumberNode.text = spelledNumVar;
         }
         // catch any parsing error (will happen if my:number contains something other than an integer
         catch (FormatException)
         {
              // Show an error to the user
              thisXDocument.UI.Alert("Please enter a valid number!");
         }
    }

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 10-31-2008 09:20 AM In reply to

    Re: Spellnumber in InfoPath

    Hi Jimmy, thanks again for your help but unfortunately I don't appear to have Visual Studio and am not having much luck with my IT department.  Is there a way to do this in IP 2003 using Jscript or VBScript?  I must admit I'm not really a developer but am in the unfortunate position of knowing more about InfoPath than my IT department so your help is greatly appreciated.

    If it helps to have a bit more detail, the form I am designing is a cheque request so I have two fields: my:ChequeAmount and my:AmountWords and I would like to set it up so that entering the amount in my:ChequeAmount spells the amount in my:AmountWords

    Thanks again!

  • 11-06-2008 06:14 AM In reply to

    Re: Spellnumber in InfoPath

    Hello Ben,

     Sorry for taking so long to reply.  I had not realized that IP2003 doesn't come with VSTA but that indeed seems to be the case.

    This task should be entirely possible in either scripting language, but as I'm a bit weak in JScript and know almost no VBScript I can't convert the code right off the top of my head.  Perhaps I can work something up within the next few days.

    More soon,
    Jimmy

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-13-2008 08:24 AM In reply to

    Re: Spellnumber in InfoPath

    Hi Jimmy,

    I really appreciate your help with this.  I've managed to get MS Visual Studio 2005 from my IT department but I'm unfortunately still stuck.  Using Visual Studio, I can get through steps 1 -7 of your instructions but then I'm a bit lost.  As I'm using Visual Studio seperately rather than within InfoPath, I'm not sure how/where to insert the code from step 8 and then how to link it to my fields within my InfoPath form?

    Thanks again, let me know if you need any more info!

  • 11-14-2008 08:34 AM In reply to

    Hello again,

     Sorry for putting you to the trouble of getting VS2005 from your IT department.  Unfortunately I'm not sure there's a way to use Visual Studio to add a code component to an existing form template, but actualy, I have come up with a JScript version of my SpellNumber code.

     To use it, set up your template to use JScript, open up the script editor and paste the contents of the attached text file into the window, but outside of any functions (sections that start with function and end with a closing curly brace }

    Then to actually use the spellNum function, you can right-click the field for my:ChequeAmount in the InfoPath designer, and under the Programming context menu item, select "On After Change Event".  This will create an event handler in the JScript editor and inside that handler you would use code something like this:

    var amountField = XDocument.DOM.selectSingleNode("/my:myFields/my:ChequeAmount");
    var spelledField = XDocument.DOM.selectSingleNode("/my:myFields/my:AmountWords");
    spelledField.text = spellNum(amountField.text, "dollar", "cent");

    I hope that's clear enough, but let me know if you need any more clarification.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-14-2008 09:27 AM In reply to

    Hi Jimmy, thanks again but I'm still having some trouble. Unfortunately I'm not really a programmer so I'm only just learning how to use/deal with code.  I'm sure I've probably just got something simple wrong or it may be due to slight differences between IP2003 and 2007?  My template is setup in Jscript programming language and I opened up the script editor as indicated and pasted your spellnumber code but don't have a programming option when I right click on the my:ChequeAmount field?  I tried setting up the script by going through the programming menu and adding an "On Load" event or a "On context Change Event" but when I try to preview the form to see if the code worked, I'm getting an error that the form template is not valid.  I've attached a copy of the code the way I've put it in - I'm sure I've probably done something small wrong that's causing the error.  If you could take a look and perhaps see where I've gone wrong it would be hugely appreciated - especially as it helps me understand how the code works in the future!

  • 11-14-2008 10:19 AM In reply to

    Re: Spellnumber in InfoPath

    I added Jimmy's script to a sample template available here. Download it and give it a try -- the script works and you can see it in the script editor. The fields have slightly different names, but it should give you the idea, okay?

    Hilary Stoupa

  • 11-17-2008 01:25 AM In reply to

    Re: Spellnumber in InfoPath

    Thanks again guys!  I'm not on IP 2007 yet but we're getting it soon so I'll hang onto this until then :)

  • 11-17-2008 11:12 AM In reply to

    Re: Spellnumber in InfoPath

    Sorry! Silly me. Here's an IP 2003 version.

    Hilary Stoupa

  • 11-19-2008 06:15 AM In reply to

    Re: Spellnumber in InfoPath

    You're a star Hilary!  Thanks again to you both for all your help :)

Page 1 of 3 (45 items) 1 2 3 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.