Solved

Batch replace of hyperlinks in Word 2000

Posted on 2000-02-25
7
821 Views
Last Modified: 2010-04-04
Ok, i have 500 differents .DOC files. Most of those files have hyperlinks in them. I would like to replace the HyperLink "http://www.this.com" named "THIS" by the HyperLink "http://www.that.com" (I don't care about the name of the hyperlink)

Is there a way to do this WITHOUT opening each of the .DOC files to verify if the link is in it and then change it manually...

Excuse my english and... Thanks !
0
Comment
Question by:ouaouaron
7 Comments
 
LVL 4

Expert Comment

by:dovcamp
ID: 2560431
Here's a quick and dirty way, it's not perfect, but it should work.  First do a batch conversion of all files to .rtf format (this should preserve the majority of formating, but some newer features could be lost) using Word 2000's Batch Conversion Wizard.  Then use any batch search and replace utility to replace the hyperlinks that you mention above.  Several shareware batch search and replace programs can be found at http://winfiles.cnet.com/apps/98/text-search.html (choose your favorite).
Finally, convert all .RTF files back to .DOC files.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 2560883
Hi ouaouaron,

this little sub will check all documents in the path specified for the text "http://www.this.com", replace all matches by "http://www.that.com" and close the file, saved with the changes.

Supposing you don't have too much directories, that should do the job rather quickly, and you don't have to pass to .rtf files and formatting losses.

Sub Macro3()
    MyFile = Dir("C:\MyPath1\MyPath2\*.doc")
    While MyFile <> ""
    Documents.Open FileName:=MyFile
    With Selection.Find
        .Text = "http://www.this.com"
        .Replacement.Text = "http://www.that.com"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Documents(MyFile).Close SaveChanges:=wdSaveChanges
    MyFile = Dir
    Wend
End Sub

Hope this helps

Calacuccia
0
 
LVL 4

Expert Comment

by:Noggy
ID: 2562874
Heh, heh :-) , you'll never guess what: there is actually a http://www.this.com and a http://www.that.com - though you may find that you are forbidden to access the latter......


.....there is also a http://www.thisnthat.com AND a http://www.thisandthat.com.

PS Sorry about the tangent, ouaouaron.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ouaouaron
ID: 2565559
Thanks for putting a smile in my face on a monday morning Noggy... That's something.. :)

I can't use Dovcamp's solution because of the complexity of my .doc files...


Calacuccia, your solution would work fine if i had to replace the name of the link. But it's the link itself that i have to change and a find methode will not find it.

But i think i've work it out this morning...:

Sub pp_ReplaceHyperLinks()


 MyFile = Dir("C:\*.doc", 16)
 
 While MyFile <> ""
   
    Documents.Open "C:\" & MyFile
   
    For Each hLink In ActiveDocument.Hyperlinks
   
        If InStr(hLink.Address, "this.com") <> 0 Then
            hLink.Address = "www.that.com"
        End If
       
    Next hLink

    ActiveDocument.Save
    ActiveDocument.Close
   
    MyFile = Dir
   
 Wend
 
 End Sub


0
 
LVL 17

Accepted Solution

by:
calacuccia earned 200 total points
ID: 2566887
Hi Ouaouaron,

the macro you typed is indeed a big step in the good direction. Forgive me for not having very well interpreted your question the first time and mixing up the naes with the actual hyperlinks.

The code I will post below will work to remove hyperlinks corresponding to what you specify and create a new one following your specification on the same place without changing the "Name".
It has to be done that way, because the Hyperlink.Address property is read-only and can not be modified by VBA once it exists, so the only method is to remove old hyperlink, and add a new one.

For the moment, I've taken my own Dir loop through one directory, as yours will not work. By using the ,16) at the end of your first dir call, you indeed look for subdirectories, but all you will get are their names, the macro won't loop through it.

So for the moment, as a first step, consider this, while I continue to look for a loop through all subdirectories, as that seems what you're looking for.

Sub pp_ReplaceHyperLinks()
 MyPath = "C:\MyPath1\MyPath2\"
 MyFile = Dir(MyPath & "*.doc")
    While MyFile <> ""
    Documents.Open FileName:=MyPath & MyFile
   
    For Each hlink In ActiveDocument.Hyperlinks
        If hlink.Address = "http://www.this.com/" Then
            hlink.Range.Select
            hlink.Delete
            ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "http://www.that.com/", SubAddress:=""
        End If
    Next hlink

    ActiveDocument.Save
    ActiveDocument.Close
     
    MyFile = Dir
     
 Wend
 
 End Sub

Calacuccia
0
 

Author Comment

by:ouaouaron
ID: 2569149
I calacuccia.

Thanks for the replay again...

I don't understand when you say that my macro won't work. I already used it to changed all my .doc files and it worked just well. The Hyperlinks.Address is R/W.

You are right about the (,16), it is useless... i realised it when i first tried my macro and that's why i had to had the path myself ("C:\"). But i forgot to remove the ,16....

You last comment made me realise that the 16 parameters is not for having the name of the file with it's current path but instead for conscidering folders in the loop...

(i'm french and i probably misunderstood the english description of the 16 parameter...)

Thanks for your time and bye !
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 2569499
Hi Ouaouaron,

you're welcome.

Just to clear things up, I use Word 97 and in that previous version, the Hyperlinks.address is read-only, which explains my last comment/code modification. Just another reason to get Office 2000.

J'en profite pour te dire un petit bonjour en français, c'est assez rare ici de trouver des francophones. Moi-même, je suis belge, et de la partie flamande, mais comme j'ai habité Lille pendant 2 ans (où je travaillas), j'aime bien une p'tite conversation pour m'excercer un peu.

Salut,

Calacuccia
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will show you how to use shortcut menus in the Access run-time environment.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now