#1
|
|||
|
|||
How to replace a letter to random letter with different color?
Hello,
Is there any way in MS Office Word 2007, to replace a letter to random letter with different color automatically? For example: change "a" to random letter. "Mama gave me an apple" to "Mdme gsve me tn rpple" As far as I know, I can use Replace All, and Format -> Font -> Color (in Ctrl+H), but no random letter selection to be replaced. Thanks in advance. |
#2
|
||||
|
||||
No. Word has no built-in randomising functions for a Replace.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
I love these seemingly pointless puzzles
You cannot do it with replace, but you can do it with a macro http://www.gmayor.com/installing_macro.htm Code:
Option Explicit Sub RandomLetters() Dim oRng As Range Dim oFind As Range Dim i As Long Dim strChar As String Start: strChar = InputBox("Replace which character?", , "a") If Len(strChar) > 1 Or Not IsLetter(asc(strChar)) Then MsgBox "Enter a single character from the ranges A-Z or a-z only", vbCritical GoTo Start End If Set oFind = Selection.Range Set oRng = Selection.Range For i = 1 To oRng.Characters.Count With oRng.Find Do While .Execute(FindText:=strChar, MatchCase:=True) If oRng.InRange(oFind) Then oRng.Text = RandomLetter(strChar) oRng.Font.ColorIndex = wdRed End If oRng.Collapse wdCollapseEnd Loop End With Next i End Sub Private Function IsLetter(ByVal i As Integer) As Boolean Select Case i Case 65 To 90, 97 To 122 IsLetter = True Case Else IsLetter = False End Select lbl_Exit: Exit Function End Function Private Function RandomLetter(strChar As String) As String Select Case asc(strChar) Case 65 To 90 RandomLetter = Chr(65 + Rnd() * 10000000 Mod 26) Case 97 To 122 RandomLetter = Chr(97 + Rnd() * 10000000 Mod 26) Case Else RandomLetter = strChar End Select lbl_Exit: Exit Function End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
Dear Mr. Macropod and Mr. Graham Mayor, thank you vey much for your fast reply and help. I will try to use the script first.
|
#5
|
|||
|
|||
This is my first time using macro, and your script works like charm. However, can I replace the symbols too? Like comma (,), space ( ) and others; and to more character other than alphabet. For example:
Replace space ( ) to random "Mama gave me an apple" to "Mama$gavermeдan2apple" I'm managed to choose the color by replace the wdred to ie: RGB(123,123,123) Sorry a lot for not request the right thing in the first place, and for request too much. Thank you. |
#6
|
||||
|
||||
Change
Code:
If Len(strChar) > 1 Or Not IsLetter(asc(strChar)) Then MsgBox "Enter a single character from the ranges A-Z or a-z only", vbCritical GoTo Start End If Code:
If Len(strChar) > 1 Then MsgBox "Enter a single character only", vbCritical GoTo Start End If
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Thank you Mr. Mayor, in addition on top script, the new one just change the color if I choose to replace the symbol. However, it was good enough for me. Thank you, I will buy you a cup of coffee from your website later.
|
#8
|
||||
|
||||
You are right, it does. I had forgotten to include the additional characters in the RandomLetter macro
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
font color, random, replace all |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
how do i get the letter in the centre | rich_cirillo | Word | 3 | 03-09-2014 06:25 PM |
Find and replace with a macro, problem with letter case | garcanrya | Word VBA | 2 | 01-10-2014 05:40 AM |
every letter different color | skan | Word VBA | 8 | 03-28-2013 04:16 AM |
Business letter | MK_Huef | Word | 1 | 03-27-2012 03:27 AM |
Replace is capitalizing the first letter of the first word | Beachhouse | Word | 1 | 02-07-2012 03:36 PM |