Picture Links in Word 2007

R

Ron Porter

How do I programmatically access the source file for linked pictures? If
it makes a difference, the file is actually from Word 2003, but I'm
using Word 2007 set up to save as 2003 by default.

I have a document with about 130 pictures in it. The pictures were
inserted using Insert Picture and they were inserted as links. While
this certainly makes it easy to maintain as better photos become
available, it does make it tough to move around. I could probably have
dealt with this single document by now, but I have about 30 others
waiting in the wings and there will eventually be nearly 200 such
documents.

My usual approach to this kind of thing is to turn on the macro recorder
while I do some of the work manually. That usually gives me everything I
need to get started. In this case, the macro recorder doesn't
process the Manage Links dialog (ALT+e,k), so I don't know which part of
the object model to use.

Thanks.
--
-
Ron Porter
Programmer/Analyst
Crestline Coach Ltd
www.crestlinecoach.com
 
M

macropod

Hi Ron,

Try something based on:
Sub GetFieldLinks()
' This routine sets the new path for external links, pointing them to the current folder.
Dim oField As Field
Dim LinkPath As String
Dim LinkFile As String
Dim LinkFilePath As String
' Set the new path
' Go through all story ranges in the document, including shapes,
' headers & footers.
For Each oRange In ActiveDocument.Fields
With oField
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
LinkPath = .LinkFormat.SourcePath
LinkFile = .LinkFormat.SourceName
LinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
End If
End With
Next oField
End Sub

You mention issues with moving the files around. Perhaps one of the apparoaches here:
http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=261488
and/or here:
http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=670027
would help.
 
J

Jay Freedman

Hi macropod,

That would work in Word 2003 and earlier, but Microsoft broke it in Word
2007. Linked pictures are no longer represented by fields. The result of
this is that Shift+F9 and Alt+F9 in the UI won't show you field codes for
the pictures, and VBA doesn't find any fields -- they're just Shape or
InlineShape objects.

You'd have to modify the code to something like this:

Sub GetFieldLinks()
' This routine sets the new path for external links, pointing them to the
current folder.
Dim oShp As Shape
Dim oILShp As InlineShape
Dim LinkPath As String
Dim LinkFile As String
Dim LinkFilePath As String
' Set the new path
' Go through all story ranges in the document, including shapes,
' headers & footers.
For Each oShp In ActiveDocument.Shapes
With oShp
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
LinkPath = .LinkFormat.SourcePath
LinkFile = .LinkFormat.SourceName
LinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
End If
End With
Next oShp

For Each oILShp In ActiveDocument.InlineShapes
With oILShp
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
LinkPath = .LinkFormat.SourcePath
LinkFile = .LinkFormat.SourceName
LinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
End If
End With
Next oILShp
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
M

macropod

Hi Jay,

For the OP's requirements, the code I posted should work - the doc originated with Word 2003. AFAIK, Word 2007 doesn't change the
fields in docs opened/converted from previous versions and even saves as field codes the links for any files saved in the '97-2003
format.
 
J

Jay Freedman

Hi Jay,

For the OP's requirements, the code I posted should work - the doc originated with Word 2003. AFAIK, Word 2007 doesn't change the
fields in docs opened/converted from previous versions and even saves as field codes the links for any files saved in the '97-2003
format.

Ah, another factoid to file away. Thanks!
 
R

Ron Porter

Thanks to you both! It'll be a few days before I can get back to to this
problem, but I'm quite sure that this is enough to get me going.

macropod is correct in that the file I'm currently working with
originated and is currently being retained as Word 2003. But Jay's code
will prove useful as we move forward with our migration to Word 2007.
--ronp

Jay said:
Hi macropod,

That would work in Word 2003 and earlier, but Microsoft broke it in Word
2007. Linked pictures are no longer represented by fields. The result of
this is that Shift+F9 and Alt+F9 in the UI won't show you field codes for
the pictures, and VBA doesn't find any fields -- they're just Shape or
InlineShape objects.

You'd have to modify the code to something like this:

