Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Text Frame at Cursor Position

Status
Not open for further replies.

hypoid333

Technical User
Mar 17, 2008
6
GB
Below is the code I use to insert a text frame in a Word document. In this example, the frame is positioned 60 points from Left and 40 points from Top. Instead of using Left and Top positioning, is it possible with VBA code to insert a text frame (or text box), where the cursor is currently positioned in the document?

Your help is much appreciated.

Sub InsertTextFrame()
ActiveDocument.Shapes.AddShape(1, 60, 40, 50, 20) _
.TextFrame.TextRange.Text = "xyz"
End Sub
 




Hi,

Try using InlineShapes rather than Shapes.

Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
That may, or may not work for you depending on what your requirements are.

Your code creates a TextFrame Anchored to the first paragraph on the page. This is very different from trying to insert a Shape (i.e it floats) at the cursor.

faq219-2884

Gerry
My paintings and sculpture
 
It looks as if only pictures can be inserted inline with the text, and is not possible to do this with a text frame or text box. Oh well, many thanks SkipVought and Fumei for your help. I appreciate your time taken on this.

Cheers
 
Not correct. As I stated, it depends on your requirements....which you have not stated, nor responded to.

I can insert a InlineShape textbox at the cursor, even if it is in the middle of a sentence.

No, it is not possible with a textframe...and for a very very simple reason. A TextFrame is not an InlineShape.

faq219-2884

Gerry
My paintings and sculpture
 
Hi Guys
I have found it possible to insert an inline text box by adding an OLE control:

Selection.InlineShapes.AddOLEControl ClassType:="Forms.TextBox.1"

Is there a bit of VBA code that would change the SpecialEffect property to fmSpecialEffectFlat ?
 





Hi,

have you checked Word VBA HELP on SpecialEffect Property?


Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
1. You declare an InlineShape object
2. You Set it as the added InlineShape
3. change the properties of the object

Regarding your specifc request:
Code:
Dim oInlineShape As InlineShape

Set oInlineShape = Selection.InlineShapes _
   .AddOLEControl(ClassType:="Forms.TextBox.1")
' 0 = Flat
oInlineShape.OLEFormat.Object.SpecialEffect = 0

Unfortunately, you will NOT find this in Help. Help only covers using SpecialEffect on controls objects on a userform. There is no mention of requiring .OLEFormat.Object. on controls in the document. Also unfortunately, while IntelliSense can get you as far as .OLEFormat.Object, it does NOT return anything with the dot - .OLEFormat.Object.

So unless you know what to put after there, you....ummmm...don't know. Essentially, .OLEFormat.Object. allows access to all the properties (that IntelliSense does show) a control object has on a userform.

faq219-2884

Gerry
My paintings and sculpture
 
But still look at Help anyway, because if you are going to use it for other types of controls, there is useful information there.

Skip, there is, in fact, NO explicit mention of textbox in Help on SpecialEffect. Other than "other than a CheckBox, OptionButton, or ToggleButton."

faq219-2884

Gerry
My paintings and sculpture
 
Fumei this is superb, it’s exactly what I want. Many thanks to you for this useful solution.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top