'.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
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...
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?
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...
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?
>
>.
>
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...
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
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...
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
---------------------------------------------------------
-----------------------------------------------------
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!
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...
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