Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Feb 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Need to size picture after pasted in Word Table

    I have code below that works great for selecting pictures from file directory and adding to the first column of a table in Word.

    But I need to fix the height of the picture so it doesn't change the table rows. I need the height to be 3.5" and the width of the picture = lock aspect ratio. I'm pretty sure that I need to convert the picture to an object to do this, but don't want to mess up the insert that already works.

    Thanks in advance!

    Code:
    Sub InsertPic()
    
        Dim i As Integer
        Dim picCount As Integer
        Dim fd As FileDialog
        Dim oTbl As Table
        Dim myPic As InlineShape
    
    
    
    ActiveDocument.Tables(1).Select
        picCount = 1
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
           With fd
               .Title = "Select image files and click OK"
               .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
               .FilterIndex = 2
               If .Show = -1 Then
                   Set oTbl = Selection.Tables(1)
                   For i = 1 To .SelectedItems.Count
                          ActiveDocument.InlineShapes.AddPicture _
                       FileName:=.SelectedItems(i), LinkToFile:=False, _
                       SaveWithDocument:=True, Range:=oTbl.Rows(picCount).Cells(1).Range
                       
                       picCount = picCount + 1
                   Next
               End If
           End With
           
           Set fd = Nothing
    
    End Sub

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,773
    Thanks
    0
    Thanked 162 Times in 150 Posts
    Change:
    Code:
                       ActiveDocument.InlineShapes.AddPicture _
                       FileName:=.SelectedItems(i), LinkToFile:=False, _
                       SaveWithDocument:=True, Range:=oTbl.Rows(picCount).Cells(1).Range
    to:
    Code:
                       Set myPic = ActiveDocument.InlineShapes.AddPicture( _
                       FileName:=.SelectedItems(i), LinkToFile:=False, _
                       SaveWithDocument:=True, Range:=oTbl.Rows(picCount).Cells(1).Range)
                       With myPic
                           .LockAspectRatio = True
                           .Height = InchesToPoints(3.5)
                       End With
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #3
    New Lounger
    Join Date
    Feb 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This works and sets the height - but for some reason, it is not locking the aspect ratio. With width of the picture is maximizing to fit the width of the cell - rather than resizing to fit the height - any thoughts????

  5. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,773
    Thanks
    0
    Thanked 162 Times in 150 Posts
    The code as posted works fine for me. An alternative approach is:
    Code:
    Dim sWdth As Single, sHght As Single
    ...
                       Set myPic = ActiveDocument.InlineShapes.AddPicture( _
                       FileName:=.SelectedItems(i), LinkToFile:=False, _
                       SaveWithDocument:=True, Range:=oTbl.Rows(picCount).Cells(1).Range)
                       With myPic
                          sWdth = .Width
                          sHght = .Height
                           .Height = InchesToPoints(3.5)
                           .Width = sWdth * .Height / sHght
                       End With
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #5
    New Lounger
    Join Date
    Feb 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Solved

    This worked perfectly - many, many thanks!

Tags for this Thread

Posting Permissions

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