Solved

Batch replace of hyperlinks in Word 2000

Posted on 2000-02-25
7
824 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

680 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