Indenting a piece of inserted text

O

Ogier

Hi out there!

With the macro below I wish to accomplish the following:

1) At the insertion point, insert a carriage return
2) Somehow mark the current position
3) Insert some formatted text
4) Select the text from the marked position to the end of the insertion
5) Indent the selected text at both margins
6) Insert a carriage return

In other words: Indent a piece of inserted text without affecting the layout
of previous or following text. Here is my macro:


Sub Makro1()
' Purpose: Write some formatted text and indent it from both left and
right
Dim i As Integer
Dim SStart As Integer
Dim SEnd As Integer
Dim Range As Object
Selection.Collapse wdCollapseStart
Selection.InsertParagraph
SStart = Selection.Start
' Insert some formatted text
Selection.Font.Bold = wdToggle
Selection.TypeText "Title in bold"
Selection.Font.Bold = wdToggle
Selection.TypeText vbCr
For i = 1 To 10
Selection.TypeText Text:="Here is some dummy text. "
Next i
SEnd = Selection.Start
' Set range
ActiveDocument.Range SStart, SEnd
' Indent text in range
With ActiveDocument.Range.ParagraphFormat
.LeftIndent = CentimetersToPoints(2)
.RightIndent = CentimetersToPoints(2)
.SpaceBefore = 6
.SpaceAfter = 6
End With
Selection.InsertParagraph
End Sub

The inserted text is indeed indented from both sides, but so is the
neighboring text! Somehow the selection is not done correctly.

Any suggestions would be helpful!

Best wishes
Holger Nielsen
 
C

Cindy M.

Hi Ogier,
With the macro below I wish to accomplish the following:

1) At the insertion point, insert a carriage return
2) Somehow mark the current position
3) Insert some formatted text
4) Select the text from the marked position to the end of the insertion
5) Indent the selected text at both margins
6) Insert a carriage return

In other words: Indent a piece of inserted text without affecting the layout
of previous or following text. Here is my macro:

Try something like the following. You'll note I'm working with the Range
object, rather than Selection. Generally, it's easier and more reliable to use
Range...

Sub InsertFormatIndentText()
Dim rng As Word.Range

Set rng = Selection.Range
rng.InsertParagraph
'Puts the range in the new paragraph
rng.Collapse wdCollapseEnd
'Insert text, plus new paragraph
rng.Text = "Here is some dummy text. " & vbCr
'Format the new paragraph
rng.Font.Bold = True
rng.Paragraphs.LeftIndent = 10
rng.Paragraphs.RightIndent = 10
End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
O

Ogier

Cindy M. said:
Try something like the following. You'll note I'm working with the Range
object, rather than Selection. Generally, it's easier and more reliable to use
Range...

Sub InsertFormatIndentText()
Dim rng As Word.Range

Set rng = Selection.Range
rng.InsertParagraph
'Puts the range in the new paragraph
rng.Collapse wdCollapseEnd
'Insert text, plus new paragraph
rng.Text = "Here is some dummy text. " & vbCr
'Format the new paragraph
rng.Font.Bold = True
rng.Paragraphs.LeftIndent = 10
rng.Paragraphs.RightIndent = 10
End Sub


Hi Cindy!

Thanks a lot for your range solution which indents as required.
However, with your solution *all* inserted text is set in bold.
I wish to be able to insert text with different attributes as to my liking.
Can this be done?

Best wishes
Holger
 
C

Cindy M.

Hi Ogier,
However, with your solution *all* inserted text is set in bold.
I wish to be able to insert text with different attributes as to my liking.
Can this be done?

Yes, it can :)

All you need to do is break up the text into "chunks" according to when the
formatting should differ. Here's a code example (untested)

Dim rng as Word.Range
Set rng = Selection.Range

rng.Text = "First bit. "
rng.Collapse wdCollapseEnd
rng.Text = "Some italic text. "
Rng.Font.Italic = True
rng.Collapse wdCollapseEnd
rng.Text = "The third bit. "
Rng.Font.Bold = True
rng.Collapse wdCollapseEnd
rng.Text = "The last bit." & vbCr
rng.Paragraphs.LeftIndent = 25

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
O

Ogier

Cindy M. said:
Yes, it can :)

Hi Cindy!

Thanks a lot for your suggestions, they were quite helpful, and I arrived at
the following sub to do the job:

Sub InsertIndentText()
Dim S As String
Dim rng As Word.Range
S = ""
For i = 1 To 8
S = S + "Text of this example, not bold nor italics. "
Next i
Set rng = Selection.Range
With rng
.Text = "Bold tilte line: "
.Font.Bold = True
.Collapse wdCollapseEnd
.Text = "Some italicised text in title line."
.Font.Bold = False
.Font.Italic = True
.Collapse wdCollapseEnd
.Text = vbCr & S & vbCr
.Font.Italic = False
.Paragraphs.LeftIndent = CentimetersToPoints(2)
.Paragraphs.RightIndent = CentimetersToPoints(2)
' .Font.Shrink
End With
End Sub


However, more wants more! So I tried also the reduce the font size of the
inserted text by adding .Font.Shrink as the last line (commented out above).
But it has only effect on the last part of the insertion (S string), the
title remains the same size.
I have tried to insert .Font.Shrink earlier on (dosn't help) and looked for
..Paragraph properties which might be useful, but found none.
Perhaps you can help me out here too?

Best wishes
Holger
 
C

Cindy M.

Hi Ogier,
So I tried also the reduce the font size of the
inserted text by adding .Font.Shrink as the last line (commented out above).
But it has only effect on the last part of the insertion (S string), the
title remains the same size.

Can you be more specific about what it is you want to shrink? Everything you're
adding?

In that case, you want to extend the result range back to the beginning point.
For your example, I'd do something like this:

rng.Start = Selection.Range.Start
rng.Font.Shrink

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
O

Ogier

Cindy M. said:
Hi Ogier,
Can you be more specific about what it is you want to shrink? Everything you're
adding?
Yes, it is.
In that case, you want to extend the result range back to the beginning point.
For your example, I'd do something like this:

rng.Start = Selection.Range.Start
rng.Font.Shrink

This woks! Cindy, you are the Meister :)
Thanks a lot!

Best wishes
Holger
 

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