PDA

View Full Version : [SOLVED:] VBA Word search and replace address of url



JPG
06-10-2020, 08:16 AM
I have been trying to get the best VBA code for this as regards speed and accuracy. 5000 links to check and change address

I have tried exposing the code of Hyperlinks by selecting all and keypress Shift+F9. Seemed to work and then I noticed partial replacement of urls.

So next try was




Dim doc As Document
Dim link, i


'Loop through all open documents.
For Each doc In Application.Documents
'Loop through all hyperlinks.
doc.Hyperlinks(i).Address = Replace(doc.Hyperlinks(i).Address, "Gentiles", "Gentile")
'Other links would be here.
Next
Next


This seems a slow way of replacing... Plus there did not seem to be any wildcards that would work, so if there was a subsequent find and replace for example on the word "Gent" to replace with "Gentleman", "Gentile" would be changed to "Gentlemantile"

If there is a quicker more robust way of replacing part of the url please help.
p.s the address is custom like this "tw://[self]?Gentiles" if that makes any difference.

gmayor
06-11-2020, 02:17 AM
Try the following which will replace the string strFind with the string strReplace (called from your loop) in both the address and the display text then update the fields.


Sub ReplaceInLink(strFind As String, strReplace As String)
Dim oStory As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strFind
.Replacement.Text = strReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
'replace in address
ActiveWindow.View.ShowFieldCodes = True
.Execute Replace:=wdReplaceAll
'replace in display text
ActiveWindow.View.ShowFieldCodes = False
.Execute Replace:=wdReplaceAll
End With
End With
'update the links
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
lbl_Exit:
Set oStory = Nothing
Exit Sub
End Sub

JPG
06-11-2020, 02:36 AM
Will try thanks. But note it is only the address that needs to be changed, it is the reason I am having to change the address. The word appears in a sentence with an asterisk at the head. This tells you to go look for the word in a glossary, and it is not always the exact word there as you can imagine.

JPG
06-11-2020, 03:26 AM
I have been trying to get the best VBA code for this as regards speed and accuracy. 5000 links to check and change address

I have tried exposing the code of Hyperlinks by selecting all and keypress Shift+F9. Seemed to work and then I noticed partial replacement of urls.

So next try was




Dim doc As Document
Dim link, i


'Loop through all open documents.
For Each doc In Application.Documents
'Loop through all hyperlinks.
doc.Hyperlinks(i).Address = Replace(doc.Hyperlinks(i).Address, "Gentiles", "Gentile")
'Other links would be here.
Next
Next


This seems a slow way of replacing... Plus there did not seem to be any wildcards that would work, so if there was a subsequent find and replace for example on the word "Gent" to replace with "Gentleman", "Gentile" would be changed to "Gentlemantile"

If there is a quicker more robust way of replacing part of the url please help.
p.s the address is custom like this "tw://[self]?Gentiles" if that makes any difference.

This is the way I need to go I think, because it confines the search to the address only. My only issue with it is I don't know if it is possible to use wildcard settings to the search.
If anyone can assist on that area I would be grateful.

gmayor
06-11-2020, 04:16 AM
If you remove the second
.Execute Replace:=wdReplaceAllit will not change the display text.

JPG
06-11-2020, 04:39 AM
the problem then is that if the word I want to replace in the address field exists in normal body text, then that gets replaces also.


ok this has taken enough of your time and the issue is not solved.

I will abandon this.

thank you for your time. Much appreciated.