FaizanRoshan
10-17-2015, 11:59 AM
I have inserts the selected image file into a merged range of cells. The images are automatically re-sized to fit into the cell (aspect ratio maintained). Everything works well, but I ran into a problem.
There are about many cells, I found out that the merged cells to contain the images vary in width and height throughout the cells, even on the same cells.
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 time, (it might take me one day).
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.
Essentially I need to maintain the aspect ratio of the image file, and fill the target cell(s) without exceeding their boundaries.
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
Any ideas on how to fix my dilemma?
There are about many cells, I found out that the merged cells to contain the images vary in width and height throughout the cells, even on the same cells.
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 time, (it might take me one day).
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.
Essentially I need to maintain the aspect ratio of the image file, and fill the target cell(s) without exceeding their boundaries.
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
Any ideas on how to fix my dilemma?