Publisher: Creating a Linked Picture from an Embedded Picture (Part 1 of 2)
A lot of the time, entries that I post here end up inspiring me so that I expand them into articles that end up on MSDN. This time, I'm doing things the other way around: the next two posts are going to be excerpts from an article I actually published about 8 months ago.
The other day a user emailed me, asking how he could 'pull out' an embedded picture from a publication; i.e., he wanted to save the embedded picture as a separate file. This is something that comes up, in email to me and the Publisher newsgroups, fairly regularly. And I've written about how to do it; it's not that tough. Problem is, I wrote about it as a (very minor) part of a 34-page article, which was itself the second in a series of three articles—aimed at commercial printers, no less. Given all that, the chances of him finding the relevant code sample by doing a Web search was pretty small.
So I'm posting the relevant portions here, in the hopes they'll be a little more 'visible'. These are excerpted from the thrillingly-titled Automating Commercial Printing Prepress Tasks in Publisher 2003, Part 2. This article really contains a lot of useful information about how to write code that manages the graphics you include in your publications, including code sample on how to:
· Identify embedded and linked pictures in the publication.
· Generate a list of all pictures that were modified or whose links are broken.
· Update linked pictures whose originals were modified.
· Convert linked pictures to embedded pictures.
· Save embedded pictures as linked pictures.
· Replace one picture with another picture.
· Replace an empty picture frame with a picture.
· Gather detailed information about a specific picture.
So if any of those tasks fit with what you need to do, check it out.
End of commercial. Back to the task: how do you turn an embedded picture back into its original, separate file?
The short answer is, unfortunately, that you can't. However, what you can do is create a new, linked picture file from the embedded picture. There's a number of reasons why you'd want to do this: Using linked pictures instead of embedded pictures not only decreases the file size of a publication but also provides a separate image file that can be edited if necessary, and then updated quickly in Publisher.
Just realize that the resulting linked picture may not be the same format or resolution as the original picture embedded in the publication. This is because Publisher converted the picture twice: once when it embedded the picture, and again when it saved the picture as a separate file. Also, you cannot save an embedded EPS picture as an EPS file.
The following example creates a linked picture from an embedded one. Currently in the Publisher object model, the SaveAsPicture method applies only to the Page object, not individual shapes on a page. The function below creates a publication of the exact same dimensions as the shape passed it. It then pastes the shape onto the publication's single page, and saves the page as a .jpg file. It passes the full file path of the saved JPG file back to the calling procedure.
Function CreatePictureAsFile(shpEmbeddedPicture As Shape, _
strFileName As String) As String
Dim pubDocument As Document
'Create the new publication
Set pubDocument = NewDocument
'Set the page dimensions to equal
'the picture dimensions
.PageHeight = shpEmbeddedPicture.Height
.PageWidth = shpEmbeddedPicture.Width
'Paste the picture into the new publication
'Position the picture correctly
.Top = 0
.Left = 0
'Passes file path of picture back to calling procedure
CreatePictureAsFile = strFileName & "_" & _
shpEmbeddedPicture.Name & ".jpg"
'Saves a picture at the specified file path
'Closes Publisher instance without
'saving publication created