I need a macro to update a link

T

TJ

This seems pretty straight forward but Word will not record the macro - and I am such a newby. I try to record the macro by turning on the recorder and then clicking Edit...Link...Locked(which I am unlocking or removing the checkmark)...Update Now...Locked (to relock the link)...OK. I edit the macro and recorder hasn't recorded anything. What am I missing?? I have to do this function about 3800 times and really don't relish the thought of doing this using a customized toolbar. Any thoughts??
 
P

Peter Hewett

Hi

This may be better than thoughts, give this a try:

Public Sub UpdateAllLinks()
Dim fldItem As Word.Field
Dim boolLockState As Boolean

' All fields in the MainTextStory
For Each fldItem In ActiveDocument.Fields

' Update any linked fields
If fldItem.Type = wdFieldLink Then
boolLockState = fldItem.Locked
If boolLockState Then fldItem.Locked = False
fldItem.Update
fldItem.Locked = boolLockState
End If
Next
End Sub

It unlocks the field if necessary, updates the link and then restore the
locked state. It only updates fields in the MainTextStory, not
headers/footers/footnotes/endnotes etc. Let me know if you have linked fields
in any of these other story ranges.

HTH + Cheers - Peter
 
P

Peter Hewett

Hi

Sorry try this version, the problem with linked fields is that there are
many different types:

Public Sub UpdateAllLinks()
Dim fldItem As Word.Field
Dim boolFieldLockState As Boolean
Dim boolLinkState As Boolean

' All fields in the MainTextStory
For Each fldItem In ActiveDocument.Fields

' Update any linked fields
If fldItem.Type = wdFieldLink Then
boolFieldLockState = fldItem.Locked
If boolFieldLockState Then fldItem.Locked = False

' Now try to update the linked field
On Error Resume Next
With fldItem.LinkFormat
boolLinkState = .Locked
.Locked = False
.Update
.Locked = boolLinkState
End With
On Error GoTo 0
fldItem.Locked = boolFieldLockState
End If
Next
End Sub

Currently any error while messing with the LinkForm are ignored as I've no
idea what objects are being linked, what types of error they can through
and no idea what to do about it!

HTH + Cheers - Peter
 

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