PDA

View Full Version : Help resizing an image



andysjp
10-17-2017, 04:23 AM
Hi folks

I am trying to write a macro that resizes an image contained within a textbox.

We have a system at work that generates these documents so I have no control over how this image starts off.

I would like the image at the top of page one to fill the entire page 1 precisely - so basically A4 size - 297mm high by 21mm wide. It doesn't matter if it's inside a textbox or not.

The textbox will always be called Text Box 2.

Please can anybody help?

Thanks
Andy

macropod
10-17-2017, 01:49 PM
Try:

Sub Demo()
Dim Shp As Shape
With ActiveDocument
.Range.InsertBefore vbCr
With .Shapes("Text Box 2")
.TextFrame.TextRange.InlineShapes(1).Range.Cut
.Delete
End With
.Characters.First.Paste
Set Shp = .InlineShapes(1).ConvertToShape
With Shp
.LockAspectRatio = True
.Width = CentimetersToPoints(21)
End With
End With
End Sub
Do note that doing what you've asked for changes the associated Section break from 'next page' to 'continuous. It also gets pushed to the next page. In any event, it's not clear what you want to do with that Section break.

andysjp
10-18-2017, 01:09 AM
Thank you VERY much!!!

andysjp
10-20-2017, 07:49 AM
Paul, this works flawlessly on our Windows 10 laptops but we still have some users on Win 7. The macro needed some slight tweaking to get it to run without syntax errors on theirs, but I managed to do that. However, the ConvertToShape line seems to destroy the image quality on the Windows 7 machines. It keeps it high quality on Win 10 though! Any idea how your macro could be modified to work on both Win 10 and Win 7 and keep good image quality on both?

macropod
10-20-2017, 12:50 PM
this works flawlessly on our Windows 10 laptops but we still have some users on Win 7. The macro needed some slight tweaking to get it to run without syntax errors on theirs
The Windows version has nothing to do with the code, so that's not the issue. Furthermore, the code would be the same for all Word versions from 2000 onwards. Evidently, your Win 7 users are using a different version of the document as well, with a lower-quality version of the image, which would explain:

the ConvertToShape line seems to destroy the image quality on the Windows 7 machines

andysjp
10-30-2017, 04:09 AM
Hi Paul

Thanks for the reply, and apologies for the delay replying, I'm just back from a holiday.

Whilst I have no doubt that in theory what you say is correct, I've just borrowed a Windows 7 machine from our IT department as I wanted to check if the users were doing something wrong, but I can confirm that the image quality is definitely degraded compared to the Windows 10 machines. I tested it using the same document with the same image - I can send the output from both machines if you're interested in seeing it. I don't think it's the code though, possibly something to do with an older version of Word reducing image quality to save space maybe?

Thanks
Andy

macropod
10-30-2017, 04:20 AM
Well, I have no idea what 'older version of Word' you're referring to. The code works fine for me with Word 2010, however. You should check whether the problem machines have automatic image compression turned on. To maximise image quality, the 'Do no compress images in file' option (under File|Options|Advanced>Image Size and Quality:All New Documents) should be checked - before ever you open the file in your attachment.