Monday, August 24, 2026
Home Hackathon VB Code to convert Digit to Text in MS Word Document

VB Code to convert Digit to Text in MS Word Document

Copy the code below by creating a new Macro to convert:

To add shortcut for this macro, you shall follow this article that I’ve written in prior.

Sub DigitToText()
    Dim datasagar_Digits As String
    Dim datasagar_NumText As String

    datasagar_NumText = ""

    ' Select Number from Word Document 
    Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdMove
    Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend

    ' Store digits in variable named datasagar_Digits
    datasagar_Digits = Trim(Selection.Text)

    If Val(datasagar_Digits) > 999999 Then
        If Val(datasagar_Digits) <= 999999999 Then
            datasagar_NumText = Trim(Int(Str(Val(datasagar_Digits) / 1000000)))
            ' For selection range, create a field containing the digits and the cardtext format flag
            Selection.Fields.Add Range:=Selection.Range, _
              Type:=wdFieldEmpty, Text:="= " + datasagar_NumText + " \* CardText", _
              PreserveFormatting:=True

            ' Select the previously created field and copy it
            Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
            datasagar_NumText = Selection.Text & " million "
            datasagar_Digits = Right(datasagar_Digits, 6)
        End If
    End If
    If Val(datasagar_Digits) <= 999999 Then
        ' Create a field containing that digits and the cardtext format flag
        Selection.Fields.Add Range:=Selection.Range, _
          Type:=wdFieldEmpty, Text:="= " + datasagar_Digits + " \* CardText", _
          PreserveFormatting:=True

        ' Select the field and copy it
        Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
        datasagar_Digits = datasagar_NumText & Selection.Text

        ' Replace number with words in the selected area of word document
        Selection.TypeText Text:=datasagar_Digits
        Selection.TypeText Text:=" "
    Else
        MsgBox "Number too large", vbOKOnly
    End If
End Sub

datasagarhttp://www.DataSagar.com
Sagar is multidisciplinary technologist, educator, and entrepreneur based in Nepal. As the founder of Illionso Technologies and Kashi Garden Resort, he operates at the intersection of web architecture, data intelligence, innovation, and digital transformation. From building digital solutions to scaling real-world concepts, his mission is to share knowledge alongside merge emerging as well as disruptive technologies with transformative offline experiences.
RELATED ARTICLES

SynthID and C2PA Explained

Welcome to another AI-focused article here on datasagar.com! As I scroll through my social media feeds lately, I find myself playing a...

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

Most Popular

SynthID and C2PA Explained

Welcome to another AI-focused article here on datasagar.com! As I scroll through my social media feeds lately, I find myself playing a...

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

What is Answer Engine Optimization (AEO)? Getting Ready for AI Powered Search and Agentic Era Digital Marketing

Search is changing faster than it has in the last two decades. For years, businesses focused on ranking their websites on search...