Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MS word textbox and VB

1,078 views
Skip to first unread message

Michael K

unread,
Sep 8, 2002, 12:03:21 PM9/8/02
to
Hi everyone ...
I am VERY frustrated in what should be a simple task!!!
I have spent 2 days trying to figure this out.
I am a VB programmer making my first attempt to
programming to the MSWord 9.0 object library and the
Office9.0 object library.
What i need to do is insert text into a MS Word textbox
I can create the Word Textbox(with code), but it is always
empty!
Here is the code I am using. it is basically copied from a
Macro.
code ......
With objWord.ActiveDocument
..Shapes.AddTextbox(msoTextOrientationHorizontal, 79.2, _
484.2, 288#, 117#).Select

'.Shapes(1).Select ' this was added from another suggestion
' but it does not work

Selection.ShapeRange.TextFrame.TextRange.Select
' Selection.Collapse
Selection.ParagraphFormat.Alignment =
wdAlignParagraphCenter
Selection.TypeText Text:="COSTS"
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "test line 1" & vbTab & vbTab & vbTab
& "Tabbed text"
Selection.Collapse wdCollapseEnd
Selection.InsertAfter (CostPremium1)
End With

The line Selection.InsertAfter(CostPremium1) is data from
a standard .BAS module within the VB program. I will need
to insert several of these.
the length of the document can vary quite a bit, due to
variables within the Vb user form and choices made. That
is why I was thinking the best way to do this a Word
TextBox. Once I can get data in it, I can make decisions
on where to place it by a SelectCase structure.
If anyone could provide an example,with both
Selection.TypeText Text: = and using info from a
module, I will be eternally grateful.
Thanks in advance,
Michael

Astrid

unread,
Sep 9, 2002, 5:55:26 PM9/9/02
to
Hi Michael,

Here's an example. Instead of using the selection object I used a Word range. Think of it as a selection that doesn't need to be selected ;-)
In most cases working with the range object is faster and it shows more nice since you don't see your cursor running up and down the screen.

--------------------------------------------------------------------------
Sub Textbox()
Dim CostPremium1 As String
Dim oShape As Word.Shape
Dim oRange As Word.Range

CostPremium1 = "c:\module1.bas"
Set oShape = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, _
79.2, 484.2, 288#, 117#)
Set oRange = oShape.TextFrame.TextRange
With oRange
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Text = "COSTS" & vbCr
.Collapse direction:=wdCollapseEnd
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Text = "test line 1" & vbTab & vbTab & vbTab & "Tabbed text"
.Collapse direction:=wdCollapseEnd
.InsertFile FileName:=CostPremium1
End With

Set oRange = Nothing
Set oShape = Nothing

End Sub
--------------------------------------------------------------------------


Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Michael K" <Ideas....@GTE.net> schreef in bericht news:7e0501c25751$410158d0$9be62ecf@tkmsftngxa03...

Michael Miller

unread,
Sep 10, 2002, 5:50:05 AM9/10/02
to
Hi, Astrid, Michael,

I am also having problems with a Textbox macro.
I want to insert a text box at the current insertion point
in the document, but I do not know how to set this
property.

Every time I click my new macro, the textbox is inserted
at the point I originally recorded the macro.

How do I make the text box insert where my cursor is?

Astrid

unread,
Sep 10, 2002, 6:36:09 AM9/10/02
to
Hi Michael,

I assumed you want your textbox positioned relative to the cursorposition (paragraph) and not fixed on the page?Try some code like this:

-------------------------------------------------------------------------
Sub AddTextBox()
Dim oRange As Range
Dim oShape As Shape

Set oRange = Selection.Range
Set oShape = ActiveDocument.Shapes.AddTextBox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=100, Height:=100, _
Anchor:=oRange)
With oShape
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.Left = 0
.Top = 0
End With

Set oShape = Nothing
Set oRange = Nothing

End Sub
-------------------------------------------------------------------------

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Michael Miller" <michaelm...@msn.com> schreef in bericht news:d13401c258af$70c9f540$36ef2ecf@tkmsftngxa12...

Michael K

unread,
Sep 10, 2002, 10:10:16 AM9/10/02
to
Astrid .. Thanks for the code. I will copy it and compare
it to this one that I got working yesterday afternoon. The
majority of this program is written to the word.range
object. it is much cleaner and quicker.
Code .....
With objWord.ActiveDocument

Dim objShape As Word.Shape

