Clear textbox of a shape in ns word

J

Jay Freedman

Javier said:
How can I clear textboxes of a shape?

Regards

Assuming you have a Shape object in your code that has been set to the
specific shape in the document, such as

Dim oShp As Shape
Set oShp = ActiveDocument.Shapes(1)

Then you can clear that shape's text with

oShp.TextFrame.TextRange.Delete

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Javier \(CC\)

Hi. That doesn't works.
I puted in a shape a group of textboxes
I saw in the web something like this for clear them.

Dim x As Shape
For Each x In ActiveDocument.Shapes
If x.Type = msoTextBox Then
x.TextFrame.TextRange.Text = " "
End If
Next

But doesn't works too. Only apears a shape object but not any textbox.

Any idea?
Regards
 
J

Jay Freedman

I didn't understand correctly before -- I thought you were talking about the
text that can be added directly to a shape by right-clicking it and
selecting "Add Text". That kind of text is addressed by
oShp.TextFrame.TextRange.

But you drew actual text boxes inside the shape, and I don't think there's
any way to address them in VBA. I can write the expression

oShp.TextFrame.TextRange.ShapeRange

but when I run the code, I get an error saying that the .ShapeRange is not
defined.
 
T

Tony Jollans

I'm still not sure I understand. I don't think shapes can contain textboxes.
All that you get is overlaid shapes. Some clever code that checked actual
positions via .Top, .Left, .etc and .ZOrder might be able to determine the
overlaps but I can't imagine it would be worth the effort.
 
T

Tony Jollans

Now I understand - the Shape has got nothing to do with it. And clearing
means clearing out, not clearing away.

The code behind the button doesn't clear the textboxes. It explicitly clears
the two comboboxes. I would make it also explicitly clear the three
textboxes. You can make it more flexible if you like and come back and ask
if you need to, but for now ...

Private Sub CBLimpiar_Click()
Me.ComboRamo.Value = Me.ComboRamo.List(0)
Me.ComboSRecibo.Value = Me.ComboSRecibo.List(0)
Me.TextBoxAgenciaCod.Value = ""
Me.TextBoxNpoliza.Value = ""
Me.TextBoxVencimiento.Value = ""
End Sub

(and delete routine LimpiaTextBox)
 
J

Javier \(CC\)

Ok, that was an example for 3 textboxes, but what is going to happen when
the template has a lot of textboxes?. The final project is going to have
around 14 templates with a lot of textboxes, is the reason for I want to
develop a more practical code like
Dim x As Shape
For Each x In ActiveDocument.Shapes
If x.Type = msoTextBox Then
x.TextFrame.TextRange.Text = " "
End If
Next

but that works :). Any idea about?
 
T

Tony Jollans

My apologies; I was wrong on my first assessment. This is a little complex.

You have a Drawing Canvas containing three textboxes and the second of these
contains your Controls. skipping through the controls you can identify - and
clear - the textboxes

Dim s As Shape, cs As Shape, ish As InlineShape, ol As Object
For Each s In ActiveDocument.Shapes
If s.Type = msoCanvas Then
For Each cs In s.CanvasItems
If cs.Type = msoTextBox Then ' Textbox on canvas
For Each ish In cs.TextFrame.TextRange.InlineShapes
If ish.Type = wdInlineShapeOLEControlObject Then '
ActiveX
Set ol = ish.OLEFormat.Object
If TypeOf ol Is MSForms.TextBox Then
ol.Text = ""
End If
End If
Next
End If
Next
End If
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top