Consulting

Results 1 to 7 of 7

Thread: Help resizing an image

  1. #1
    VBAX Regular
    Joined
    Mar 2017
    Posts
    10
    Location

    Help resizing an image

    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
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Mar 2017
    Posts
    10
    Location
    Thank you VERY much!!!

  4. #4
    VBAX Regular
    Joined
    Mar 2017
    Posts
    10
    Location
    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?

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by andysjp View Post
    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:
    Quote Originally Posted by andysjp View Post
    the ConvertToShape line seems to destroy the image quality on the Windows 7 machines
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Regular
    Joined
    Mar 2017
    Posts
    10
    Location
    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

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •