Search notes:

Excel Object Model: OLEObject / MSForms / TextBox

functions.bas

functions.bas is a VBA module that creates a Forms-text box:
option explicit

sub main() ' {

    with activeSheet ' {

         dim rng as range
         set rng = .range(.cells(2,2), .cells(12, 7))

         dim obj as oleObject
         set obj = activeSheet.OLEObjects.add( _
            classType       := "Forms.TextBox.1"  , _
            link            :=  false             , _
            displayAsIcon   :=  false             , _
            left            :=  rng.left          , _
            top             :=  rng.top           , _
            width           :=  rng.width         , _
            height          :=  rng.height          _
         )

    end with ' }

    dim tb as msForms.textBox
    set tb = obj.object

    with tb ' {

      .name              = "txt"
      .font              = "Courier"
      .enterKeyBehavior  =  true
      .multiLine         =  true
      .borderStyle       =  fmBorderStyleSingle
      .backColor         =  rgb(255, 240, 100)

    end with ' }

end sub ' }
Github repository about-MS-Office-object-model, path: /Excel/OLEObject/MSForms/textBox/functions.bas

sheet1.bas

sheet1.bas is the sheet-module that is needed to capture events of form controls placed on a given sheet.
In order to stop a key from being prcocessed, 0 must be assigned to keyCode that is passed to the _keyDown event.
option explicit

private sub txt_keyDown(byVal keyCode as msForms.returnInteger, byVal state as integer) ' {

    dim shift, alt, ctrl as string

    if state and 1 then shift = "shf" 
    if state and 2 then ctrl  = "ctl"
    if state and 4 then alt   = "alt"

    debug.print _
      shift    & chr(9) & _
      ctrl     & chr(9) & _
      alt      & chr(9) & _
      keyCode  & chr(9)

end sub ' }
Github repository about-MS-Office-object-model, path: /Excel/OLEObject/MSForms/textBox/sheet1.bas

Result

After being created, the text box looks like this:

create.wsf

The workbook with this two sheets can be created with the VB Script MS-Office App creator ant the following script:
<job>
<script language="VBScript" src="..\..\..\..\VBS-MS-Office-App-Creator\create-MS-Office-app.vbs" />
<script language="VBScript">

   option explicit

   dim app
   dim xls
   set xls = createOfficeApp("excel", currentDir() & "created.xlsm")
   set app = xls.application

'
'  Microsoft Forms 2.0 Object Library
'
   addReference app, "{0D452EE1-E08F-101A-852E-02608C4D0BB4}", 2, 0

   insertModule app, currentDir() & "functions.bas", "funcs"               , 1
   insertModule app, currentDir() & "sheet1.bas"   , xls.sheets(1).codeName, 1

   app.run "main"

   xls.save

   wscript.echo "The end."

</script> </job>
Github repository about-MS-Office-object-model, path: /Excel/OLEObject/MSForms/textBox/create.wsf

See also

The OLEObject object in Excel's object model
MS Forms 2.0 Object Library

Index