Link to home
Create AccountLog in
Avatar of tonygardner1
tonygardner1Flag for United States of America

asked on

Need Assistance with Using Variables within a Find and Replace VB Script

Experts:
I have another puzzle that needs solving, and given that the answer is very likely to be extremely rudimentary, I will award 200 points for the kind soul who can provide an answer. Obviously, if it's more complicated that I believe it to be, I will increase the point value accordingly.

I have made a simple VB script which will be used in Word that prompts for an RGB value, and places the result in a Find & Replace operation to change from the input value to the desired value (0,0,144). Here is what I've put together, errors and all:

Sub SetRGB144()
'
' Set all instances of provided RGB to RGB(0,0,144)
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
' I am uncertain as to the correct type to assign RGB_String
    Dim RGB_String As Variable
    RGB_Value = InputBox("Please input the RGB values separated by commas:", "RGB Values", "0,0,128")
' The below line produces a run-time error 424: Object Required
    Set RGB_String = "RGB(" & RGB_Value & ")"
    With Selection.Find
        .Text = "^?"
        .Font.Color = RGB_String
        .Replacement.Text = "^&"
        .Replacement.Font.Color = RGB(0, 0, 144)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Open in new window


As always, your help is most sincerely appreciated!

Tony G.
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of tonygardner1

ASKER

Thanks MatthewsPatrick!

That worked like a champ. I would like to add to my question, so I'm bumping the point value to 500.

Is it possible for Word to detect the RGB value of the text it's currently sitting on? Selected text would also work if it's possible for the Find & Replace to still cover the whole document. Obviously, this would allow us to eliminate the prompt altogether.

Cheers,
Tony G.

Current VB Script:
Sub SetRGB144()
'
' Set all instances of RGB(x,y,z)  to RGB(0,0,144)
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Dim RGB_Array As Variant
    RGB_Value = InputBox("Please input the RGB values separated by commas:", "RGB Values", "0,0,128")
    RGB_Array = Split(Replace(RGB_Value, " ", ""), ",")
    With Selection.Find
        .Text = "^?"
        .Font.Color = RGB(RGB_Array(0), RGB_Array(1), RGB_Array(2))
        .Replacement.Text = "^&"
        .Replacement.Font.Color = RGB(0, 0, 144)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Open in new window

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
No problem. Glad to help ;-)
Both contributions were extremely helpful in helping me to create the final subroutine.