PDA

View Full Version : Embedded photos



mdmackillop
02-18-2011, 05:40 AM
I'm trying to reduce Word Document file size due to embedded pictures (Word 2003). Thanks to Cindy Meister for the method, I save the file as htm and reload the smaller images. This all works, reducing from 65MB to 1MB for my test doc containing 46 photos. I need to check all is in order before deleting the original.

Problem: The code should reduce and scroll "before and after" side by side, but even with the built in delay (at end of OpenTwo), the photos are not all visible. Is there a method to wait for the document to be "Ready" before the scroll starts?




Option Explicit
Sub DoHTM()
Dim Fname As String
Dim Pth As String
Dim HtmPth As String
Dim Doc As String
Dim MyDoc As Document
Dim F As String
Dim i As Long

Application.ScreenUpdating = False

'Get data
Pth = ActiveDocument.Path & "\"
Fname = ActiveDocument.Name
Doc = ActiveDocument.FullName
Fname = Left(Fname, Len(Fname) - 4)

'Save Documernt as HTM
ChangeFileOpenDirectory Pth
ActiveDocument.SaveAs Filename:=Fname & ".htm", FileFormat:= _
wdFormatHTML
HtmPth = Pth & Fname & "_files\"

'Ensure folder is created
Do
DoEvents
Loop Until Dir(HtmPth & "*.*") <> ""

'Delete non jpg files
F = Dir(HtmPth & "*.*")
Do Until F = ""
If Right(F, 3) <> "jpg" Then Kill HtmPth & F
F = Dir
Loop

'Reopen original document
Set MyDoc = Documents.Open(Doc)
'Iterate through and relace pictures
F = Dir(HtmPth & "*.*")
Do Until F = ""
i = i + 1
MyDoc.InlineShapes(i).Select
Selection.InlineShapes.AddPicture Filename:= _
HtmPth & F, LinkToFile:=False, SaveWithDocument:=True
F = Dir
Loop
ActiveDocument.SaveAs Fname & "-01.doc"
Documents(Fname & ".htm").Close
Kill Pth & Fname & ".htm"
Application.ScreenUpdating = True
OpenTwo
End Sub

Sub OpenTwo()
Dim tim As Single

'Set window 1 size
Application.WindowState = wdWindowStateNormal
Application.Move Left:=100, Top:=75
Application.ReSize Width:=451, Height:=581
ActiveWindow.ActivePane.View.Zoom.Percentage = 25

'Set window 2 size
Documents.Open Filename:="""Schedule Photographs.doc"""
Application.WindowState = wdWindowStateNormal
Application.Move Left:=600, Top:=75
Application.ReSize Width:=451, Height:=581
ActiveWindow.ActivePane.View.Zoom.Percentage = 25

'Wait for images
Application.OnTime Now + TimeValue("00:00:15"), "DoScroll"
End Sub

Sub DoScroll()
Dim x As Single
Dim tim As Single

'Scroll speed factor
x = 0.025

'Ensure start from top
Windows(1).Activate
ActiveWindow.ActivePane.VerticalPercentScrolled = 0
Windows(2).Activate
ActiveWindow.ActivePane.VerticalPercentScrolled = 0
'Scroll
Do
Windows(1).SmallScroll Down:=1
Windows(2).SmallScroll Down:=1
tim = Timer
Do
DoEvents
Loop Until Timer - tim > x
If Windows(2).ActivePane.VerticalPercentScrolled > 85 Then Exit Sub
Loop
End Sub

Frosty
02-18-2011, 09:55 AM
I don't think there is such a method. Since Word only needs to paint whatever is in the viewing screen, it wouldn't necessarily make sense to provide a property that says "I'm ready to display everything in this entire document, even the stuff you haven't requested to look at yet"

I assume it's the 45 meg file that is slow to display all of the images?

And you're writing this routine to compare side by side whether the .htm save process didn't "lose" a picture or two? Or that the compression quality is good enough for an individual picture?

You've commented the code well, but I'm still wondering if this isn't a round peg in a square hole problem.

This is better than just manually using the compress all pictures function (which lets you choose the dpi) and then scrolling down?

Just a brainstorm as you clearly don't need the sample...

1. Come up with a longer amount of time to wait before starting your scroll down. Since Word isn't really designed to deal well with 45 meg files, it's really processor and ram/video ram dependent when that file becomes available on that machine.

2. Try going through all the shapes in the document and selecting each one them (which would trigger a repaint) until you get to your last one, and then starting at the top with your scroll down process. This will look ugly, but it may work. I'm not sure, but you may be able to have screenupdating off and still basically "slow down" word (a strange kind of doevents) with that.

3. Throw out the scrolling percentage, and instead use selection.movedown wdline or something along those lines. Since they are the same document, presumably, you should get the same "percentage" but just a different way. That might "jiggle" word's display a little bit and keep you in synch?

mdmackillop
02-18-2011, 11:08 AM
Thanks for your comments. I'll try some of your suggestions.
The built in Word Compress routine reduces the file size only to 30MB, hence this more convoluted method.

Frosty
02-18-2011, 11:13 AM
Aren't there options on different compression rates? There's one for web and one for printing at least, if I recall correctly. So you should at least get two different values-- unless by 30 meg you meant that was the smallest.

It does surprise me, as I would have thought saving as an .htm simply did the 96 dpi compression. Is it possible there's some other kind of bloat in the document besides the pictures (i.e., autotext entries which are lost when saved to the .htm)?

But I haven't dealt with this kind of issue, so I'm just sort of brainstorming with you. Hope it helps.

mdmackillop
02-18-2011, 01:19 PM
In 2003 when I save as htm it creates 2 files for each picture. A PNG which seems to be the same size as originally inserted picture eg 4MB, and a JPG of about 30-40kb, which appears the same dimensions as the document image. The code deletes the PNG files and reinserts the small JPGs.

I'm trying this at home in 2010, but of course everything is different! No PNGs and mutiple JPGs are created for each image.

The Work document only contains images and captions. Nothing "clever".

Frosty
02-18-2011, 02:15 PM
Yep, although the same basic problem may exist: the original file is simply too large to quickly show everything, so you'll have to figure out some way of waiting before you start your scrolling comparison.

As for the double images, have you checked this out?

http://support.microsoft.com/kb/224663/en-us

I found it to be helpful when dealing with .rtf formats in 2003, maybe you're ending up with the same thing?

A quick summary: you can add a regkey which tells word not to create duplicate versions of the images when you save to a different file format. I've never tested it on saving to .htm format, but it definitely made a big difference in .rtf format.

mdmackillop
02-19-2011, 05:10 AM
The double images are not an issue asd on completion all these will be deleted. My basic purpose is to recover hard disc space by reducing the size of archived files. I guess I can buid in more delays to allow images to appear, and once I have confidence in the basic routine, I can dispense with the visual checks.
Thanks for your assistance and suggestions.