Word Macro to change links in header/footer

  • Thread starter GainesvilleWes via OfficeKB.com
  • Start date
G

GainesvilleWes via OfficeKB.com

Good morning,
First off I would like to thank everyone on the fourms here for the help they
have given me over the last 2 weeks. You guys, (and gals), are wonderful.
Bravo Zulu.

Okay, I have the maco, (shown below, also aquired from these fourms), that
changes the source links in a word doc (to an excel worksheet) for all but
the one link in the header. What code would i add to this to change the link
in the header also?

---------------------------
Dim alink As Field, linktype As Range, linkfile As Range
Dim linklocation As Range, i As Integer, j As Integer, linkcode As Range
Dim Message, Title, Default, Newfile
Dim counter As Integer
Dim docPath As String

' Sets docPath to the .xls file in the user created directory
docPath = Replace$(ActiveDocument.Path & "\Mercury Commitment Excel
Worksheet.xls", "\", "\\") & "\\"

' Changes links to new path
counter = 0
For Each alink In ActiveDocument.Fields
If alink.Type = wdFieldLink Then

Set linkcode = alink.Code
i = InStr(linkcode, Chr(34))
Set linktype = alink.Code
linktype.End = linktype.Start + i
j = InStr(Mid(linkcode, i + 1), Chr(34))
Set linklocation = alink.Code
linklocation.Start = linklocation.Start + i + j - 1
If counter = 0 Then
Set linkfile = alink.Code
linkfile.End = linkfile.Start + i + j - 1
linkfile.Start = linkfile.Start + i
Default = linkfile
End If
linkcode.Text = linktype & docPath & linklocation
counter = counter + 1
End If
Next alink
 
J

Jean-Guy Marcil

GainesvilleWes via OfficeKB.com was telling us:
GainesvilleWes via OfficeKB.com nous racontait que :
Good morning,
First off I would like to thank everyone on the fourms here for the
help they have given me over the last 2 weeks. You guys, (and gals),
are wonderful. Bravo Zulu.

Okay, I have the maco, (shown below, also aquired from these fourms),
that changes the source links in a word doc (to an excel worksheet)
for all but the one link in the header. What code would i add to this
to change the link in the header also?

See
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
and change the code use to do the Find/Replace with your code.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

GainesvilleWes via OfficeKB.com

Ugh. I have been messing around with this for a few hours and I just cant
figure it out.
Any help you can give would save me from banging my head against my keyboard
until one of them breaks.



Jean-Guy Marcil said:
GainesvilleWes via OfficeKB.com was telling us:
GainesvilleWes via OfficeKB.com nous racontait que :
Good morning,
First off I would like to thank everyone on the fourms here for the
[quoted text clipped - 5 lines]
for all but the one link in the header. What code would i add to this
to change the link in the header also?

See
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
and change the code use to do the Find/Replace with your code.
 
J

Jezebel

Dim pField as Word.Field
Dim pRange as Word.Range
Dim pNewLink as string

pNewLink = ActiveDocument.Path & "\Mercury Commitment Excel Worksheet.xls"
(you don't want the double slashes for this method)
For each pRange in ActiveDocument.StoryRanges
Do
For each pField in pRange.Fields
if pField.Type = wdFieldLink then
pField.LinkFormat.SourceFullName = pNewLink
End if
Next
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next


This will get every field anywhere in the document (including headers and
footers) with the exception of fields within textboxes anchored to a header
or footer. Post again if you need to do that.




GainesvilleWes via OfficeKB.com said:
Ugh. I have been messing around with this for a few hours and I just cant
figure it out.
Any help you can give would save me from banging my head against my
keyboard
until one of them breaks.



Jean-Guy Marcil said:
GainesvilleWes via OfficeKB.com was telling us:
GainesvilleWes via OfficeKB.com nous racontait que :
Good morning,
First off I would like to thank everyone on the fourms here for the
[quoted text clipped - 5 lines]
for all but the one link in the header. What code would i add to this
to change the link in the header also?

See
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
and change the code use to do the Find/Replace with your code.
 
G

GainesvilleWes via OfficeKB.com

Jezebel,
Thank you for your reply and assistance.
However, the code you provided me seems to be stuck in an infinite loop when
i create a macro and plug it in.
Any ideas?

Dim pField as Word.Field
Dim pRange as Word.Range
Dim pNewLink as string

pNewLink = ActiveDocument.Path & "\Mercury Commitment Excel Worksheet.xls"
(you don't want the double slashes for this method)
For each pRange in ActiveDocument.StoryRanges
Do
For each pField in pRange.Fields
if pField.Type = wdFieldLink then
pField.LinkFormat.SourceFullName = pNewLink
End if
Next
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

This will get every field anywhere in the document (including headers and
footers) with the exception of fields within textboxes anchored to a header
or footer. Post again if you need to do that.
Ugh. I have been messing around with this for a few hours and I just cant
figure it out.
[quoted text clipped - 14 lines]
 
J

Jezebel

If you step through it using F8, what does it do?



GainesvilleWes via OfficeKB.com said:
Jezebel,
Thank you for your reply and assistance.
However, the code you provided me seems to be stuck in an infinite loop
when
i create a macro and plug it in.
Any ideas?

Dim pField as Word.Field
Dim pRange as Word.Range
Dim pNewLink as string

pNewLink = ActiveDocument.Path & "\Mercury Commitment Excel Worksheet.xls"
(you don't want the double slashes for this method)
For each pRange in ActiveDocument.StoryRanges
Do
For each pField in pRange.Fields
if pField.Type = wdFieldLink then
pField.LinkFormat.SourceFullName = pNewLink
End if
Next
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

This will get every field anywhere in the document (including headers and
footers) with the exception of fields within textboxes anchored to a
header
or footer. Post again if you need to do that.
Ugh. I have been messing around with this for a few hours and I just
cant
figure it out.
[quoted text clipped - 14 lines]
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
and change the code use to do the Find/Replace with your code.
 
G

GainesvilleWes via OfficeKB.com

When i step through it, it changes the first link in the list to the correct
folder, then keeps looping on that one. The remaining links do not change or
get altered in any way.
If you step through it using F8, what does it do?
Jezebel,
Thank you for your reply and assistance.
[quoted text clipped - 31 lines]
 
J

Jezebel

Puzzle. The code certainly works in general, so either there's a typo or
something else is going on. Try this --

For each pRange in ActiveDocument.StoryRanges
Do
Debug.print left(pRange, 20)
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

Try stepping through that. It should print the first 20 characters of each
story range in the immediate window.



GainesvilleWes via OfficeKB.com said:
When i step through it, it changes the first link in the list to the
correct
folder, then keeps looping on that one. The remaining links do not change
or
get altered in any way.
If you step through it using F8, what does it do?
Jezebel,
Thank you for your reply and assistance.
[quoted text clipped - 31 lines]
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
and change the code use to do the Find/Replace with your code.
 
G

GainesvilleWes via OfficeKB.com

I am sorry, i don't see where it is printing to.
But this code doesn't loop indefinetly. =)

Puzzle. The code certainly works in general, so either there's a typo or
something else is going on. Try this --

For each pRange in ActiveDocument.StoryRanges
Do
Debug.print left(pRange, 20)
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

Try stepping through that. It should print the first 20 characters of each
story range in the immediate window.
When i step through it, it changes the first link in the list to the
correct
[quoted text clipped - 9 lines]
 
J

Jezebel

It prints to the immediate window (below the code window in VBA). If that's
working, now try --

For each pRange in ActiveDocument.StoryRanges
Do
For each pField in pRange.Fields
debug.print pField.Code
Next
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

Step through that and check if it prints each field in the document just
once.



GainesvilleWes via OfficeKB.com said:
I am sorry, i don't see where it is printing to.
But this code doesn't loop indefinetly. =)

Puzzle. The code certainly works in general, so either there's a typo or
something else is going on. Try this --

For each pRange in ActiveDocument.StoryRanges
Do
Debug.print left(pRange, 20)
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

Try stepping through that. It should print the first 20 characters of each
story range in the immediate window.
When i step through it, it changes the first link in the list to the
correct
[quoted text clipped - 9 lines]
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
and change the code use to do the Find/Replace with your code.
 
G

GainesvilleWes via OfficeKB.com

Ah. I found the Immediate window, apparently i had that hidden.
I stepped through the code, and it prints all the links *except* the one in
the header. But it gets the rest one time.
It prints to the immediate window (below the code window in VBA). If that's
working, now try --

For each pRange in ActiveDocument.StoryRanges
Do
For each pField in pRange.Fields
debug.print pField.Code
Next
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

Step through that and check if it prints each field in the document just
once.
I am sorry, i don't see where it is printing to.
But this code doesn't loop indefinetly. =)
[quoted text clipped - 17 lines]
 
J

Jezebel

a) did the previous exercise (now you've found the immediate window) include
the header in what it printed?

b) is your field in the header within a textbox?



GainesvilleWes via OfficeKB.com said:
Ah. I found the Immediate window, apparently i had that hidden.
I stepped through the code, and it prints all the links *except* the one
in
the header. But it gets the rest one time.
It prints to the immediate window (below the code window in VBA). If
that's
working, now try --

For each pRange in ActiveDocument.StoryRanges
Do
For each pField in pRange.Fields
debug.print pField.Code
Next
Set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

Step through that and check if it prints each field in the document just
once.
I am sorry, i don't see where it is printing to.
But this code doesn't loop indefinetly. =)
[quoted text clipped - 17 lines]
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
and change the code use to do the Find/Replace with your code.
 
G

GainesvilleWes via OfficeKB.com

a) the previous exercise returns this (minus the ----------------)
---------------------------
Estimated Invoice








--------------------------

b) when you ask me if my field is in a header within a text box, i am not
entirely sure what you mean but i will do my best to describe it.
the header in the word doc starts on the second page. the link is to the file
number that is obtained from the excel sheet. i put in the link using the
copy -> paste special -> paste link -> formatted text.
hopefully that helps you. if not, i would be more than willing to send you
both the excel sheet and the word doc so you can see what i am talking about.

also, on a side note. you are spending a lot of time helping me out here, and
i am more than willing to compensate you for your assistance. my email is
wesleyD0TmillerATredvisionD0Tcom, let me know.
a) did the previous exercise (now you've found the immediate window) include
the header in what it printed?

b) is your field in the header within a textbox?
Ah. I found the Immediate window, apparently i had that hidden.
I stepped through the code, and it prints all the links *except* the one
[quoted text clipped - 22 lines]
 
