Click here to Skip to main content
12,693,254 members (32,568 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked

FormatMessage-like formatting in VBScript

, 11 Feb 2002 Public Domain
Rate this:
Please Sign up or sign in to vote.
A simple function to do FormatMessage-like formatting in VBScript


Recently I've been forced to do a lot of ASP coding using VBScript, and immediately I sorely missed the slot-based string formating functions available in most other languages.

In Python, for instance, you could do this:

result = "Hello %s! How are you this %s?" % (name, "morning")

Well, even more useful is to have named, or indexed, slots so you can do I18N without code changes, or reuse parameters without repeating them in the argument list. The SDK function FormatMessage does this for you if you are programming in a "real" language.

I decided to whip up a very simple FormatMessage look-alike for my own use. I'm sure most everybody coding in VBScript has done something similar. In case you haven't done so already, feel free to use this code. Enjoy!


Well, here it is in all it's glory.

function Fmt(str, args)
    dim res
    res = str

    dim i
    for i = 0 to UBound(args)
        res = Replace(res, "%"&CStr(i+1)&"%", args(i))

    res = Replace(res, "\n", vbCrLf)
    res = Replace(res, "\t", vbTab)

    fmt = res
end function

You call it like so:

str = Fmt("<%1%>This is a %2%</%1%>", Array("div", "test"))

That's all there is to it. You use slot markers numbered from 1 placed between %-signs, e.g. %1%. If you use more slots than you have arguments the extraneuos ones will simply be left in the string. The arguments are passed in an Array. Since VBScript only uses VARIANT-types, you can put anything in here and the script engine will do it's best to convert it to a string.

I also added the convenience of using \t and \n for Tab and NewLine respectively.


This code is somewhat similar to an article posted by Uwe Keim a long time ago. My approach is considerably simpler and, I think, more efficient.

If you have any comments on this article, you can contact me at


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Sven Axelsson
Web Developer
Sweden Sweden
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralOld wine in Old Bottle! Pin
SDX-{5A853460-2944-42f8-84B8-2432DE3657EF}4-Jan-07 4:59
memberSDX-{5A853460-2944-42f8-84B8-2432DE3657EF}4-Jan-07 4:59 
QuestionAdd validation in Faculty field Pin
viashu23-May-06 20:47
memberviashu23-May-06 20:47 
GeneralNice idea! Pin
Jörgen Sigvardsson12-Feb-02 10:36
memberJörgen Sigvardsson12-Feb-02 10:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 12 Feb 2002
Article Copyright 2002 by Sven Axelsson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid