Trying to add a textbox with VBA, but getting a Smiley Face instead

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,200
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Sorry, the first attempt to post this appeared to have failed so this may be posted twice.
Excel 2007, Vista.
When I step through the code and hover over "Type:= msoTextBox" the tip pointer indicates a value of 17 which is consistent with the mso shape type enum for a TextBox, but Excel produces a smiley face instead. Am I missing something?
Code:
Sub TBoxAdd()
Dim sh As Shape
'next line returns a smiley face
Set sh = ActiveSheet.Shapes.AddShape(Type:=msoTextBox, Left:=100, Top:=100, Width:=100, Height:=100)
MsgBox sh.Name  'Returns "Smiley Face #"
End Sub
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
No response, so I did some homework on this. I'll pass my findings on for those, like me, who are still learning. The syntax I used to add a shape apparently applies to 138 (Excel 2007) 'basic shapes', but does not include a text box.

To add a text box I found this statement to work:
Code:
Dim sh as shape
Set sh = ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:= 100, Top:=10, width:= 100, Height:= 50)
Using the statement in my original post generated a Smiley Face because msoTextBox has a value of 17 and for the 'basic shapes' number 17 is indeed a Smiley Face.

The macro below will list (draw) the 138 'basic shapes' and place the type number value for each shape within the shape. Might serve as a handy reference because with Excel 2007 the macro recorder is mostly out to lunch when it comes to trying to record with shapes.
Code:
Sub ListShapes()
'Lists (draws) shapes of Type i from i = 1 to 137 (apparently the limit)
'down the activesheet. Note textbox requires a different
'syntax to generate and is not included here.
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
    sh.Delete
Next sh
For i = 1 To 137
Set sh = ActiveSheet.Shapes.AddShape(Type:=i, Left:=100, _
    Top:=80 * i, Width:=50, Height:=50)
ActiveSheet.Shapes(sh.Name).OLEFormat.Object.Text = CStr(i)
Cells(sh.TopLeftCell.Row, sh.TopLeftCell.Column).Offset(0, 2).Value = sh.Name
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,517
Members
448,968
Latest member
Ajax40

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top