#1
|
|||
|
|||
Case sensitivity not followed when using replace method.
Hi all.. I hope all are doing well.. I have been trying to develop a small macro which could find certain words from a PowerPoint presentation and replace them with certain other words using the method TextRange.Replace(FindWhat:=FindString, _ Replacewhat:=ReplaceString, WholeWords:=True, MatchCase := False) The requirement is that the ReplaceString should have the same case sensitivity as that of the words to be replaced in the presentation. But using the above method, the case of the ReplaceString always remains the same( as defined earlier). I need it to change it as per the case of the FindString from the presentation. For example, if my FindString is bridge and ReplaceString is brg, in my presentation, if Bridge comes up, it should be replaced with Brg ( but with above method, it is replacing Bridge with brg) I hope my query has made some sense.. Can anyone please help me with this? Thanks Regards Last edited by pushpi004; 10-10-2020 at 10:35 AM. Reason: Providing example for better understanding |
#2
|
|||
|
|||
That's not how MatchCase works it applies to the word to be replaced.
If set to false both Bridge, bridge and e.g. BrIDGe would be replaced with brg Try Replace Bridge with Brg matchCase True AND also Replace bridge with brg (matchcase True) |
#3
|
|||
|
|||
Quote:
Hi John.. thanks for the reply.. I am aware of the functionality of MatchCase.. I do not require MatchCase feature. As per my example, I have defined that the all the words irrespective of their cases should be replaced, but the replaced word must have same case as that of the word being replaced in the presentation, i.e. if my presentation have both, Bridge and BRIDGE, they should be replaced with Brg and BRG respectively. Unfortunately, Textrange.replace is replacing both with brg. I hope I have made myself clearer. I request if you have any solution to this problem. Thanks |
#4
|
|||
|
|||
As I already said ...
You would need to account for each case e.g. Replace BRIDGE with BRG (matchcase=True) Replace Bridge with Brg (matchcase=True) Replace bridge with brg (matchcase=True) |
#5
|
|||
|
|||
Quote:
Apparently, this isn't an efficient solution in my case.. Thanks anyway |
#6
|
|||
|
|||
Solved: >Case sensitivity not followed when using replace method
Hi all..
Looks I have come across a solution to my problem. It is indeed using MatchCase as suggested by John, but in a slightly more developed logic. Nevertheless, it does the trick. Sub DoReplace(tr As TextRange, findThis, replaceWith) If InStr(1, tr.Text, findThis, vbTextCompare) > 0 Then 'is the word found at all? 'found at least one case form - just try to replace any case form found... tr.Replace findWhat:=LCase(findThis), Replacewhat:=LCase(replaceWith), _ WholeWords:=True, MatchCase:=True tr.Replace findWhat:=UCase(findThis), Replacewhat:=UCase(replaceWith), _ WholeWords:=True, MatchCase:=True tr.Replace findWhat:=StrConv(findThis, vbProperCase), _ Replacewhat:=StrConv(replaceWith, vbProperCase), _ WholeWords:=True, MatchCase:=True End If End Sub I hope it helps others as well. Best of luck! Cheers! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Title case macro gets a "type mismatch" error at the Instr method | marceepoo | Word VBA | 1 | 03-05-2020 09:41 PM |
Drawing Guides Sensitivity Setting? | Slidemaker | PowerPoint | 2 | 05-18-2017 12:45 PM |
Using wildcards in Find/Replace to change font/case | angie450 | Word | 2 | 05-26-2016 11:09 AM |
Find and replace with a macro, problem with letter case | garcanrya | Word VBA | 2 | 01-10-2014 05:40 AM |
Replace & case | Jennifer Murphy | Word | 1 | 02-11-2013 03:26 AM |