PDA

View Full Version : [SOLVED:] Converting images to JPEG



malik641
11-05-2005, 11:06 AM
Is there a way to go through an entire word document, find every image (all are in .BMP format) and convert the images to JPEG to reduce the file size using VBA???

Or would it just be easier to do this manually???

TIA

fumei
11-05-2005, 03:05 PM
How exactly were you going to do the conversion?

Killian
11-05-2005, 03:34 PM
AFAIK this isn't something that can be done in Word directly.
Certainly Word isn't able to re-save the images as jpeg's - the most you can do natively is to use the "Compress" feature in the Format Picture dialog but the methods and compression dialog aren't available in the object model, so short of some nasty SendKeys code, I can't see a way of doing it.
I recently offered a solution for previewing pictures on a user form (here (http://www.vbaexpress.com/forum/showthread.php?t=5758) ) by copying them into PowerPoint and exporting them from there (where you can use jpg or png formats) but it would be quite convoluted to do that then replace each pic and I doubt you'd get the quality you'd want for document pictures

TonyJollans
11-05-2005, 05:33 PM
I'm not sure about this one - I haven't got a proper grip on Word picture formats.

You can Display the Compress Picture Dialog from VBA but I don't know how to go on and run the compress.

What you might be able to do is copy and paste special as jpeg, something like this ...


For Each shp In ActiveDocument.InlineShapes
shp.Range.Copy
shp.Range.PasteSpecial Link:=False, DataType:=15, Placement:=wdInLine, _
DisplayAsIcon:=False
Next

TonyJollans
11-05-2005, 06:00 PM
Sorry, that should of Cut, not Copy.

malik641
11-05-2005, 07:25 PM
I was going to do it through paint or photoshop or imageready if Word didn't have a method for it.


As for TonyJollans' code: I tried it with the "Cut" instead of "Copy" and I came up with an error that the shape had been deleted :dunno

I just used the "Copy" and deleted the word art and kept the shapes...My 10MB file is now 1MB...AWESOME :thumb

Thanks guys!!

Ken Puls
11-05-2005, 07:56 PM
Very cool, Tony!

Out of curiosity, though, doesn't the .copy .pastespecial make sense? You connect to it, copy it, and then overwrite it with the jpg version of itself, no?

malik641
11-05-2005, 09:45 PM
Not in the code that I got...it just copied and placed it above the old bitmap image...:dunno That would've been nice if it replaced it...I just went and manually deleted the other images, though...no biggie :thumb

Ken Puls
11-05-2005, 10:36 PM
Manually? :bug:

:rotlaugh:

What about this?

Dim shp As InlineShape
For Each shp In ActiveDocument.InlineShapes
shp.Range.Copy
shp.Range.PasteSpecial Link:=False, DataType:=15, Placement:=wdInLine, _
DisplayAsIcon:=False
shp.Delete
Next

Seemed to work for me, although it did convert every picture to just a plain black field. Is the shp supposed to be inlineshape or something else?

TonyJollans
11-06-2005, 02:46 AM
I recorded the process of doing it manually (where the picure did get replaced). and got ..


Selection.Copy
Selection.PasteSpecial etc.

and just changed it to a Range to put it in a loop. It was only after I posted that I saw that I had duplicate images in my document and posted back to say use Cut, which worked for me. Clearly there's more to this than meets the eye.

Ken Puls
11-06-2005, 11:13 AM
Clearly there's more to this than meets the eye.

Agreed. I tried using the selection.copy and selection.pastespecial just now. Like Joseph, I also was left with two images. Very strange.

What's even weirder is that on my machine, it always replaces it with a solid black image. :dunno

TonyJollans
11-06-2005, 01:27 PM
Apologies - I'm being careless. Firstly, it looks like I was mistaken about it replacing the image. Secondly the Cut, of course, removes the shape so you can't paste into its range.

How to fix it? Ken's ought to work, or another way ..


Dim shp As InlineShape
Dim rng As Range
For Each shp In ActiveDocument.InlineShapes
Set rng = shp.Range
shp.Range.Cut
rng.PasteSpecial Link:=False, DataType:=15, Placement:=wdInLine, DisplayAsIcon:=False
Next


Lastly the black hole. I don't know, unfortunately. Maybe there's an option controlling it but I can't reproduce it at the moment.

Hpw the shape pastes depends on your version of Word (97 pastes as shape, 2000 pastes as inlineshape, 2002/3 has an option under Tools > Options > Edit).

malik641
11-07-2005, 05:47 AM
Thanks Tony and Ken for your efforts so far :thumb

The code that I performed (before Ken's and Tony's additional codes) I did at my home with MS Office 2003. Here at work I have MS Office 2000 and I'm getting error for both codes :dunno

With Ken's I get a complile error that says "Method or data member not found" and it highlights the following red text:


Dim shp As Variable
For Each shp In ActiveDocument.InlineShapes
shp.Range.Copy
shp.Range.PasteSpecial Link:=False, DataType:=15, Placement:=wdInLine, _
DisplayAsIcon:=False
Next

And with Tony's it deletes the first shape, then gives me a Run-Time error '5342' that says "The specified data type is unavailable and it highlights the following red text:


Dim shp As InlineShape
Dim rng As Range
For Each shp In ActiveDocument.InlineShapes
Set rng = shp.Range
shp.Range.Cut
rng.PasteSpecial Link:=False, DataType:=15, Placement:=wdInLine, DisplayAsIcon:=False
Next

I'm pretty familiar with VBA using Excel, but I just started using it in word...not sure what's going wrong here :dunno

Again thanks for everything so far :thumb

TonyJollans
11-07-2005, 07:42 AM
Hi Joseph,

Dim shp As Variable

is probably the cause of problem #1

The second is trickier - Paste as JPEG isn't an option in 2000.

As I think I said earlier I haven't got a good handle on exactly how Word holds pictures but if it's not possible to paste as a jpeg it's probably because Word 2000 can't hold it in that format. You might find it worth experimenting with the formats that are available.

malik641
11-08-2005, 06:18 AM
Thanks for the info Tony :thumb, I appreciate it. And to everyone else thanks for your help as well.

...Still trying to get a handle on VBA in Word :think: , but I'm sure I'll get the hang of it from all your help :thumb

Again thanks!

fumei
04-24-2013, 09:33 PM
Huh? I have no idea what you are saying. Try writing with some clarity.

"some in front"

Say what?