Answered by:
Macro for Paragraph needs to be modified

Question
-
I have a macro to format a paragraph , which i have made through a button macro. I need to highlight the paragraph then only the macro runs effectively. I want the same macro maualy through the View -> create new Macro Mode in VB script. I need to remove the disadvantage of highlighting the paragrapgh
The Paragraph should also align vertically itself from the Header if any at 1.5 spacing.
Macro code given below - The Macro does the following things
Sub Para_1()
'
' Para_1 Macro
'
'
With Selection.Font
.Name = "Arial"
.Size = 9
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = -587137025
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.Ligatures = wdLigaturesNone
.NumberSpacing = wdNumberSpacingDefault
.NumberForm = wdNumberFormDefault
.StylisticSet = wdStylisticSetDefault
.ContextualAlternates = 0
End With
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceAtLeast
.LineSpacing = 12
.Alignment = wdAlignParagraphJustify
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0.5)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
End Sub
ALI
- Edited by ALI74 Tuesday, December 18, 2012 8:08 AM editied
Tuesday, December 18, 2012 7:25 AM
Answers
-
Modifying the document's font and paragraph layout this way is the wrong way to go about it. You should modify the Styles.
Sitll, if you want to do it your way, try:
Sub Demo()
With ActiveDocument.Range
With .Font
.Name = "Arial"
.Size = 9
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = -587137025
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.Ligatures = wdLigaturesNone
.NumberSpacing = wdNumberSpacingDefault
.NumberForm = wdNumberFormDefault
.StylisticSet = wdStylisticSetDefault
.ContextualAlternates = 0
End With
With .ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceAtLeast
.LineSpacing = 12
.Alignment = wdAlignParagraphJustify
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0.5)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
End With
End Sub
Cheers
Paul Edstein
[MS MVP - Word]- Edited by macropodMVP Wednesday, December 19, 2012 11:22 AM
- Marked as answer by 许阳(无锡) Tuesday, December 25, 2012 2:57 AM
Wednesday, December 19, 2012 11:20 AM
All replies
-
Hi Ali
Here's how to get the entire paragraph for the current selection:
Sub Para_1()
Dim rngPara as Word.Paragraph
'Gets the paragraph for the current cursor
Set rngPara = Selection.Paragraphs(1).Range
With rngPara.Font
.Name = "Arial"
'usw....
End With
With rngPara.ParagraphFormat
'usw...
End With
End SubCindy Meister, VSTO/Word MVP, my blog
Tuesday, December 18, 2012 9:09 AM -
Code is giving error after modification . Kindly help
Sub Para_1()
Dim rngPara As Word.Paragraph
'Gets the paragraph for the current cursor
Set rngPara = Selection.Paragraphs(1).Range
With rngPara.Font
.Name = "Arial"
.Size = 9
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = -587137025
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.Ligatures = wdLigaturesNone
.NumberSpacing = wdNumberSpacingDefault
.NumberForm = wdNumberFormDefault
.StylisticSet = wdStylisticSetDefault
.ContextualAlternates = 0
End With
With rngPara.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceAtLeast
.LineSpacing = 12
.Alignment = wdAlignParagraphJustify
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0.5)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
End SubALI
Tuesday, December 18, 2012 11:50 AM -
Hi Ali
Sorry, my brain was working on various things. Change
Dim rngPara as Word.Paragraph
To
Dim rngPara as Word.Range
Note: when you're getting an error, please always quote the error text in your message.
Cindy Meister, VSTO/Word MVP, my blog
Tuesday, December 18, 2012 11:56 AM -
Hi
the code is working only on the first para. the remaining paragraph in the documents remain untouched, there are no changes in the format of the other para....
Kindly guide....
ALI
Tuesday, December 18, 2012 12:15 PM -
No one can read your mind!
Change: Set rngPara = Selection.Paragraphs(1).Range
To: For Each rngPara in ActiveDocument.Paragraphs
'The existing code
Next rngPara
Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
- Edited by Greg Maxey Tuesday, December 18, 2012 1:17 PM
Tuesday, December 18, 2012 12:54 PM -
After changing the code as shown below . it si giving error
Giving compile error
For without next
Sub Para_1()
Dim rngPara As Word.Range
'Gets the paragraph for the current cursor
For Each rngPara In ActiveDocument.Paragraphs
With rngPara.FontALI
Tuesday, December 18, 2012 1:14 PM -
If you get and error For without next, the natural thing to do would be to look for where a For Starts and where a Next should be. Have you done that?
Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
Tuesday, December 18, 2012 1:18 PM -
I am not a coder.. Had done it long time ago. Kindly modify the code.
ALI
Tuesday, December 18, 2012 1:41 PM -
No one can modify your code if you don't show it to us in its new form.
Furthermore, no one here is your servant or obligated in any way to provide you with code. As I mentioned before, the MSDN forum is here to help DEVELOPERS (programmers - people who write code) learn to deal with the Word programming interface, not to provide a free service. You need to put in some effort at understanding what you're doing, or pay someone to do the job for you.
Otherwise, I recommend you try the Communities site, which targets enthusiasts, rather than professionals:
http://answers.microsoft.com/en-us/office/forum/customize?tm=1355845187564Cindy Meister, VSTO/Word MVP, my blog
Tuesday, December 18, 2012 3:42 PM -
Hi,
I think you are have misunderstood me ....... I am just asking for a help. I HAVE NEVER TOLD IN THE FORUM THAT YOU EXPERTS ARE MY SERVANT !!!!!!...
I am here after 10 years trying to work on the macros .. I have created around 6 to 7 macros on my own . The Macros here will help me work on a document which is having 600 PAGES. There are three such more documents which needs to be worked on a short time frame
Same is the reason for the other thread " Macro for formating Heading & paragraph" where i am asking for changes in the code. I cannot manualy keep on changing around 600 headers & the corresponding para text
Below is the code in the new form of this FORM. Kindly help or ignore.
Sub Para_1()
Dim rngPara As Word.Range
'Gets the paragraph for the current cursor
Set rngPara = Selection.Paragraphs(1).Range
With rngPara.Font
.Name = "Arial"
.Size = 9
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = -587137025
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.Ligatures = wdLigaturesNone
.NumberSpacing = wdNumberSpacingDefault
.NumberForm = wdNumberFormDefault
.StylisticSet = wdStylisticSetDefault
.ContextualAlternates = 0
End With
With rngPara.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceAtLeast
.LineSpacing = 12
.Alignment = wdAlignParagraphJustify
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0.5)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
End SubALI
Tuesday, December 18, 2012 6:24 PM -
Modifying the document's font and paragraph layout this way is the wrong way to go about it. You should modify the Styles.
Sitll, if you want to do it your way, try:
Sub Demo()
With ActiveDocument.Range
With .Font
.Name = "Arial"
.Size = 9
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = -587137025
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.Ligatures = wdLigaturesNone
.NumberSpacing = wdNumberSpacingDefault
.NumberForm = wdNumberFormDefault
.StylisticSet = wdStylisticSetDefault
.ContextualAlternates = 0
End With
With .ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceAtLeast
.LineSpacing = 12
.Alignment = wdAlignParagraphJustify
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0.5)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
End With
End Sub
Cheers
Paul Edstein
[MS MVP - Word]- Edited by macropodMVP Wednesday, December 19, 2012 11:22 AM
- Marked as answer by 许阳(无锡) Tuesday, December 25, 2012 2:57 AM
Wednesday, December 19, 2012 11:20 AM