Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-06-2021, 11:26 PM
laith93 laith93 is offline Using custom style instead of inserting font attributes manually in VBA code Windows 10 Using custom style instead of inserting font attributes manually in VBA code Office 2019
Competent Performer
Using custom style instead of inserting font attributes manually in VBA code
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Post Using custom style instead of inserting font attributes manually in VBA code

Hi,
I have this code to change attributes of multiple words, which requires to type it manually each time, but I want to use custom style.


I edited the code by inserting the line which marked in red color, but doesn't make changes. Any solution please with thanks.
Code:
Sub SearchMultipleWords()
    Dim oDoc As Document
    Dim MyAr() As String, strToFind As String
    Dim i As Long
    strToFind = "Background, Methods, Results, Conclusion"
    MyAr = Split(strToFind, ",")
    For i = LBound(MyAr) To UBound(MyAr)
        With selection.Find
            .ClearFormatting
            .Text = MyAr(i)
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Execute

            '~~> Change the attributes
            Do Until .Found = False
            With Selection.Style = ActiveDocument.Styles("B12")
'                With Selection.Font
'                    .Name = "Times New Roman"
'                    .Size = 12
'                    .Bold = True
'                   ' .Color = RGB(200, 200, 0)
''                    .Italic = True
                End With
                Selection.Find.Execute
            Loop
        End With
    Next i
End Sub
Reply With Quote
  #2  
Old 11-07-2021, 12:34 AM
Guessed's Avatar
Guessed Guessed is offline Using custom style instead of inserting font attributes manually in VBA code Windows 10 Using custom style instead of inserting font attributes manually in VBA code Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It looks you were over-engineering this solution a bit
Code:
Sub SearchMultipleWords()
  Dim MyAr() As String, strToFind As String
  strToFind = "Background, Methods, Results, Conclusion"
  MyAr = Split(strToFind, ",")
  For i = LBound(MyAr) To UBound(MyAr)
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = MyAr(i)
      .Replacement.Style = "B12"
      .Forward = True
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
    End With
  Next i
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 11-07-2021, 12:52 AM
laith93 laith93 is offline Using custom style instead of inserting font attributes manually in VBA code Windows 10 Using custom style instead of inserting font attributes manually in VBA code Office 2019
Competent Performer
Using custom style instead of inserting font attributes manually in VBA code
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
It looks you were over-engineering this solution a bit
Thanks Mr. Andrew
Excuse me, that all that I know
We continue to learn
Best Regards
Reply With Quote
Reply

Tags
find & replace, vba code, word vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Duplicating Ranges and changing Font Attributes CharlieM Word VBA 3 08-10-2021 04:44 AM
Using custom style instead of inserting font attributes manually in VBA code Inserting page numbers manually in footer msnarayanan Word 5 10-30-2018 06:30 AM
Using custom style instead of inserting font attributes manually in VBA code VBA Code to search for field codes with certain text before the Field code and to change style welcometocandyland Word VBA 4 02-08-2017 06:53 PM
Using custom style instead of inserting font attributes manually in VBA code Inserting image into paragraph style gnelz Word 1 02-07-2017 01:12 PM
Using custom style instead of inserting font attributes manually in VBA code VBA code for inserting a future date cosmopolitan Word VBA 1 08-14-2013 01:58 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:24 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft