PDA

View Full Version : Edit-Paste Special-As a Picture



Anne Troy
06-23-2004, 04:44 PM
I would absolutely LOVE to have the following TWO macros.

The first one will work on an individual graphic that is already selected.

The second will process ALL graphics in a Word document.

Here's what it should do:

Cut the graphic.
Edit-Paste Special, as a Picture.

If the picture style JPG is available, that should be chosen.
If it's NOT, then just "picture" should be the type.
If it's an Office drawing object, and not a graphic, then do nothing to it at all.

No message boxes required, unless you want to provide one at the end of the *document* one that tells the user how much electronic size they saved, and also saves the file to a new name since SOMETIMES graphics get ruined by this process, tho it's rare.

I have a COM addin for PPT that does this and can direct you to the person who created it if you need assistance with code. However, I don't think it distinguishes between the picture type...

Kelly
06-26-2004, 04:01 PM
Before I paste ANY of my code or say ANYTHING else, I must first say:

WARNING! WARNING!

My "solution" to this challenge of extracting images/pictures is FAR FAR FAR FAR FAR FAR from perfect.

Additionally, I have tested it (in a limited sense) on Word 2000 on a Windows XP machine. I have NO IDEA if it will work in other configurations.

ALSO, the following macro depends on being able to search the correct "Temp" folder that any particular version of Word is using. So I guess if someone's Temp folder is located in some highly unorthodox location, then that my pose even more problems.

For this macro to work, it ASSUMES THAT the document with the images is OPEN **AND** IS the active document!

Also, for reasons that I am embarrassed to say I don't fully understand, this macro seems to have a much greater chance of success if Word has just been opened immediately before using the macro, and the document in question has then immediately been opened without opening anything else or doing anything else. It sounds great already, doesn't it??? :rofl :dunno :help

Here goes the code, and then I will attempt to explain....


Option Explicit
Sub ExtractPictures()
Dim ToContinue 'ToContinue will be true if the user chooses to continue after the warning msg
Dim WhereIsTheTemp As String 'path to the temp folder where word is putting wmf images
Dim OldSettings1 As Boolean 'save users original settings for restoring them later
Dim OldSettings2 As Byte 'save users original settings for restoring them later
Dim NewImgFolder As String 'the folder where the extracted images will be saved
Dim DocToCloseAndOpen As String
Dim FoundIt As Boolean
Dim MyTotal As Integer
Dim FileToTest As String 'stores the names that come up from the Dir command looking for wmf's
Dim TimeBefore As Date 'I'm using date/time stamps to figure out which TEMP wmfs correspond to current doc
Dim TimeFileCreated As Date 'I'm using date/time stamps to figure out which TEMP wmfs correspond to current doc
Dim MyShape As InlineShape

ToContinue = MsgBox("Images are about to be extracted from :" & vbCr & vbCr _
& vbTab & ActiveDocument.Name & vbCr & vbCr _
& "In order to extract the images, " & ActiveDocument.Name _
& " will be" & vbCr & "automatically closed and then reopened." & vbCr _
& vbCr & "If you do not wish for this document to be closed, then" & vbCr _
& "stop the process of image extraction now by clicking Cancel.", vbOKCancel + vbExclamation + vbDefaultButton2)

Do While ToContinue = 1
MyTotal = 0
NewImgFolder = "C:\ImgsSpecial\"
On Error Resume Next
FileSystem.MkDir NewImgFolder 'Create the ImgsSpecial folder on the user's computer
On Error GoTo 0

WhereIsTheTemp = Options.DefaultFilePath(wdTempFilePath) & "\" 'Find out where_
'this version of Word is storing its TEMP information
OldSettings1 = Application.DisplayRecentFiles
OldSettings2 = RecentFiles.Maximum
Application.DisplayRecentFiles = False 'I seem have a higher chance of success_
'with this macro if I disable DisplayRecentFiles and then close_
'and reopen the document, but I truly don't know why

DocToCloseAndOpen = ActiveDocument.FullName
On Error GoTo AnError
ActiveDocument.Close 'If document does not close (for example if the user hits Cancel on_
On Error GoTo 0 'a message) then the macro terminates by going to the AnError error
'handler at the end of the macro

Documents.Open (DocToCloseAndOpen)
For Each MyShape In ActiveDocument.InlineShapes
If MyShape.Type = wdInlineShapePicture Or wdInlineShapeEmbeddedOLEObject Then
'In the above IF statement, I'm not sure whether it would be desireable to add_
'other possible shape types in addition to "ShapePicture" and "EmbeddedOLEObject"_
'those were simply the two options that worked for the word documents that i tested
TimeBefore = VBA.DateTime.Now
MyShape.Activate 'when we activate the picture, we HOPE word creates a NEW wmf in the TEMP folder
FoundIt = False
FileToTest = Dir(WhereIsTheTemp & "*.wmf")

Do 'begin searching the temp folder for wmf's created AFTER the date variable TimeBefore
TimeFileCreated = FileSystem.FileDateTime(WhereIsTheTemp & FileToTest)
If (TimeFileCreated - TimeBefore) >= 0 Then

'This is where all the "good stuff" happens if we are successful:
FileSystem.FileCopy WhereIsTheTemp & FileToTest, NewImgFolder & FileToTest
MyTotal = MyTotal + 1
FoundIt = True
Exit Do
End If
FileToTest = Dir()
Loop While FileToTest <> ""
ActiveDocument.Close 'closes the picture editing screen

End If
Next 'Go find more images
Application.DisplayRecentFiles = OldSettings1
RecentFiles.Maximum = OldSettings2
MsgBox MyTotal & " image files were extracted."
End
Loop 'I never intended for this loop to cycle. I used 'Do While ToContinue=1' instead of 'If'
End 'we arrive here if the user chooses Cancel on the first MsgBox_
'warning of the document closure
AnError:
MsgBox "Unable to close " & ActiveDocument.Name & ". Program cannot continue. Image Extractor failed."
Application.DisplayRecentFiles = OldSettings1
RecentFiles.Maximum = OldSettings2
End Sub

Kelly
06-26-2004, 04:10 PM
Okay, here goes an explanation...

Notes:
1. my macro is intended to process all images in a document. no image needs to be selected before running macro

2. my macro doesn't save the images as JPG files. It currently saves them as WMF files. However, perhaps someone who understands WMFs better than I can explain a few things to me, BECAUSE.... just to fool around, I changed the ".wmf" file extension/suffix thingy of a wmf file to ".jpg" without doing anthing else, and then I double clicked the file and it opened up beautifully as a JPG. Can it really be that simple with WMFs?? If so, then the macro could simply rename the files. But that seems suspiciously easy.......

3. My macro doesn't use the 'cut and paste' concept

4. My macro doesn't use any COM programming....


Okay, so what the heck does it do? Well, I discovered that if you open a word document with images, then when you select a picture in the document and go into that "picture edit" mode where the picture opens in a window of its own and you can mess with cropping, contrast, brightness, etc, then what Word is doing is CREATING A WMF FILE in the temp folder. so my macro finds the correct WMF file and copies it to "C:\ImgsSpecial"

The problem is that I don't know ENOUGH about Word's "internal rules" governing when and why it creates a WMF for a picture. So sometimes my macro fails because even though the picture has been activated for editing, for some reason no WMF file appears in the TEMP folder!!!

Hopefully someone can explain this phenomenon.

Kelly
06-26-2004, 04:16 PM
Oh!

IMPORTANT IMPORTANT
IMPORTANT IMPORTANT

I forgot to specify that this macro must be put in "NORMAL"

It CANNOT be put in the module of the document with the images, because it needs to CLOSE and REOPEN the document with the images.

And in case you are wondering WHY the heck I even need to close and then open the document, it has to do with the problem that I mentioned earlier.... namely: I don't fully understand WHEN and WHY Word sometimes does and sometimes doesn't create the wmf's in the Temp folder. But for whatever reason, I found I was much more likely to get the WMF files I was looking for when I "freshly" closed and opened the document before cycling throught the images.

Mulder
06-27-2004, 11:15 AM
I understand that macro perfectly, but Dreamboat's never going to get it!

:)

JackInTheUK
06-27-2004, 02:55 PM
Mulder


Hi buddy wonder if you can help bit, do you know why the open close of *.doc is such an issue here and why this is so Windows driven, Jacks kind of thing and i cant solve this, unless its a cache issue but then again what difference in the case is that to clipboard to cache?

Maybe you can let me know - as the dance with the docs seems a bit much to me and should be able to fix.

many thanks buddy, Jack works hard on how Excel works with Windows or not as is the case often so this is extremely interesting - well IMHO !

Kelly
06-27-2004, 03:09 PM
I can tell you why the opening and closing matters. (sort of)


the only source of problems with what my macro does is this:

if the "Temp" folder ALREADY has a WMF in it for one of the pictures in the document, then my loop that searches for NEWLY CREATED wmf files will not find any newly created files, because Word apparently checks for the correct WMF when you click to edit an image. If Word finds that it already has a WMF for that image, then it won't create a new one.

A case or scenario in which the Temp folder would have the "desired" WMF already (meaning PRIOR to the execution of the macro) is the case where the user has already opened the document and activated some of the images.

Is that comprehensible? I get tripped up explaining computer stuff sometimes, but I hope that's clear enough. It's one of those things where I "get it" inside my own head but find it tougher to put into words.

Kelly
06-27-2004, 03:16 PM
There would be a few ways to deal with this issue of "prior WMFs impeding the search for brand new WMFs"

One way would be to delete everything in the Temp folder at the start of the macro. However, the problem with this is that while Word is open, there will frequently be many files in Temp where you won't have "permission" to delete them.

One "work-around" for that would be to write a script in VBScript for example for the WSH that would make sure no Word application is running, then delete the Temp folder, then open Word and open the target document and run the macro.

The other "work-around" would be to find a different method of identifying the correct WMF. I am identifying the WMF by looking at the "time created."

does anyone know another way to cycle through the WMFs in the Temp folder and determine which document they correspond to?

I don't, and that's the problem. But there are many people here that are vastly more advanced and experienced than I am, so I'm sure a better solution can be found.

I only "dared to venture" my solution because this question had sat unanswered for a few days, so I figured what the heck. It's my two cents (or less depending on any given person's assessment of its value....)

Kelly
06-27-2004, 03:18 PM
slight clarification: where I said "delete the Temp folder" (above) I should have said "delete the CONTENTS of the Temp folder." I would not want to delete the folder itself

Mulder
06-27-2004, 04:39 PM
Mulder


Hi buddy wonder if you can help bit, do you know why the open close of *.doc is such an issue here and why this is so Windows driven, Jacks kind of thing and i cant solve this, unless its a cache issue but then again what difference in the case is that to clipboard to cache?

Maybe you can let me know - as the dance with the docs seems a bit much to me and should be able to fix.

many thanks buddy, Jack works hard on how Excel works with Windows or not as is the case often so this is extremely interesting - well IMHO !
Hey JackInTheBox,

I actually don't understand a word of that macro. I was just yanking Dreamboat's chain! :)

Although Kelly seems to be a whiz!