“The linked image
cannot be displayed, the file may have been moved, renamed or deleted. Verify
that the link points to the correct file or location” – Microsoft Word 2010.
Problem Statement:-
Recently I have migrated our word document automation from
Word 2007 to Word 2010. I have found that image below error message in place of
image.
In our automation I just insert the html file into Word
Document by using the below code.
this.wordDocument.Application.Selection.InsertFile(strpath, ref this.missingValue,
ref this.objFalse, ref this.objFalse, ref this.objFalse);
Which works fine in Word 2007, but not in Word 2010. In the
html file we have absolute path to image and after html file inserted into Word
document, I delete the original file. Means word remove the links to external
file and saves the image in document file itself as it does for insert of
normal image file(like *.bmp, *.JPEG,…etc) below.
InlineShape picture = this.wordDocument.Application.Selection.InlineShapes.AddPicture(
path, ref this.objTrue, ref this.objTrue, ref this.missingValue);
Here is the change in Word 2010, which retains the links to
external image instead of saving them into document as it does for adding of
inline normal image. (One of the reason by guess why Word 2010 does retain
the original image file link could be Word not able to confirm as image file of
extension because our file doesn’t have
proper image extension like *.bmp, *.JPEG…etc). Because have deleted the
original image file after insertion into Word document…it couldn’t able to find
the file. So above message will be displayed.
Solution:-
After spend almost two to three days goggling and doing the trial
and error, finally contacted the Microsoft support. Support team really very
good with technical knowledge about the product.
After careful examination in the macro editor about how many
external links the document holds currently and found that it has all the
external file as inserted.
Same can be viewed in File->Info->Related documents
->Edit links to file in the lower
right corner in below.
Click the Edit links to file will open up below dialog,
which will have all the external file links and type of file and options for
document links…etc.
we have found that still it’s pointing to links which got
deleted it. So we decided to delete the links yourself automatically in our
automation before deleting the original file and works perfectly in both Word
2007 and Word 2010.
Below is the code which remove the external file links
automatically in c#
if (this.wordDocument.Application.Selection != null)
{
if
(this.wordDocument.Application.ActiveDocument.InlineShapes.Count > 0)
{
foreach (InlineShape inlineShape in
this.wordDocument.Application.ActiveDocument.InlineShapes)
{
if (inlineShape.LinkFormat != null &&
!inlineShape.LinkFormat.SavePictureWithDocument)
{
inlineShape.LinkFormat.SavePictureWithDocument = true;
inlineShape.LinkFormat.BreakLink();
}
}
}
}
Before finally saving the document just remove the links
which are “SavePictureWithDocument” flag false. Means which just image file
inside the html document not as just inline image, anyway which is working fine
and Word intelligently add them as “SavePictureWithDocument” flag set in the “options
for documents links” options.
Microsoft support team really helped us their timely response
to release the product on time without any delay.
I thought of share the knowledge as it would save someone
two days of effort as have spend it already.
Please post the comments if anyone required any more information.
6 comments:
Thank you very much! You saved me at least a days work! I had to rework the code into a different language but that was easy enough.
Thank you for this post. It was very useful and time saving...:-)
The code that you posted is great. I used it and it worked as expected, Thank you,
This is for VBA. But it doesn't recover images they seem to be lost.
Option Explicit
Sub FixPicturesLink()
Dim oILS As InlineShape
Dim oSHP As Shape
Dim objInlineShape As InlineShape
For Each objInlineShape In ActiveDocument.InlineShapes
With objInlineShape
If .Type = wdInlineShapeLinkedPicture Then
If Not .LinkFormat Is Nothing Then
.LinkFormat.SavePictureWithDocument = True
.LinkFormat.BreakLink
End If
End If
End With
Next
End Sub
Great post! thanks!
For if it helps someone, I was using this code on a webservice, and this solution didnt work for me until I checked, on IIS, folder contained images, with anonymous authentication
Your are excellent!!!
Post a Comment