Formatting textbox shape in word from excel

B

brad

Hi All,

I'm making an application in excel to set up a word document and add a
textbox to it. However, I'm having trouble setting the line and color
of the textbox. Does anyone know the specific code to do this as the
following code produces the following error:

Run-time error '438':
Object doesn't support this property or method

Public Const pts2mm = 2.834467

Sub ControlWord()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document

Set wrdApp = CreateObject("Word.Application")

wrdApp.Visible = True

Set wrdDoc = Documents.Add

Dim x As Object

'Add the textbox
Set x = wrdDoc.Shapes.AddTextbox(msoTextOrientationHorizontal, 40
* pts2mm, 90 * pts2mm, 20 * pts2mm, 6.6 * pts2mm)

With x
.Name = "Textbox 1"

'Add text and set font and size
With .TextFrame
.TextRange = "01/07/07"
.TextRange.Font.Name = "Arial"
.TextRange.Font.Size = "10"
End With

'Select textbox and set background and lines to nothing
.Select
With Selection.ShapeRange '<---- error occurs here
.Line.Visible = msoFalse
.Fill.Visible = msoFalse
End With

End With

'Clean up
Set x = Nothing

Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub

I've added the reference to word in the vbe so I don't know why it
doesn't work. I tried this code first in a module inside word and it
worked fine.

Can anyone assist?
 
J

Jay Freedman

Hi Brad,

I don't see how that code could have worked within Word. After the .Select,
the selection consists of the textbox itself, which doesn't contain a
ShapeRange -- that's why the error occurs. The paragraph in which the
textbox is anchored would have a ShapeRange that includes the textbox, but
that's not what you're doing.

If you properly declare the variable x as Word.Shape, then you don't need to
do the .Select at all -- just use the .Line and .Fill properties of the
object x to do the work:

Dim x As Word.Shape
....

With x
.Name = "Textbox 1"

'Add text and set font and size
With .TextFrame
.TextRange = "01/07/07"
.TextRange.Font.Name = "Arial"
.TextRange.Font.Size = "10"
End With

.Line.Visible = msoFalse
.Fill.Visible = msoFalse

End With

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

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