2016-03-31 92 views
0

我在PowerPoint VBA中创建一个宏以从当前幻灯片导出图像。导出图像将是宽度超过250个单位的第一个图像。图像存储为Shape,所以我做了一个For Each ... Next循环来做到这一点。代码工作正常。如何使用vba在PowerPoint中调整导出图像大小?

Function FindAndSavePicture() As String 
' 
' Find the target picture at the active windows 
' 
' 
    Dim myTempPath As String 
    myTempPath = "C:\Users\" & Environ$("USERNAME") _ 
      & "\AppData\Local\Microsoft\Windows\pic_VBA.jpg" 

    With ActiveWindow.Selection.SlideRange 
     For Each s In .Shapes 
      Debug.Print s.Name 
      If s.Type = msoPicture And s.Width > 250 Then 

       ' Show scale 
       Debug.Print "s.Width=" & s.Width ' s.Width=323,3931 
       Debug.Print "s.Height=" & s.Height ' s.Height=405 

       ' Save pic in file system 
       s.Export myTempPath, ppShapeFormatJPG 

       ' assign the return value for this function 
       FindAndSavePicture = myTempPath 
       Exit For 

      End If 
     Next 
    End With 
End Function 

问题

导出的图像pic_VBA.jpg远小于它在PowerPoint中所示。 我想要图片的原始尺寸。由VBA pic_VBA.jpg导出的图像尺寸为331 x 413。如果我使用另存为图片...手动导出图像,导出图像pic_SaveAs.jpg的尺寸为692 x 862,这是原始尺寸。

  • pic_VBA.jpg尺寸:331 X 413
  • pic_SaveAs.jpg尺寸:692 X 862(原始大小)

我测试

s.Export myTempPath, ppShapeFormatJPG, s.Width, s.Height, ppScaleXY 

它不工作。出口图像的尺寸为150×413

问题

那么,如何在PowerPoint中使用VBA调整出口图像大小?


相关infomations

回答

1

在图像中的PowerPoint缩放?如果它不是100%,则需要计算X/Y尺寸的比例%,将其设置为100%,将其导出并将其缩放回存储的设置。该功能将帮助是:

' Function to return the scale percentages of a given picture shape 
' Written by: Jamie Garroch of YOUpresent.co.uk 

Public Type ypPictureScale 
    ypScaleH As Single 
    ypScaleW As Single 
End Type 

' Calculate the scale of a picture by resetting it to 100%, 
' comparing with it's former size and then rescaling back to it's original size 
Public Function PictureScale(oShp As Shape) As ypPictureScale 
    Dim ShpW As Single, ShpH As Single 
    Dim LAR As Boolean 

    ' Save the shape dimensions 
    ShpH = oShp.height 
    ShpW = oShp.width 

    ' Unlock the aspect ratio if locked 
    If oShp.LockAspectRatio Then LAR = True: oShp.LockAspectRatio = msoFalse 

    ' Rescale the image to 100% 
    oShp.ScaleHeight 1, msoTrue 
    oShp.ScaleWidth 1, msoTrue 

    ' Calculate the scale 
    PictureScale.ypScaleH = ShpH/oShp.height 
    PictureScale.ypScaleW = ShpW/oShp.width 

    ' Rescale the image to it's former size 
    oShp.ScaleHeight PictureScale.ScaleH, msoTrue 
    oShp.ScaleWidth PictureScale.ScaleW, msoTrue 

    ' Relock the aspect ratio if originally locked 
    If LAR Then oShp.LockAspectRatio = msoFalse 
End Function 
+0

请问我可以在我的代码中使用你的函数吗?我以前从不使用'Type'。我应该像'Dim myType As ypPictureScale'一样声明使用某种东西吗? –

+0

类型声明必须放入代码模块的声明部分(首先是Sub和Function过程)。 –

+0

嗨@JamieG,我试过了。你可能忘记在第27行和第28行添加前缀“yp”?在第27行中,它应该是'PictureScale。代替ypScaleH'。除了这个问题,你的代码完美地工作。然而,它并没有解决我的问题,因为它导出的图像大小与我使用默认设置's.Export myTempPath,ppShapeFormatJPG'时的大小相同。我无法接受它,但我赞成你的答案。我现在不再处理这个问题(我的同事不再需要它了),但感谢您的帮助! –

0

这不是从你的意见明确,但你可能会缺少PowerPoint使用点的事实(72点到英寸)的尺寸,而不是英寸或像素。

将形状的大小从点转换为英寸然后乘以150以获得PPT将导出到的大小。

150可能会因系统而异,但我不相信它。

+0

感谢您的回应,史蒂夫。是的,我没有提到单位换算。但恐怕问题不在这里。当输入“宽度”和“高度”时,输出模式不符合尺寸比例。 '331:413' ='692:862' = 0.80,但是'150:413' = 0.36 –

+0

我认为它可能不是一个简单的乘法(例如150)它迁徙需要像@JamieG提出的规模高度和规模宽度。不幸的是,我不知道如何应用他的代码...... –

+0

这取决于在导出图像之前将图像恢复为原始大小(如PPT构想的)是否重要。 –

相关问题