Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 11-16-2018, 06:08 PM
schausberger2019 schausberger2019 is offline Windows 7 64bit Office 2010
Novice
 
Join Date: Nov 2018
Posts: 3
schausberger2019 is on a distinguished road
Default VBA Different replacement for each word in the string

Hello Everybody.
I have a code able to highlight all the words in the string keywords = "if,then,from,to,all,first,between,first,last, of); and start a new line for each one.
My request here is:


first, my list is really long -SO- how can I break it or separated in different lines.
Second: how to replace each word in the list for different replacement for each one.
This is the code I have:
Code:
Sub HiLightList()
Application.ScreenUpdating = False
Dim keywords As String, Rng As Range, i As Long
keywords = "if,then,from,to,all,first,between,first,last,of,and,or,print,find,swap,check,print,count,when,require,do not,with,but, rather than,where,is not,as,in,an,is,by,are,on,for,because,some,before,after,also,under,inside,around,into,next to,often,let,delete,copy,find,calculate,check,print,until,was,were,behind,had been"

For i = 0 To UBound(Split(keywords, ","))
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = Split(keywords, ",")(i)
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^l^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
   .MatchCase = False
    .MatchWholeWord = True
  
    .MatchWholeWord = True
    .Execute Replace:=wdReplaceAll
  End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Thank you for reading this post.
Reply With Quote
  #2  
Old 11-16-2018, 06:23 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,468
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

You could use something like:
Code:
Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, StrRep As String, i As Long
StrFnd = "if,then,from,to,all,first,between,first,last,of,and,or,print,find,swap," & _
  "check,print,count,when,require,do not,with,but,rather than,where,is not,as,in,an," & _
  "is,by,are,on,for,because,some,before,after,also,under,inside,around,into,next to," & _
  "often,let,delete,copy,find,calculate,check,print,until,was,were,behind,had been"
StrRep = "if,then,from,to,all,first,between,first,last,of,and,or,print,find,swap," & _
  "check,print,count,when,require,do not,with,but,rather than,where,is not,as,in,an," & _
  "is,by,are,on,for,because,some,before,after,also,under,inside,around,into,next to," & _
  "often,let,delete,copy,find,calculate,check,print,until,was,were,behind,had been"
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Replacement.Highlight = True
  .Format = True
  .Forward = True
  .MatchCase = False
  .MatchWholeWord = True
  .Wrap = wdFindContinue
  For i = 0 To UBound(Split(StrFnd, ","))
    .Text = Split(StrFnd, ",")(i)
    .Replacement.Text = "^l" & Split(StrRep, ",")(i)
    .Execute Replace:=wdReplaceAll
  Next
End With
Application.ScreenUpdating = True
End Sub
Note how the long lines are broken. The StrFnd and StrRep variables must have the same # of elements. Also note how the F/R code has been made more efficient by moving most of it outside the loop.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 11-16-2018, 06:38 PM
schausberger2019 schausberger2019 is offline Windows 7 64bit Office 2010
Novice
 
Join Date: Nov 2018
Posts: 3
schausberger2019 is on a distinguished road
Default

Thank you for your Answer.
I didn't got, for example how I will replace each word: example print replace for method, if for selection and so on.
Thank you so much Paul for the time you spend helping me.
Reply With Quote
  #4  
Old 11-16-2018, 06:43 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,468
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by schausberger2019 View Post
TI didn't got, for example how I will replace each word: example print replace for method, if for selection and so on.
You would have to replace all the terms in StrRep with the ones that correspond to with each of the StrFnd terms. So, where you see 'print' in StrFnd, you'd have to find which term that corresponds to in StrRep (which just happens to be 'print' because both strings are the same) and change that to 'method'.

PS: you have three 'print' entries in your list...
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 11-16-2018, 06:49 PM
schausberger2019 schausberger2019 is offline Windows 7 64bit Office 2010
Novice
 
Join Date: Nov 2018
Posts: 3
schausberger2019 is on a distinguished road
Default

work perfect thank you so much
Reply With Quote
Reply

Tags
vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
How can I compare a string in a cell to another string? Amitti Word VBA 2 04-10-2017 07:35 PM
How to find all string withing string. PRA007 Word VBA 18 02-12-2016 08:11 PM
Word Replacement Option mattyra Word 7 02-11-2015 11:13 PM
Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM


All times are GMT -7. The time now is 01:47 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft