Macro To Set Image Links

B

Bob Berg

I import documents from my Genealogy program that require the image links be
reset (Edit>Links dialog). I have tried various ways of recording this dialog
to a macro but nothing is recorded. I appears that the dialog window for
resetting links may not support macro recording??? If so how could this be
done?
 
J

Jezebel

Iterate the Shapes collection and set the links directly --

Dim pShape as Word.Shape

For each pShape in ActiveDocument.Shapes
pShape.LinkFormat.SourcePath = "..."
Next

You'll need to be more sophisticated than this if a) not all your shapes are
linked images (in which case text they type, etc), or b) the path is
different for different graphics (in which case you'll need to look at the
SourceName and apply whatever logic you need).
 
B

Bob Berg

I must be missing something, I get message:
Compile error.
"Can't assign to read only property" with "source path =" highlighted in
debugger.
 
J

Jezebel

Sorry, my mistake. You have to set the SourceFullName property. If all the
files are linking to a new folder, use

Dim pShape as Word.Shape
Const pNewFolder = "C:\...\"

For each pShape in ActiveDocument.Shapes
pShape.LinkFormat.SourceFullName = pNewFolder &
pShape.LinkFormat.SourceName
Next
 
B

Bob Berg

Must still be doing something wrong - macro doesn't link anything. I have
tried a very simple two image document with images in same folder as doc but
links are still not reset.
 
J

Jean-Guy Marcil

Bob Berg was telling us:
Bob Berg nous racontait que :
Must still be doing something wrong - macro doesn't link anything. I
have tried a very simple two image document with images in same
folder as doc but links are still not reset.

I have just created a document with two linked images in it (Insert >
Picture... > From File), located at
"X:\Office 2003\Images\"

Then, I Cut/Pasted the images to
"X:\Office 2003\Test\"

Finally, I ran this code:

Dim pShape As Word.Shape
Const pNewFolder = "X:\Office 2003\Test\"

For Each pShape In ActiveDocument.Shapes
pShape.LinkFormat.SourceFullName = pNewFolder &
pShape.LinkFormat.SourceName
Next

Everything worked as expected.

So either there is something wrong with your document or your method.

Can you try just as I have done to make sure you are using the code
properly.
If it works, then there is something wrong with your approach. Try to
retrace your steps and make sure the images are indeed located where they
should be.

You write that the images aren't being linked. Is the code running without
error? Are the image inline or floating?

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

Jezebel

Are you images floating or inline? If they are inline, replace
"ActiveDocument.Shapes" with "ActiveDocument.InlineShapes"
 
B

Bob Berg

Images are inline so used:
Dim pShape As Word.Shape
Const pNewFolder = "c:\tmg6\reports\"
For Each pShape In ActiveDocument.InlineShapes
pShape.LinkFormat.SourceFullName = pNewFolder & pShape.LinkFormat.SourceName
Next

I verified folder for images and the images by name in the folder. It is the
same as the document is in. I can go to edit>link, select all the images,
update and that works just fine.

I use the following routine after the photos are linked and showing to
resize, it works just fine:

Sub ResizePhotos()
Dim PreferredHeight As Integer
Dim MaximWidth As Integer
PreferredHeight = InputBox("Enter height in points (72pts/in,
28pts/cm)", , 108)
MaximWidth = InputBox("Enter maximum allowable width in points", , 144)
For Each iShape In ActiveDocument.InlineShapes
With iShape
ImageWidth = .Width
ImageHeight = .Height
NewHeight = PreferredHeight
NewWidth = Round(NewHeight * ImageWidth / ImageHeight)
If NewWidth > MaximWidth Then
NewWidth = MaximWidth
NewHeight = Round(NewWidth * ImageHeight / ImageWidth)
End If
.Height = NewHeight
.Width = NewWidth
End With
Next
End Sub
 
J

Jean-Guy Marcil

Bob Berg was telling us:
Bob Berg nous racontait que :
Images are inline so used:
Dim pShape As Word.Shape
Const pNewFolder = "c:\tmg6\reports\"
For Each pShape In ActiveDocument.InlineShapes
pShape.LinkFormat.SourceFullName = pNewFolder &
pShape.LinkFormat.SourceName Next

I am guessing it did not work.. You never mention that fact in your reply.
By reading between the lines, I guess you still have a problem, it is more
helpful to those helping you when you are explicit... ;-)

In any case, I see that you changed the Shapes to InlineShape as suggested
by Jezebel, but you did not change the variable assignment, try also
changing:

Dim pShape As Word.Shape

for

Dim pShape As Word.InlineShape

Otherwise you are telling Word to look for a Shape in the InlineShape
collection, and there aren't any! :)

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

Bob Berg

I am now trying:

Sub TMGPhotoLink()
Dim pShape As Word.InlineShape
Const pNewFolder = "c:\tmg6\reports\"
For Each pShape In ActiveDocument.InlineShapes
pShape.LinkFormat.SourceFullName = pNewFolder & pShape.LinkFormat.SourceName

Next

End Sub

Still doesn't work, by the way using Word 2003

Bob Berg
 
J

Jean-Guy Marcil

Bob Berg was telling us:
Bob Berg nous racontait que :
I am now trying:

Sub TMGPhotoLink()
Dim pShape As Word.InlineShape
Const pNewFolder = "c:\tmg6\reports\"
For Each pShape In ActiveDocument.InlineShapes
pShape.LinkFormat.SourceFullName = pNewFolder &
pShape.LinkFormat.SourceName

Next

End Sub

Still doesn't work, by the way using Word 2003

I have just tried the code you posted by doing the following:

1) Created a blank document;
2) Added two linked inline images located at:
"X:\Office 2003\OriginalFolder\"
(To make sure they are inline, select an image, do SHIFT-F9
and you should see an INCLUDEPICTURE field instead of
the image, do F9 to update the image and hide the field code)
3) Moved the images to:
"X:\Office 2003\NewFolder\"
4) Updated the images in the document;
5) As expected, the images were replaced by a red "X" in a white rectangle;
6) Ran this code:

Sub TMGPhotoLink()

Dim pShape As Word.InlineShape

Const pNewFolder = "X:\Office 2003\NewFolder\"

For Each pShape In ActiveDocument.InlineShapes
pShape.LinkFormat.SourceFullName = pNewFolder & _
pShape.LinkFormat.SourceName
Next

End Sub

7) The images were automatically updated;
8) Just to be sure, I selected the images and hit SHIFT-F9 to see the field
code;
9) The new path was there.

You only stated "Still doesn't work". This doesn't give us much to go on.
You have to be more explicit.
Is the macro running seemingly OK, but images are still represented by
red "X"'s?
Do you get an error message?

I would suggest you try the 9 steps above with a brand new document.
If it works, then compare those 9 steps with you have done with your current
document. What is the difference?
If it does not, post back describing exactly what you have done and the
on-screen results.

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

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