J

Jean-Guy Marcil

GainesvilleWes via OfficeKB.com was telling us:
GainesvilleWes via OfficeKB.com nous racontait que :
a) the previous exercise returns this (minus the ----------------)
---------------------------
Estimated Invoice








--------------------------

b) when you ask me if my field is in a header within a text box, i am
not entirely sure what you mean but i will do my best to describe it.
the header in the word doc starts on the second page. the link is to
the file number that is obtained from the excel sheet. i put in the
link using the copy -> paste special -> paste link -> formatted text.
hopefully that helps you. if not, i would be more than willing to
send you both the excel sheet and the word doc so you can see what i
am talking about.

If your header starts on the second page, does this means that you have a
section break at the end of page 1?
If so, presumably the header on page 1 (section 1) is empty.
If that is the case, you have to use the workaround explained in the web
site I linked you to in my first reply. This situation is described.
Just remove all the code that has to do with Finding/Replacing and use your
own instead, keep the StoryRanges loops and the workaround to get around the
problem of the empty first section header/footer that the regular loop can't
get pass.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

GainesvilleWes via OfficeKB.com

Hrum. I will give it a shot.


Jean-Guy Marcil said:
GainesvilleWes via OfficeKB.com was telling us:
GainesvilleWes via OfficeKB.com nous racontait que :
a) the previous exercise returns this (minus the ----------------)
---------------------------
[quoted text clipped - 17 lines]
send you both the excel sheet and the word doc so you can see what i
am talking about.

If your header starts on the second page, does this means that you have a
section break at the end of page 1?
If so, presumably the header on page 1 (section 1) is empty.
If that is the case, you have to use the workaround explained in the web
site I linked you to in my first reply. This situation is described.
Just remove all the code that has to do with Finding/Replacing and use your
own instead, keep the StoryRanges loops and the workaround to get around the
problem of the empty first section header/footer that the regular loop can't
get pass.
 

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