Change like https://imgur.com/kix3xWe to https://i.imgur.com/kix3xWe.jpg
HiI need some help with Word VBA to replace/ change a lot of links in Word texts. ( The links are to a cloud image place, and they stopped working, but I found a workaround that makes them work. This workaround involves changing slightly the link format )
I expect I need to do something with those wild things, but for some reason my brain seems to be allergic to those and cant get my head around the syntax, or I can’t find a clear blog on it with lots of good examples to help me get the hang of using them.. ….
_._______________
So what I want to be able to do is:- I will select some text in a word document, then run the macro that will replace any occurrences of URL text like this
https://imgur.com/kix3xWe
to this
https://i.imgur.com/kix3xWe.jpg
In other words the bit at the start ,
https://imgur.com/
is changed to
https://i.imgur.com/
and an extra
.jpg
Is added on the end
This bit, kix3xWe , can be 6 or 7 characters made up of normal letters or numbers. The letters can be upper case or lower case.
The macro below does the first bit. Can anyone help me do it all, either by modifying that macro , or a better macro?
Note: My separator is a semi colon ; ( I use German Office )
Thanks
Alan
Code: Select all
Sub ImgurWorkaround() ' To change in a Word doc selection, things like https://imgur.com/kix3xwe to https://i.imgur.com/kix3xwe.jpg
Rem 1 Reset Selection stuff
With Selection.Find
.ClearFormatting: .Replacement.ClearFormatting: .Text = "": .Replacement.Text = "": .Forward = True: .Wrap = wdFindAsk: .Format = False: .MatchCase = False: .MatchWholeWord = False: .MatchKashida = False: .MatchDiacritics = False: .MatchAlefHamza = False: .MatchControl = False: .MatchWildcards = False: .MatchSoundsLike = False: .MatchAllWordForms = False '
End With
Rem 2
With Selection.Find ' This is the VBA code ( or very similar ) used by Excel when Using the Find Replace text Dialogue box. So this is an improved version of what a macro recording would give.
.ClearFormatting: .Replacement.ClearFormatting ' Don't use formating, ? not sure this comes into the equation ??
.Wrap = wdFindStop ' Tell Word not to continue past the end of the selection ( And therefore prevents also a display Alert asking )
.MatchWildcards = False ' Don't use wildcards. The default anyway, but in this code is an important - if not set to false, then
.Text = "https://imgur.com/"
.Replacement.Text = "https://i.imgur.com/"
.Execute Replace:=wdReplaceAll ' Replace all within selection. This is the "OK" button!
' I expect something with Wild things might be needed here...
End With
Rem 10 Reset Selection stuff
With Selection.Find
.ClearFormatting: .Replacement.ClearFormatting: .Text = "": .Replacement.Text = "": .Forward = True: .Wrap = wdFindAsk: .Format = False: .MatchCase = False: .MatchWholeWord = False: .MatchKashida = False: .MatchDiacritics = False: .MatchAlefHamza = False: .MatchControl = False: .MatchWildcards = False: .MatchSoundsLike = False: .MatchAllWordForms = False '
End With
End Sub