Insert image into Word Table cell without changing cell size

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to add a picture to a Word Table cell;

Code:
oDoc.Tables(1).Cell(1, 2).Range.InlineShapes.AddPicture "C:\Users\James\Desktop\Hampshire.bmp"

Can someone please show me how I can fit the image to the cell dimensions without changing the cell in any way?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Simply give the cell a fixed height and width!

For the:
• height, set the exact row height under Table Tools>Layout>Properties>Row>Specify Height>Exactly.
• width, set the preferred column width under Table Tools>Layout>Properties>Columns, and uncheck the 'automatically resize to fit contents' option under Table Tools>Layout>Properties>Table>Options.

You might also want to set the cell margins to 0 all round. It's also a good idea to format the table paragraph with 0 before/after spacing.
 
Upvote 0
Got it - thanks.....I've done that and it works fine and it deals with pictures that are too big for the cell, but what about when the image is too small to fill the cell, is there a way I can increase it to fill the cell but keeping the aspect ratio?
 
Upvote 0
Got it - thanks.....I've done that and it works fine and it deals with pictures that are too big for the cell, but what about when the image is too small to fill the cell, is there a way I can increase it to fill the cell but keeping the aspect ratio?
For that, you might use code along the lines of:
Code:
Sub ResizeTablePics(Tbl As Table)
Dim Tbl As Table, iShp As InlineShape
With Tbl.Range
  For Each iShp In .InlineShapes
    With iShp
      .LockAspectRatio = True
      With .Range.Cells(1).Range.ParagraphFormat
        .SpaceBefore = 0
        .SpaceAfter = 0
        .LeftIndent = 0
        .RightIndent = 0
        .FirstLineIndent = 0
      End With
      With .Range.Cells(1)
        .TopPadding = 0
        .BottomPadding = 0
        .LeftPadding = 0
        .RightPadding = 0
        If .HeightRule = wdRowHeightExactly Then
          If .Column.PreferredWidthType = wdPreferredWidthPoints Then
            If .Height <> iShp.Height Then
              If .Width <> iShp.Width Then
                If .Height > iShp.Height Then
                  iShp.Height = .Height
                  If .Width < iShp.Width Then
                    iShp.Width = .Width
                  End If
                End If
              End If
            End If
          End If
        End If
      End With
    End With
  Next
End With
End Sub
The above is probably overkill for what you want; it checks & formats all cells containing inlineshapes in the nominated table, optimising both the paragraph parameters and the cell parameters for the image fitting. Only cells that have a fixed height and width are processed - because there's nothing to 'lock' the image size to otherwise.
 
Last edited:
Upvote 0
Thanks Paul, very much appreciated - the other thing I've considered is how big the file will be after resizing the image so I've thought about compressing it - I've tried sendkeys but that just gives me a dialog box to manually click, is there a way to avoid that do you know?
 
Upvote 0
Unfortunately, Word 2007 & later provide no programmatic access to Word's image compression tools. That's because Word 2007 & later contain two command bars with the same ID - one being the new one, the other being the older Word 2003 & earlier one - but it’s only the older non-functioning one the SendKeys code activates.
 
Upvote 0
Ok, thanks Paul - I'm wondering if I can come up with something staying within Excel rather than putting it in a Word document so I'll have a play - thanks for your help.
 
Upvote 0
Paul,

I'm struggling to sort the resizing out, I'm getting 'User defined type not defined' when I try to run your resizing suggestion but don't know why?
 
Upvote 0

Forum statistics

Threads
1,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top