If I try to edit the link to point to a different image file (in the Links dialog) then Word crashes (Office 2007).
OK, Is your Word installation fully updated (SP2+)? Have you tried repairing it (Word Options|Resources|Detect & Repair)?
Beyond that, try the following code, which is based on the link I posted previously, plus the alterations I suggested in my previous
posts:
Option Explicit
' Word macro to automatically update IncludePicture field links.
' Created by macropod.
Dim TrkStatus As Boolean ' Track Changes flag
Dim Pwd As String ' String variable to hold passwords for protected documents
Dim pState As Boolean ' Document protection state flag
Sub ChangePicLinks()
' This routine runs whenever the document is opened.
' It calls on the others to do the real work.
' Prepare the environment.
With ActiveDocument
' Insert your document's password between the double quotes on the next line
Pwd = ""
' Initialise the protection state
pState = False
' If the document is protected, unprotect it
If .ProtectionType <> wdNoProtection Then
' Update the protection state
pState = True
' Unprotect the document
.Unprotect Pwd
End If
Call MacroEntry
' Most of the work is done by this routine.
Call UpdateIncludePictureFields
' Go to the start of the document
Selection.HomeKey Unit:=wdStory
' Clean up and exit.
Call MacroExit
' If the document was protected, reprotect it, preserving any formfield contents
If pState = True Then .Protect wdAllowOnlyFormFields, Noreset:=True, Password:=Pwd
End With
End Sub
Private Sub MacroEntry()
' Store current Track Changes status, then switch off temporarily.
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
End With
' Turn Off Screen Updating temporarily.
Application.ScreenUpdating = False
End Sub
Private Sub MacroExit()
' Restore original Track Changes status
ActiveDocument.TrackRevisions = TrkStatus
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
Private Sub UpdateIncludePictureFields()
' This routine sets the new path for external links, pointing them to the current folder.
Dim oRange As Range
Dim oField As Field
Dim OldPath As String
Dim NewPath As String
Dim OldFile As String
Dim NewFile As String
' Go through all story ranges in the document, including shapes,
' headers & footers.
For Each oRange In ActiveDocument.StoryRanges
' Go through the fields in the story range.
For Each oField In oRange.Fields
With oField
' Skip over fields that don't have links to image files.
If .Type = wdFieldIncludePicture Then
' Get the old path
OldPath = .LinkFormat.SourcePath
' Get the old filename
OldFile = .LinkFormat.SourceName
' Get the new path.
GetPath:
NewPath = InputBox("The document contains a link to an image named:" _
& vbCr & OldFile & vbCr & "in the folder below." & vbCr & _
"What is the new Path?", "Change File Path", OldPath)
If NewPath = "" Or InStr(NewPath, ":") = 0 Then GoTo GetPath
OldPath = Replace(.LinkFormat.SourcePath, "\", "\\")
' Get the new filename
GetName:
NewFile = InputBox("What is the new Filename?", "Change Filename", OldFile)
If NewFile = "" Or InStr(NewFile, ".") = 0 Then GoTo GetName
' Replace the link to the external file
.Code.Text = Replace(.Code.Text, OldPath & "\\" & OldFile, NewPath & "\\" & NewFile)
.Update
End If
End With
Next oField
Next oRange
End Sub
Place the code in your document's template (or Normal.dot/dotm), then activate the document to be updated and run the macro
'ChangePicLinks'.
--
Cheers
macropod
[Microsoft MVP - Word]
Robin said:
OK, I can try and explain the problem like this...
A company has a set of 50 document templates. In each of these templates
there are various images (pictures), for example a product logo, company logo
etc. There may be as many as 10 of these images in each template, and nearly
all are common to all templates. To make things easier we inserted these
images as linked and embedded objects with the intention that we could just
update the source image file (being linked to) and the new image would appear
in the template(s). This is a classic situation where a company chnages its
logo every few years. In theory this should work, but there are problems.
The problem has arisen that some of the images have been used in Headers and
Footers, and these Heasders and Footers have been included in the template's
Building Blocks (and Quick Lists). We did this because some headers we need
to replace on the fly, for example using a macro can change a Confidential
Document header to an Internal Document header and thereby void having even
more temples to cover each of these nuances. Now when we change the source
file there are problems. Firstly the Building Block does not update with the
new image, even though the Links dialog in the template shows there is a link
to the image file. If I try to edit the link to point to a different image
file (in the Links dialog) then Word crashes (Office 2007).
As a last resort I want to try and edit the image file paths and file names
programatically - that means using different image files, but maybe it will
work. So that is why I want to access the path and file names and replace
them. Any suggestions would be welcome.
thanks
Robin
.