Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

Macro To Set Image Links

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Berg - 12 Sep 2006 02:43 GMT
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?
Signature

Bob Berg

Jezebel - 12 Sep 2006 02:58 GMT
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).

>I import documents from my Genealogy program that require the image links
>be
[quoted text clipped - 3 lines]
> resetting links may not support macro recording??? If so how could this be
> done?
Bob Berg - 12 Sep 2006 03:19 GMT
I must be missing something, I get message:
Compile error.
"Can't assign to read only property" with "source path =" highlighted in
debugger.
Signature

Bob Berg

> Iterate the Shapes collection and set the links directly --
>
[quoted text clipped - 16 lines]
> > resetting links may not support macro recording??? If so how could this be
> > done?
Jezebel - 12 Sep 2006 04:12 GMT
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

>I must be missing something, I get message:
> Compile error.
[quoted text clipped - 26 lines]
>> > be
>> > done?
Bob Berg - 12 Sep 2006 14:39 GMT
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.
Signature

Bob Berg

> Sorry, my mistake. You have to set the SourceFullName property. If all the
> files are linking to a new folder, use
[quoted text clipped - 37 lines]
> >> > be
> >> > done?
Jean-Guy Marcil - 12 Sep 2006 22:05 GMT
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
[quoted text clipped - 10 lines]
>> pShape.LinkFormat.SourceName
>> Next

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?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Jezebel - 12 Sep 2006 22:42 GMT
Are you images floating or inline? If they are inline, replace
"ActiveDocument.Shapes" with "ActiveDocument.InlineShapes"

> 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
[quoted text clipped - 47 lines]
>> >> > be
>> >> > done?
Bob Berg - 12 Sep 2006 23:09 GMT
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
Signature

Bob Berg

> Are you images floating or inline? If they are inline, replace
> "ActiveDocument.Shapes" with "ActiveDocument.InlineShapes"
[quoted text clipped - 50 lines]
> >> >> > be
> >> >> > done?
Jean-Guy Marcil - 13 Sep 2006 01:20 GMT
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! :-)

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Bob Berg - 13 Sep 2006 10:25 GMT
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
Signature

Bob Berg

> Bob Berg was telling us:
> Bob Berg nous racontait que :
[quoted text clipped - 22 lines]
> Otherwise you are telling Word to look for a Shape in the InlineShape
> collection, and there aren't any! :-)
Jean-Guy Marcil - 13 Sep 2006 12:07 GMT
Bob Berg was telling us:
Bob Berg nous racontait que :

> I am now trying:
>
[quoted text clipped - 10 lines]
>
> 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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.