Consulting

Results 1 to 3 of 3

Thread: Inlineshape.width and .height only works when debugging

  1. #1

    Inlineshape.width and .height only works when debugging

    Hi,

    I'm inserting a picture in a Word table cell. Then I resize it using .Width and .Height. The strange thing is that it only works when I debug the code, using F8 to step through the commands.

    When I remove the break points and execute the code, the picture doesn't resize. It gets the same width as the table cell... This is the code I'm using:

    Dim oPic As InlineShape
    Set oPic = ActiveDocument.InlineShapes.AddPicture(plotLocation & "\" & fileName, False, True, CellRange)
    With oPic
          .LockAspectRatio = False
          .Width = CentimetersToPoints(15.5)
          .Height = CentimetersToPoints(10)
    End With
    Note: plotLocation, fileName, CellRange are defined earlier in the code.

    Any ideas?

    Best regards,
    David

  2. #2
    It works fine here - given that a cell width of approximately 6 inches is pretty large?

    Option Explicit
    Sub Macro1()
    Dim oPic As InlineShape
    Dim plotLocation As String
    Dim CellRange As Range
    Dim FileName As String
    
        plotLocation = "D:\My Documents\My Pictures"
        FileName = "Filename.jpg"
        Set CellRange = ActiveDocument.Tables(1).Cell(1, 1).Range
        CellRange.End = CellRange.End - 1 'Remove the cell end character.
        Set oPic = ActiveDocument.InlineShapes.AddPicture(plotLocation & "\" & FileName, False, True, CellRange)
        With oPic
            .LockAspectRatio = False
            .Width = CentimetersToPoints(15.5)
            .Height = CentimetersToPoints(10)
        End With
    End Sub
    Bear in mind, if the aspect ratio of your picture is correct and the width of the picture is larger than the cell width. If you set the cell width to fixed, the image will shrink to the width of the cell and the height will maintain its aspect ratio.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Yes, my table is 16.75 cm wide. It works for me too if I put it in a new document but not in my template file..

    I suspected that the AddPicture command took some time to finish, and that that was the reason the width and height commands didn't work so I tried to insert some Pause functions in between, even as long as 15 seconds, to no avail...

    I'm stuck here. Don't know what to look for..

Posting Permissions

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