Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compress a Word doc through programming

Status
Not open for further replies.

johnny76

Programmer
Feb 14, 2006
24
CA
Hi,

My problem is that I have these Word documents that are too large in size, upwards of a MB. These documents have images in them. I can manually go in an right click an image inside the document click Format Picture then click Compress... then select all pictures in the document and save the doc. This will cut the doc down to around 100kb which is what I need.

What I would like to find out if I can do is do this sort of compress prgramatically...does anyone have any ideas for my problem?

Please let me know if I am not being clear enough..

I am open to any suggestion for my problem. The key is the doc file needs to be made smaller and stay in doc format. Unfortunately I cannot convert these files to PDF which would cut the size down as well.

CES
 
It sounds like what he wants is to automate compressing the photos inside a set of .DOC files rather than doing them all manually.

Lee
 
Zipping the entire document would seem more simple than each image inside the document.

Are we talking "loss-less reversible" compression or "lossy irreversible" compression?

I mean to ask if you just want to pack the bytes tighter and then inflate them later or do you want to actually DISCARD a portion of the data as is done with JPEG compression?
 
From this quote

The key is the doc file needs to be made smaller and stay in doc format.

I'd say that the zip format isn't what he's looking for. I'd guess that what Word does when you compress an image is save it resized rather than the original size and then resizing it on the fly when the document is viewed.

Lee
 
Sorry...

Thank you for the replies...

I cannot change the format of the file, it must stay in doc format. If you have an image in a word doc and right click it, you can select Format Picture and then click Compress....then All pictures in document. This shrinks the doc size. Picture still looks the same, everything looks the same but I can do this to a doc file with pictures that is 1MB and it will shrink to around 100KB. I need to figure out a way to do this through programming.

Does that make sense?

Any suggestions?

CES
 
I made a little word macro to set the height of every image in a word doc... but I think it just squished the display rather than compress the data.

Anyway, I'll post the code because it is at least a starting place:[tt]
Dim shapes As InlineShapes
Dim shape As InlineShape

Set shapes = Me.InlineShapes

For Each shape In shapes
shape.Height = 100
Next

Set shape = Nothing
Set shapes = Nothing
[/tt]

Pushing F2 brings up Object Browser in the word macro editor ... this will allow you to see the properties and methods of the shape and shapes objects.
 
Thanks for the suggestion Sheco! I will look a little further into this but so far I can't seem to get this to work with compressing images.

Thanks for the replies.

CES
 
The only way I would see you being able to do this programmatically is if you used the MS Word component from your script to do so:

For example:
Code:
Set objWord = GetObject(, "Word.Application")
   ' Use Word methods to insert some sample text
   objWord.Selection.TypeText "This is from VBScript!"
(Excerpt from MS Support Page)

It's my assumption that you could do something similar to this from an ASP script (the above example is strictly VBScript) but I don't know if you could get handles on the images in the document or would have the capability to call some sort of compress function once you did.

You might have sme luck doing additional searching on "Word.Application" and "compress image" or similar keywords.
The same functions available on the Application object in VBA should be available with VBScript on the Word.Application object, so if you could find a way to programmatically compress the images in VBA you should be able to make some minor modfications to make it work from VBScript.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top