Attribute VB_Name = "mStringInserts1" Option Explicit ' Brad Martinez, http://www.mvps.org/ccrp/ Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _ (ByVal dwFlags As FM_dwFlags, _ lpSource As Any, _ ByVal dwMessageId As Long, _ ByVal dwLanguageId As Long, _ ByVal lpBuffer As Any, _ ByVal nSize As Long, _ Arguments As Any) As Long Public Enum FM_dwFlags FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100 FORMAT_MESSAGE_IGNORE_INSERTS = &H200 FORMAT_MESSAGE_FROM_STRING = &H400 FORMAT_MESSAGE_FROM_HMODULE = &H800 FORMAT_MESSAGE_FROM_SYSTEM = &H1000 FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000 End Enum Public Const LANG_USER_DEFAULT = &H400& ' Sub main() Dim sInserts(1 To 3) As String sInserts(1) = "we've" sInserts(2) = "failure" sInserts(3) = "communicate" MsgBox DoStringInsert("What %1 got here is %2 to %3.", sInserts) End Sub ' Inserts into the sIn param the specified strings in the sInserts array param. ' If the number of inserts in sIn does not exactly match then number of ' elements in the sInserts array, a GPF will more than likely occur. ' If successful, returns sIn with each respective insert placed in it's ' specified location, returns an emty string otherwise. Public Function DoStringInsert(sIn As String, sInserts() As String) As String Dim iLow As Long Dim iHigh As Long Dim lpInserts() As Long ' array of insert pointers Dim sTmp() As String Dim i As Long Dim cbInserts As Long Dim sOut As String Dim cbOut As Long ' get the array's lower bound element, exit on error On Error Resume Next iLow = LBound(sInserts) If Err Then Exit Function On Error GoTo 0 ' get the array's upper bound element iHigh = UBound(sInserts) ' Re-allocate the array of string pointers, and an array ' to hold the Unicode strings for StrPtr to point to. ReDim lpInserts(iLow To iHigh) ReDim sTmp(iLow To iHigh) ' Copy each insert's string pointer value to the array, ' adding the length of each insert string as we go... For i = iLow To iHigh cbInserts = cbInserts + Len(sInserts(i)) sTmp(i) = StrConv(sInserts(i), vbFromUnicode) lpInserts(i) = StrPtr(sTmp(i)) Next ' Re-allocate the output string to the length of the input ' string and the sum total of the length of all insert strings. sOut = String$(Len(sIn) + cbInserts, 0) ' Do the inserts... cbOut = FormatMessage(FORMAT_MESSAGE_FROM_STRING Or _ FORMAT_MESSAGE_ARGUMENT_ARRAY, _ ByVal sIn, _ 0, _ LANG_USER_DEFAULT, _ sOut, _ Len(sIn) + cbInserts, _ lpInserts(iLow)) If cbOut Then DoStringInsert = Left$(sOut, cbOut) End Function