Create a Check in Microsoft Word File From Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

Note: The following tutorial contain a code that will enable you to convert a number to words.

Before you start you need to download the check template (Microsoft Word Template) that I created for this tutorial from the following link

Download Word Check Template

Save the template anywhere in the computer. Then point your code to the downloaded template (look for the following code ("your/check/template"))

Create a Form in Visual Basic and add 4 TextBoxes

Rename your textboxes to:

DateTxt
PaytoText
AmountText
ForText

Then label each textbox with a Label control:

Your form should look kind of like the following image:

Create a check in a word document from vb.net application- vb.net

Now you need to import Microsoft Word 12.0 Object Library. Go to Project - Add Reference - COM - Word 12.0 Object Library

Microsoft Word 12.0 Object Library- vb.net

Add the following code before the Public Class Form1 Line

Imports Word = Microsoft.Office.Interop.Word
Imports System.Math

Add the following code after Public Class Form1 Line

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oneWords As String = ",One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten, _ 
Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen"
Dim ones() As String = oneWords.Split(",")
Dim tenWords As String = ",Ten,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety"
Dim tens() As String = tenWords.Split(",")

Add the following function:

Public Sub Convert(ByVal input As String)
        input = input.Replace("$", "").Replace(",", "")
        If input.Length > 12 Then '"Error in input value"

        Else
            Dim output, dollars, mills, thous, hunds, cents As String
            Dim mill, thou, hund, cent As Integer
            If input.IndexOf(".") > 0 Then
                dollars = input.Substring(0, input.IndexOf(".")).PadLeft(9, "0")
                cents = input.Substring(input.IndexOf(".") + 1).PadRight(2, "0")
                If cents = "00" Then cents = "0"
            Else
                dollars = input.PadLeft(9, "0") : cents = "0"
            End If
            mill = CType(dollars.Substring(0, 3), Integer) : mills = convertHundreds(mill)
            thou = CType(dollars.Substring(3, 3), Integer) : thous = convertHundreds(thou)
            hund = CType(dollars.Substring(6, 3), Integer) : hunds = convertHundreds(hund)
            '  cent = CType(cents, Integer) : cents = convertHundreds(cent)
            output = IIf(mills.Trim = "", "", mills + " Million ")
            output += IIf(thous.Trim = "", "", thous + " Thousand ")
            output += IIf(hunds.Trim = "", "", hunds)
            output = IIf(output.Length = 0, "Zero Dollars and ", output + " and ")
            output = IIf(output = "One Dollars and ", "One Dollar and ", output)
            output += IIf(cents = "", "Zero", cents) + "/100"
            AmountText.Tag = output
        End If

    End Sub

    Private Function convertHundreds(ByVal input As Integer) As String
        Dim output As String
        If input <= 99 Then
            output = (convertTens(input))
        Else
            output = ones(Floor(input / 100))
            output += " Hundred "
            If input - Floor(input / 100) * 100 = 0 Then
                output += ""
            Else
                output += "" + convertTens(input - Floor(input / 100) * 100)
            End If
        End If
        Return output
    End Function

    Private Function convertTens(ByVal input As Integer) As String
        Dim output As String
        If input < 20 Then
            output = ones(input)
            input = 0
        Else
            output = tens(CType(Floor(input / 10), Integer))
            input -= Floor(input / 10) * 10
        End If
        output = output + IIf(ones(input).Trim = "", "", "-" + ones(input))
        Return output
    End Function
 

Add a Button and add the following code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            AmountText.Text = FormatNumber(AmountText.Text, 2)
        Catch ex As Exception
        End Try

        oWord = CreateObject("Word.Application")

        oDoc = oWord.Documents.Add("your/check/template") 'Change to the downloaded template path
        oDoc.Bookmarks.Item("Date").Range.Text = DateTxt.Text
        oDoc.Bookmarks("Payee").Range.Text = PayToText.Text
        oDoc.Bookmarks("NumberAmount").Range.Text = AmountText.Text
        Convert(AmountText.Text)
        oDoc.Bookmarks("For").Range.Text = ForText.Text
        oDoc.Bookmarks("WordAmount").Range.Text = AmountText.Tag
        oWord.Visible = True
    End Sub


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.