'Add the textbox
Set objShape = ActiveDocument.Shapes.AddTextbox
(msoTextOrientationHorizontal, 100.8, _
72#, 309.6, 129.6)

'Make sure it is visible
objShape.Visible = True

'Change left, top,
objShape.Left = 200

'Add text to the textbox
objShape.TextFrame.TextRange.Text = "Is this
working??!! "
objShape.TextFrame.TextRange.InsertParagraphAfter
objShape.TextFrame.TextRange.InsertAfter (CostPremium1)
'destroy objects
Set objShape = Nothing
End With

Michael K.


>-----Original Message-----
>Hi Michael,
>
>I assumed you want your textbox positioned relative to
the cursorposition (paragraph) and not fixed on the page?
Try some code like this:
>
>----------------------------------------------------------
---------------

>With objWord.ActiveDocument

Dim objShape As Word.Shape

'Add the textbox
Set objShape = ActiveDocument.Shapes.AddTextbox
(msoTextOrientationHorizontal, 100.8, _
72#, 309.6, 129.6)

'Make sure it is visible
objShape.Visible = True

'Change left, top,
objShape.Left = 200

'Add text to the textbox
objShape.TextFrame.TextRange.Text = "Is this
working??!! "
objShape.TextFrame.TextRange.InsertParagraphAfter
objShape.TextFrame.TextRange.InsertAfter (CostPremium1)
'destroy objects
Set objShape = Nothing
End With>----------------------------------------------


---------------------------
>
>Hope this helps,
>regards,
>Astrid
>
>So that all can benefit from the discussion, please post
all follow-ups to the newsgroup.
>Visit the MVP Word FAQ site at http://www.mvps.org/word/
>
>"Michael Miller" <michaelm...@msn.com> schreef in
bericht news:d13401c258af$70c9f540$36ef2ecf@tkmsftngxa12...
>> Hi, Astrid, Michael,
>>
>> I am also having problems with a Textbox macro.
>> I want to insert a text box at the current insertion
point
>> in the document, but I do not know how to set this
>> property.
>>
>> Every time I click my new macro, the textbox is
inserted
>> at the point I originally recorded the macro.
>>
>> How do I make the text box insert where my cursor is?
>

>.
>

Astrid

unread,
Sep 10, 2002, 7:37:37 PM9/10/02
to
Hi Michael,

Let us know if it's working. To write code and the text of your .bas file to the textbox, use something like:

--------------------------------------------------------------------
Sub AddTextBox()
Dim oRange As Range
Dim oShape As Shape

Dim sModuleName As String

sModuleName = "c:\mymodule.bas"


Set oRange = Selection.Range
Set oShape = ActiveDocument.Shapes.AddTextBox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=100, Height:=100, _
Anchor:=oRange)
With oShape

With .TextFrame.TextRange
.Text = "Hi there"
.InsertParagraphAfter
.Collapse direction:=wdCollapseEnd
.InsertFile FileName:=sModuleName, ConfirmConversions:=False
End With


.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.Left = 0
.Top = 0
End With

Set oShape = Nothing
Set oRange = Nothing

End Sub
--------------------------------------------------------------------


Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Michael K" <Ideas....@GTE.net> schreef in bericht news:f1ec01c258d3$c9efe0c0$3bef2ecf@TKMSFTNGXA10...

Michael Miller

unread,
Sep 11, 2002, 3:35:36 AM9/11/02
to
Astrid,

Thank you for your response. This code gets me closer than
I have been, but it inserts the textbox at the left margin
of the page on the current line, regardless of cursor
position or paragraph formatting (left, center, or right
justified). Can you invent something that will let me
insert a textbox right at the current cursor position?
Right at the insertion point?

Mike

----- Original Message -----
From: Astrid
Sent: Tuesday, September 10, 2002 3:37 AM
To: Michael Miller
Subject: Re: MS word textbox and VB

Hi Michael,

I assumed you want your textbox positioned relative to the
cursorposition (paragraph) and not fixed on the page?Try
some code like this:

-----------------------------------------------------------


Sub AddTextBox()
Dim oRange As Range
Dim oShape As Shape

Set oRange = Selection.Range
Set oShape = ActiveDocument.Shapes.AddTextBox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=100, Height:=100, _
Anchor:=oRange)
With oShape
.RelativeVerticalPosition =
wdRelativeVerticalPositionParagraph
.RelativeHorizontalPosition =
wdRelativeHorizontalPositionColumn
.Left = 0
.Top = 0
End With

Set oShape = Nothing
Set oRange = Nothing

End Sub
-----------------------------------------------------------

Hope this helps,
regards,
Astrid

Astrid

unread,
Sep 11, 2002, 5:06:09 AM9/11/02
to
Hi Michael,