Sub GetFieldLinks()
' This routine sets the new path for external links, pointing them to the
current folder.
Dim oShp As Shape
Dim oILShp As InlineShape
Dim LinkPath As String
Dim LinkFile As String
Dim LinkFilePath As String
' Set the new path
' Go through all story ranges in the document, including shapes,
' headers & footers.
For Each oShp In ActiveDocument.Shapes
With oShp
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
LinkPath = .LinkFormat.SourcePath
LinkFile = .LinkFormat.SourceName
LinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
End If
End With
Next oShp

For Each oILShp In ActiveDocument.InlineShapes
With oILShp
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
LinkPath = .LinkFormat.SourcePath
LinkFile = .LinkFormat.SourceName
LinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
End If
End With
Next oILShp
End Sub

--
-
Ron Porter
Programmer/Analyst
Crestline Coach Ltd
www.crestlinecoach.com
 
R

Ron Porter

Jay, your code turned out to be the one that sort of worked, even though
the original document format is 2003. I say 'sort of worked', because
there are a couple of problems.

When I run the code
.LinkFormat.SourceFullName = newLinkPath & "\" & currentLinkFile
all the pictures show up nicely, but when I save (or save as) and
reopen, the pictures are 'gone' again. If I then re-run the updater, I
get an error telling me that oShp doesn't have a LinkFormat
property. Note that I can get the 'no property' error simply by
running the updater twice back to back.

Word also sometimes crashes when closing the document.

Here is my code (well your code with my changes):
---------start code------------
Sub UpdatePictureLinks()
'
' UpdatePictureLinks Macro
'
'
' This routine sets the new path for external links, pointing them to the current folder.
Dim oShp As Shape
Dim oILShp As InlineShape
Dim currentLinkPath As String
Dim currentLinkFile As String
Dim currentLinkFilePath As String

Dim newLinkPath As String
Dim newLinkFile As String
Dim newLinkFilePath As String

newLinkPath = InputBox("Please enter the new location of the photos.", "Update Photo Location", "P:\Crestline Document Library\Training\Production\Manuals\")
' Set the new path
' Go through all story ranges in the document, including shapes,
' headers & footers.
If newLinkPath <> "" Then
On Error Resume Next
For Each oShp In ActiveDocument.Shapes
With oShp
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
currentLinkPath = .LinkFormat.SourcePath
currentLinkFile = .LinkFormat.SourceName
currentLinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
.LinkFormat.SourceFullName = newLinkPath & "\" & currentLinkFile
End If
End With
Next oShp

For Each oILShp In ActiveDocument.InlineShapes
With oILShp
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
currentLinkPath = .LinkFormat.SourcePath
currentLinkFile = .LinkFormat.SourceName
currentLinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
.LinkFormat.SourceFullName = newLinkPath & "\" & currentLinkFile
End If
End With
Next oILShp
End If
End Sub
-----------end code---------

Jay said:
Hi macropod,

That would work in Word 2003 and earlier, but Microsoft broke it in Word
2007. Linked pictures are no longer represented by fields. The result of
this is that Shift+F9 and Alt+F9 in the UI won't show you field codes for
the pictures, and VBA doesn't find any fields -- they're just Shape or
InlineShape objects.

You'd have to modify the code to something like this:

Sub GetFieldLinks()
' This routine sets the new path for external links, pointing them to the
current folder.
Dim oShp As Shape
Dim oILShp As InlineShape
Dim LinkPath As String
Dim LinkFile As String
Dim LinkFilePath As String
' Set the new path
' Go through all story ranges in the document, including shapes,
' headers & footers.
For Each oShp In ActiveDocument.Shapes
With oShp
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
LinkPath = .LinkFormat.SourcePath
LinkFile = .LinkFormat.SourceName
LinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
End If
End With
Next oShp

For Each oILShp In ActiveDocument.InlineShapes
With oILShp
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the path
LinkPath = .LinkFormat.SourcePath
LinkFile = .LinkFormat.SourceName
LinkFilePath = .LinkFormat.SourceFullName
'Now do whatever you want to with the returned values
End If
End With
Next oILShp
End Sub

--
-
Ron Porter
Programmer/Analyst
Crestline Coach Ltd
www.crestlinecoach.com
 

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