Home > Net >  Justify All Text
Justify All Text

Time:08-12

I'm new here so thank you in advance for your patience. Also, I'm not a native English speaker so some things might get lost in translation.

I found this wonderful vba macro to "Justify all text is Microsoft Word" [from Alvin567] and you all 1 and it works just as planned.

I would like to adapt it so that it doesn't justify paragraphs that has Shift Enter (linebreak I think) in my document. I can't seem to find how to refer to that specific character, since it's different than "Chr(13)".

I'm usually good at adapting codes from the recording tool or find online what I'm looking for even though I never learned it through any courses, but with this one, I can't seem to figure it out on my own.

Any help would be greatly appreciated.

So here is the code :

Sub JustifyAllTheText(control As IRibbonControl) 'Don't forget to link it with RibbonX
    On Error Resume Next
    Dim para   As Paragraph
    Dim searchRange As Range
    Set searchRange = Selection.Range
    searchRange.End = ActiveDocument.Content.End
For Each para In searchRange.Paragraphs
    If para.Range.Font.Size = 10 Then
    'If para.Range.Font.ColorIndex = wdBlack Then 'I don't need it but kept it just in case
    If Not para.Range.InlineShapes.Count > 0 Then
    'If Not para.Range.IsEndOfRowMark = True Then 'Added line to test linebreak but doesn't work to made into text
    If Not para.Range = vbLf Then
    If Not para.Range.Information(wdWithInTable) Then
    para.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
    End If
    End If
    End If
    End If
Next para

End Sub

Thanks!

CodePudding user response:

You cannot justify paragraphs containing manual line breaks via any of the justification options. Instead, you need to modify a compatibility setting, thus:

Sub Demo()
Application.ScreenUpdating = False
ActiveDocument.Compatibility(wdExpandShiftReturn) = True
Application.ScreenUpdating = True
End Sub

Moreover, you should not force the justification of paragraphs via code like:

para.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify

That is liable to lead to document bloat and, potentially, corruption. Rather, you should modify the underlying paragraph Style - which also means making the change once for the whole document.

CodePudding user response:

You can check for If InStr(para.Range.Text, vbVerticalTab) = 0 Then

vbVerticalTab is equal to chr(11) which is in Word the "character" for Shift Enter

  • Related