The horizontal position of the textbox is set with the .left property. You can find out the position of the selectionpoint with the .Information property of the Selection object.
Note that there are a few considerations, your selection doesn't have to be visible on the screen, then the information property returns a -1 value. You need to compensate for that etc.
For more info see http://www.mvps.org/word/FAQs/MacrosVBA/GetPosRelToPage.htm

lPos = Selection.Information(wdHorizontalPositionRelativeToTextBoundary)
'Other code here
oShape.left = lPos

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.


Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Michael Miller" <michaelm...@msn.com> schreef in bericht news:ddd401c25965$d193f9a0$36ef2ecf@tkmsftngxa12...

Michael Miller

unread,
Sep 11, 2002, 6:03:44 AM9/11/02
to
Astrid.

Thanks for the code sample. I pared it down to a single
line for what I wanted, which was to add and format a
textbox where I was typing. I came across
the ...Information property while I was looking for a way
to get the textbox positioned at the cursor. Until I learn
more about that, this code does the job satisfactorily,
inserting the text box at the left margin on the current
line. Thanks for your help.

---------------------------------------------------------

Sub TransparentTextBox()
'
' Transparent TextBox Macro
' Inserts a Text Box with no fill at the left margin


' of the page on the current line

'
ActiveDocument.Shapes.AddTextBox _
(msoTextOrientationHorizontal, 0, 0, 100, 100, _
Anchor:=Selection.Range).Select
Selection.ShapeRange.Fill.Visible = msoFalse

End Sub

---------------------------------------------------------

Sub TransparentNoLineTextBox()
'
' Transparent No-Line TextBox Macro
' Inserts a Text Box with no fill and no line at the


' left margin of the page on the current line

'
ActiveDocument.Shapes.AddTextBox _
(msoTextOrientationHorizontal, 0, 0, 100, 100, _
Anchor:=Selection.Range).Select
Selection.ShapeRange.Fill.Visible = msoFalse
Selection.ShapeRange.Line.Visible = msoFalse
End Sub

---------------------------------------------------------

Michael Miller

unread,
Sep 11, 2002, 7:09:03 AM9/11/02
to
Here is the solution to putting a textbox on the page at
the point of insertion, or where the cursor is currently
located on the page. Please note that this method will
work properly only in "Print" view. It works in "Web" view
also, but the positioning will be a little off, and when
used in "Normal" view, it will automatically switch you
back to "Print" view, and produce unexpected results.
Thanks to Astrid and the MVP Word FAQ site for the
guidance:

-----------------------------------------------------

Sub TextBox()
'
' TextBox Macro
' Inserts a Text Box of the specified size at the
' current cursor location
'
x = Selection.Information _
(wdHorizontalPositionRelativeToPage)

y = Selection.Information _
(wdVerticalPositionRelativeToPage)

ActiveDocument.Shapes.AddTextBox _
(msoTextOrientationHorizontal, x, y, 100, 100).Select

End Sub

-----------------------------------------------------

Using additional code, this text box can be automatically
formatted any way you wish. If you don't know the specific
formatting code, use the macro recorder and the Insert
Textbox menu item to place a textbox on the screen, then
format it however you want. Then open the VB editor and
examine the code for your new macro. You should be able to
recognize the formatting code, then simply copy and paste
it into this macro. Also remember that if you create a
toolbar button for this macro, the ToolTip displayed when
you point at it will be the name of the macro subprocedure
itself. For example:

Sub InsertTextboxHere()

Will produce a Toolbar ToolTip of:

Insert Textbox Here

You can see the relationship. Enjoy, all, and thanks again!

Astrid

unread,
Sep 11, 2002, 7:52:46 AM9/11/02
to
Hi Michael,

Glad to see you got it to work!

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Michael Miller" <michaelm...@msn.com> schreef in bericht news:14b3401c25983$a32da2a0$39ef2ecf@TKMSFTNGXA08...

Rob Kuijpers

unread,
Oct 9, 2002, 1:52:45 AM10/9/02
to
I have a similar problem, only I don't wan't to create a textbox in
Word, but only select it and then work on the fields in the textbox.
You have to know the number of the shape. This is to be done by trial
and error. Is there a smoother way, with less chance of error when
started on another machine?

The code I use:
ActiveDocument.Shapes("Text Box 22").Select
With ActiveDocument.Shapes("Text Box 22").TextFrame
If .HasText = True Then
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
GoTo bmkplace
ElseIf .HasText = False Then
GoTo bmkplace
End If
End With

TIA,
Rob

0 new messages