Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 10-17-2015, 12:33 PM
FaizanRoshan FaizanRoshan is offline Word - Resize image to specified selected cells' dimensions Windows 8 Word - Resize image to specified selected cells' dimensions Office 2010 64bit
Novice
Word - Resize image to specified selected cells' dimensions
 
Join Date: Oct 2015
Posts: 3
FaizanRoshan is on a distinguished road
Default Word - Resize image to specified selected cells' dimensions

What would be helpful is if there were a way to write the code so that the image is resized to the size of the cell (it is actually a merged set of cells), that it is being placed into.


I do not want to go through every one and figure out the width and height for each cell and type in the corresponding values for every cell, (it might take me one day).
Essentially I need to maintain the aspect ratio of the image file, and fill the target cell(s) without exceeding their boundaries.
Code:
Sub FitPics() 
    Application.ScreenUpdating = False 
    Dim Tbl As Table, iShp As InlineShape 
    With ActiveDocument 
        For Each Tbl In .Tables 
            For Each iShp In Tbl.Range.InlineShapes 
                With iShp 
                    .LockAspectRatio = msoTrue 
                    If .Height > .Range.Cells(1).Height Then 
                        .Height = .Range.Cells(1).Height 
                    End If 
                    If .Height < .Range.Cells(1).Height Then 
                        .Height = .Range.Cells(1).Height 
                         
                    End If 
                    If .Width > .Range.Cells(1).Width Then 
                        .Width = .Range.Cells(1).Width 
                    End If 
                    If .Width < .Range.Cells(1).Width Then 
                        .Width = .Range.Cells(1).Width 
                    End If 
                End With 
                 
            Next 
        Next 
    End With 
    Application.ScreenUpdating = True 
End Sub
Attached Images
File Type: png Sample.png (5.6 KB, 26 views)
  #2  
Old 10-17-2015, 03:12 PM
macropod's Avatar
macropod macropod is offline Word - Resize image to specified selected cells' dimensions Windows 7 64bit Word - Resize image to specified selected cells' dimensions Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

If your table has fixed cell widths and heights, any images you insert into it, other than by dragging or copying them from elsewhere in a document, will automatically resize to optimally fill the space available while retaining the correct aspect ratio.

As for "I do not want to go through every one and figure out the width and height for each cell and type in the corresponding values for every cell", to justify that on the basis of "it might take me one day" seems nothing more than a pretext. How else would you propose to do it?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
  #3  
Old 10-17-2015, 08:20 PM
macropod's Avatar
macropod macropod is offline Word - Resize image to specified selected cells' dimensions Windows 7 64bit Word - Resize image to specified selected cells' dimensions Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Cross-posted at: http://answers.microsoft.com/en-us/o...6-bc3837ccb721
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
  #4  
Old 10-18-2015, 03:56 AM
FaizanRoshan FaizanRoshan is offline Word - Resize image to specified selected cells' dimensions Windows 8 Word - Resize image to specified selected cells' dimensions Office 2010 64bit
Novice
Word - Resize image to specified selected cells' dimensions
 
Join Date: Oct 2015
Posts: 3
FaizanRoshan is on a distinguished road
Default

Meaning of figure of height and width that i don't want to change h & w in code for each cells as i mention that all the cells have different size. i actually need one macro that resize any picture with size of selected cell.
  #5  
Old 10-18-2015, 04:17 AM
macropod's Avatar
macropod macropod is offline Word - Resize image to specified selected cells' dimensions Windows 7 64bit Word - Resize image to specified selected cells' dimensions Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by FaizanRoshan View Post
i don't want to change h & w in code for each cells as i mention that all the cells have different size.
The code you've already posted doesn't require any foreknowledge of the cell height or width - for every table in the document. Have you tried running it?

Your code could be made more efficient, though:
Code:
Sub FitPics()
Application.ScreenUpdating = False
Dim Tbl As Table, iShp As InlineShape
With ActiveDocument
  For Each Tbl In .Tables
    For Each iShp In Tbl.Range.InlineShapes
      With iShp
        .LockAspectRatio = msoTrue
        .Height = .Range.Cells(1).Height
        If .Width > .Range.Cells(1).Width Then
          .Width = .Range.Cells(1).Width
        End If
      End With
    Next
  Next
End With
Application.ScreenUpdating = True
End Sub
Still, if you insert the pictures directly into the table cells via Insert|Picture, you don't even need a macro...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
  #6  
Old 10-18-2015, 05:21 AM
FaizanRoshan FaizanRoshan is offline Word - Resize image to specified selected cells' dimensions Windows 8 Word - Resize image to specified selected cells' dimensions Office 2010 64bit
Novice
Word - Resize image to specified selected cells' dimensions
 
Join Date: Oct 2015
Posts: 3
FaizanRoshan is on a distinguished road
Default

Its work good but no as i want, if original picture height > width then after running code its resize on width and crop height with picture size. or if width > height then code resize width only with cell size.
Attached Images
File Type: jpg Sample.jpg (34.4 KB, 27 views)
  #7  
Old 10-18-2015, 12:52 PM
macropod's Avatar
macropod macropod is offline Word - Resize image to specified selected cells' dimensions Windows 7 64bit Word - Resize image to specified selected cells' dimensions Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by FaizanRoshan View Post
Its work good but no as i want, if original picture height > width then after running code its resize on width and crop height with picture size. or if width > height then code resize width only with cell size.
Well, you did say in your first post:
Quote:
fill the target cell(s) without exceeding their boundaries
Unless the aspect ratio is the same as the cell, you cannot fill it in both directions without exceeding the boundaries. But, if that's what you want, change > to <.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
  #8  
Old 10-18-2015, 03:34 PM
macropod's Avatar
macropod macropod is offline Word - Resize image to specified selected cells' dimensions Windows 7 64bit Word - Resize image to specified selected cells' dimensions Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I see you've now had the temerity to cross-post my code on other sites (http://windowssecrets.com/forums/sho...height-exactly & http://www.vbaexpress.com/forum/show...-cells-in-word) without attribution and without observing the cross-posting etiquette.

THREAD CLOSED.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Closed Thread

Tags
ms word 2010, picture format, word vba macro

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word template that automatically resize's and formats image layout vikkitoria60 Word VBA 3 09-30-2015 01:16 PM
Insert and resize image lenguyenleduong Word VBA 2 05-11-2014 07:23 AM
Shortcut for image resize Hatthans PowerPoint 0 01-06-2014 04:32 PM
How to resize the image in the header so that it fits the page Isadora Excel 1 08-20-2013 06:02 AM
Image proportional resize not functioning! alexfcm PowerPoint 0 09-27-2012 07:22 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:21 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft