Search code examples
vbapowerpointscale

Find original scale of Powerpoint VBA msoLinkedPicture


I have a routine that update all links in a PowerPoint presentation. However, if the images that comprise the links have been resized, they will come in at a the same size, which will distort them.

I wish to ascertain the original scale of the image link, then after updating it, reset it to that scale (not all of our images are scaled the same, so I can't simply rescale them to 100% or whathaveyou).

The ScaleWidth and ScaleHeight methods to not seem to have any related properties which can be read.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.shape.scalewidth%28v=office.14%29.aspx

Dim objPres As Object
Dim objSlide As Object
Dim objShape As Object
Dim myPath As String
Dim myName As String
Dim valueWidth As Long
Dim valueHeight As Long

Dim sl As Slide, sh As Shape, myMaster As Integer, count As Integer, relinked As Integer, lt As CustomLayout, sm As Master, ds As Design
Dim failureCounter As String, successCounter As Integer
successCounter = 0
'pages
For Each objSlide In ActivePresentation.Slides
For Each objShape In objSlide.Shapes
    If objShape.Type = msoLinkedPicture Then
        myName = objShape.LinkFormat.SourceFullName
        valueWidth = objShape.ScaleWidth '<- this does not work
        valueHeight = objShape.ScaleHeight '<- this does not work
        If Dir(myName) <> "" Then
            objShape.LinkFormat.update
            successCounter = successCounter + 1
            objShape.ScaleWidth valueWidth, msoTrue
            objShape.ScaleHeight valueHeight, msoTrue
        Else
            failureCounter = failureCounter & "," & myName
        End If
    End If
Next objShape
Next objSlide

Solution

  • I figured out a workaround. First, note the current size of the image. Then, rescale it to 100%, and calculate the ratio of the image at 100% to the original size. After updating the link, reapply that ratio. Not elegant, but it works well, and quickly.

    Sub testScale()
    Dim objShape As Object
    Dim sHeightOld As Variant
    Dim sWidthOld As Variant
    Dim tScaleWidth As Variant
    Dim tScaleHeight As Variant
    Set objShape = Application.ActiveWindow.Selection
    sHeightOld = objShape.ShapeRange.Height
    sWidthOld = objShape.ShapeRange.Width
                objShape.ShapeRange.ScaleWidth 1#, msoTrue
                objShape.ShapeRange.ScaleHeight 1#, msoTrue
    tScaleHeight = sHeightOld / objShape.ShapeRange.Height
    tScaleWidth = sWidthOld / objShape.ShapeRange.Width
        objShape.ShapeRange.LinkFormat.update
        objShape.ShapeRange.ScaleWidth tScaleWidth, msoTrue
        objShape.ShapeRange.ScaleHeight tScaleHeight, msoTrue
    End Sub