PDA

View Full Version : Changing font name, size, bold, etc...



dagerr
02-13-2018, 05:35 AM
Hi again,
I want change font name, size, bold, etc... Code is divided by 2 parts: 1 - changing fontname, 2 - changing size, bold, etc..,
They work ok separately, but when I connect them in one macro it working only first part. What I'm doing wrong?
Thanks in advanced.


Sub Makro_change_font_and_format()
'
'changing font
Dim zRng As Word.Range
Set zRng = ActiveDocument.Range
With zRng.Find
.Text = "Abc"
With .Replacement
.Font.Name = "Arial Narrow"
End With

.Execute Replace:=wdReplaceAll
End With
albl_Exit:
Exit Sub

'changing color, size, etc....
With Selection.Find.Replacement.Font
.Size = 9
.bold = True
.Color = 1403396

End With
With Selection.Find.Replacement.ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(0.9)
.Alignment = wdAlignParagraphRight
.LineUnitBefore = 0
.LineUnitAfter = 0
End With

With Selection.Find
.Text = "Abc"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True

End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

gmayor
02-13-2018, 06:41 AM
Use the following instead - but bear in mind that the Paragraph formatting applies to the whole paragraph that the found text resides in.


Sub Makro_change_font_and_format()
Dim zRng As Word.Range
Set zRng = ActiveDocument.Range
With zRng.Find
.Text = "Abc"
With .Replacement
.ClearFormatting
.Font.Name = "Arial Narrow"
.Font.Size = 9
.Font.Bold = True
.Font.Color = 1403396
With .ParagraphFormat 'the whole paragraph
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(0.9)
.Alignment = wdAlignParagraphRight
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
End With
.Execute Replace:=wdReplaceAll
End With
lbl_Exit:
Set zRng = Nothing
Exit Sub
End Sub

RaymondGalla
02-10-2021, 08:01 AM
I found this topic very timely, I was already thinking of creating a new topic with such a question.