replacing hyperlinks using vba

J

Julie

Hi

i have a large number of documents (100+) in which hyperlinks need to
be changed. I know that i can do edit / replace on the hyperlinks but
i need to use code as its not a simple change

for example
the following hyperlink address
C:\my documents\methodology documents\drm\drm_010.doc
needs changing to
http://www.govsec.gov.au/index.cfm?fuseaction=drm.drm_010
and
the following hyperlink address
C:\my documents\methodology documents\soc\soc_010_015.doc
needs changing to
http://www.govsec.gov.au/index.cfm?fuseaction=soc.soc_010_015

etc
so i'm working on the theory that i need to open every document in the
directory, determine the first three letters of the filename, display
field codes, edit replace the paths (using the first three letters of
the filename) and removing the .doc extension
am i on the right track ... is there an easier way?
and the bit of code that i'm not sure about is the find & replace bit
- can i use something like:

dim firstthree as string
dim fname as string
dim oldpath as string
dim newpath as string

fname = activedocument.name
firstthree = left(fname, 3)
oldpath = "C:\my documents\methodology documents\" & firstthree &
fname
newpath = "http://www.govsec.gov.au/index.cfm?fuseaction=" &
firstthree & =LEFT(fname,LEN(fname)-4)

find oldpath 'really not sure how to do this
replace newpath

Cheers
JulieD
 
J

Jezebel

Use code along these lines:

Dim pHyperlink as Word.Hyperlink
Dim pOld as string
Dim pNew as string
Dim pSegments() as string

For each pHyperlink in ActiveDocument
pOld = pHyperlink.Address
pSegments = split(pOld, "\")
pNew = pSegments(Ubound(pSegments)) 'Get last part of path
pNew = Left(pNew, len(pNew)-4) 'Discard ".doc"

pNew = "http://www.govsec.gov.au/index.cfm?fuseaction=" & left(pNew,3) &
"." & pNew

' For debugging, run a few documents with this line
debug.print pOld, pNew 'Check that you're getting what you
expect

'If the above is OK, uncomment this line
'pHyperlink.Address = pNew

Next
 
P

Peter Hewett

Hi Julie

I probably wouldn't use search and replace for this, I'd iterate the documents Hyperlinks
collection. You'll probably need to change the Address and TextToDisplay properties.

HTH + Cheers - Peter


(e-mail address removed) (Julie), said:
 
J

JulieD

Hi Peter

some of my google reading was saying that iterating the hyperlinks
collection was "flakey" and it's better to use search & replace ...

i'm using word 2000 btw.

being that there's so many documents, i don't want to have to go through
them all manually afterwards and double check all the links ... have you
found using the collection works okay?

Cheers
JulieD
 
J

JulieD

Hi jezebel

only problem is that i can't do every hyperlink in the active document -
sorry i should have explained this in my earlier post - there are basically
three types of hyperlinks used - the one i outlined, then one which
references a glossary and targets within the glossary document and then ones
that refernce documents but on a server drive.

so i need to "FIND" the ones with *.doc (without # and without '\source
docs\' in the name) and then replace those with the new form of the link,
then i need to do the ones with # and then i need to do the ones with
'\source docs\' ... i didn't mention it earlier because i (a) was on a lunch
break and seriously running out of time and (b) thought that once i had the
idea of how to do the one i asked about i could figure the other two out.

any ideas ... or could i cycle through all the hyperlinks (as outlined by
you) using an IF statement to do the three different types? ... might look
into this.

cheers
JulieD
 
J

Jonathan West

JulieD said:
Hi Peter

some of my google reading was saying that iterating the hyperlinks
collection was "flakey" and it's better to use search & replace ...

Its only flakey if you are deleting or adding hyperlinks from the document.
If you are merely changing the properties of some of them, it is perfectly
reliable. Even if you are deleting or adding links, then it can be reliable
so long as you know what you are doing and how the collection behaves.
i'm using word 2000 btw.

being that there's so many documents, i don't want to have to go through
them all manually afterwards and double check all the links ... have you
found using the collection works okay?

Yes, for the purpose you are describing. it seems that you have a means of
distinguishing between the various types of link by examing the Address and
SubAddress property of each one and branching accordingly.
 
J

JulieD

Hi guys

pretty much got it working .. only having trouble with the hyperlink with
the # in it ... when i get "pOld" it doesnt' seem to read the #abc bit ...

here's the code i've got todate ... any ideas?
Sub changelinks()
Dim pHyperlink As Word.Hyperlink
Dim pOld As String
Dim pNew As String
Dim pSegments() As String
Dim tstring As String

For Each pHyperlink In Documents(1).Hyperlinks
pOld = pHyperlink.Address
tstring = Right(pOld, 3) 'check to see what type
of hyperlink
If tstring = "doc" Then
If InStr(1, pOld, "my pictures") Then
pNew = "c:\my documents\newsgroup examples\test.doc"
Else
pSegments = Split(pOld, "\")
pNew = pSegments(UBound(pSegments)) 'Get last part of
path
pNew = Left(pNew, Len(pNew) - 4) 'Discard
".doc"
pNew = "http://www.govsec.gov.au/index.cfm?fuseaction=" &
Left(pNew, 3) & "." & pNew
End If
ElseIf InStr(1, pOld, "#") Then
pSegments = Split(pOld, "#")
pNew = pSegments(UBound(pSegments)) 'Get last part of
path
pNew = Left(pNew, Len(pNew) - 4) 'Discard
".doc"
pNew = "http://www.govsec.gov.au/index.cfm?fuseaction=glossary#"
& pNew
End If

' For debugging, run a few documents with this line
Debug.Print pOld, pNew 'Check that you're getting what
you expect

'If the above is OK, uncomment this line
pHyperlink.Address = pNew

Next

End Sub

--

i appreciate the input.

cheers
JulieD
 
J

Jonathan West

JulieD said:
Hi guys

pretty much got it working .. only having trouble with the hyperlink with
the # in it ... when i get "pOld" it doesnt' seem to read the #abc bit ...

Everything to the right of the # goes into the SubAddress property of the
hyperlink, not the address. You need to read both to get the full link
destination.
 
P

Peter Hewett

Hi JulieD

I suspect that a *lot* of the posts where people are using collection objects and then
deleting stuff from it/them and then complaining that "there's a problem" with the
collection object is due to start-end iteration rather than end-start iteration.

As Jay says it's mostly a case of understanding what you're doing to start with.

Good luck + Cheers - Peter

Hi Peter

some of my google reading was saying that iterating the hyperlinks
collection was "flakey" and it's better to use search & replace ...

i'm using word 2000 btw.

being that there's so many documents, i don't want to have to go through
them all manually afterwards and double check all the links ... have you
found using the collection works okay?

Cheers
JulieD

HTH + Cheers - Peter
 
J

JulieD

Hi Jonathan

that solved it

cheers
julieD

Jonathan West said:
....

Everything to the right of the # goes into the SubAddress property of the
hyperlink, not the address. You need to read both to get the full link
destination.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top