syntactical help

W

Workgroups

I'm trying to use the following method to insert a textbox into a document
and format it as I go, but I'm up against some sort of vba
compiler/syntactical problem I don't understand yet:

With ActiveDocument.Shapes.AddTextBox([orientation, position size etc])
...[various textbox class members here, e.g....]
.TextAlign = fmTextAlignCenter
.BorderStyle = fmBorderStyleNone
[etc]
End Width

But Option Explicit doesn't like that, presumeably becuase 'AddTextBox' is
returning a Shape, not a Textbox.

My first instict is to want to do something like...

With Ctype(ActiveDocument.Shapes.AddTextBox(...), TextBox)
...[compiler-friendly textbox member references]
End With

But that's vb.net, not vba. How do I get vba to let me play with what
..AddTextBox is returning as if it were a textbox while keeping Option
Explicit happy? I don't want to cheat and turn it off. Is there a
Ctype-like keyword, a different add-a-textbox method I'm not aware of?
 
J

John

I think a shape object has a TextFrame object, so I guess you need to
reference the shape to add and size and then the TextFrame object to deal
with the text itself. Maybe something like:

Dim shp As Shape

shp = ActiveDocument.Shapes.AddTextBox........

With shp.TextFrame etc...........

Actually, also looking at your .TextAlign property, I think this is a form
control textbox property not a normal document textbox (which is a shape
object). So it looks like you're mixing up your object model. If this is
the case, just go with the above code.

Anyway I hope this helps.

John
 
W

Workgroups

Yep I think you're right, I'm confusing my textboxes; the 'fm' prefix on
those enumerated values, 'fmTextAlignCenter' and 'fmBorderStyleNone',
probably should have clued me in (I'd been staring at it too long
yesterday). I think I've been trying to fit a "Shape peg" in a "Form
Control hole", so to speak.

Thanks for your help, I'll try my luck with the TextFrame object.

John said:
I think a shape object has a TextFrame object, so I guess you need to
reference the shape to add and size and then the TextFrame object to deal
with the text itself. Maybe something like:

Dim shp As Shape

shp = ActiveDocument.Shapes.AddTextBox........

With shp.TextFrame etc...........

Actually, also looking at your .TextAlign property, I think this is a form
control textbox property not a normal document textbox (which is a shape
object). So it looks like you're mixing up your object model. If this is
the case, just go with the above code.

Anyway I hope this helps.

John

Workgroups said:
I'm trying to use the following method to insert a textbox into a
document and format it as I go, but I'm up against some sort of vba
compiler/syntactical problem I don't understand yet:

With ActiveDocument.Shapes.AddTextBox([orientation, position size etc])
...[various textbox class members here, e.g....]
.TextAlign = fmTextAlignCenter
.BorderStyle = fmBorderStyleNone
[etc]
End Width

But Option Explicit doesn't like that, presumeably becuase 'AddTextBox'
is returning a Shape, not a Textbox.

My first instict is to want to do something like...

With Ctype(ActiveDocument.Shapes.AddTextBox(...), TextBox)
...[compiler-friendly textbox member references]
End With

But that's vb.net, not vba. How do I get vba to let me play with what
.AddTextBox is returning as if it were a textbox while keeping Option
Explicit happy? I don't want to cheat and turn it off. Is there a
Ctype-like keyword, a different add-a-textbox method I'm not aware of?
 

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