【问题标题】:How to adjust export image size in PowerPoint using vba?如何使用 vba 在 PowerPoint 中调整导出图像大小?
【发布时间】:2016-03-31 12:46:49
【问题描述】:

我正在 PowerPoint VBA 中创建一个宏以从当前幻灯片中导出图像。导出图像将是第一个宽度大于 250 个单位的图像。图像存储为Shape,因此我执行For Each ... Next 循环来执行此操作。代码运行良好。

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 x 413

问题

那么,如何使用 vba 在 PowerPoint 中调整导出图像的大小?


相关信息

【问题讨论】:

    标签: image vba export powerpoint


    【解决方案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
    

    【讨论】:

    • 你能告诉我在我的代码中哪里使用你的函数吗?我以前从未使用过Type。我应该像Dim myType As ypPictureScale这样声明吗?
    • 类型声明必须放在代码模块的声明部分(高于所有 Sub 和 Function 过程)。
    • 嗨@JamieG,我试过了。您可能忘记在第 27 行和第 28 行添加前缀 yp ?在第 27 行,它应该是 PictureScale.ypScaleH。除了这个问题,您的代码运行良好。但是它并没有解决我的问题,因为它导出的图像大小与我使用默认设置 s.Export myTempPath, ppShapeFormatJPG 所做的相同。我不能接受它,但我赞成你的回答。我现在将停止处理这个问题(我的同事不再需要它),但感谢您的帮助!
    • 糟糕。对不起那个错误。我更正了它以防其他人看到这个问题。
    【解决方案2】:

    您的 cmets 并不清楚,但您可能错过了 PowerPoint 使用点(72 点到英寸)作为尺寸,而不是英寸或像素这一事实。

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

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

    【讨论】:

    • 感谢您的回复,史蒂夫。是的,我没有提到单位转换。但恐怕问题不在这里。输入s.Widths.Height时,导出模式不考虑大小比例。 331 : 413 = 692 : 862 = 0.80,但150 : 413 = 0.36
    • 我认为Export Sub 可能不是简单的乘法(例如 150)。正如@JamieG 建议的那样,它可能需要一个比例高度和一个比例宽度。不幸的是,我不知道如何应用他的代码...
    • 这取决于在导出之前将图像恢复到原始大小(如 PPT 所设想的那样)对您是否重要。
    • 导出前还原图片不重要,重要的是还原。
    • 我不明白你的意思。
    【解决方案3】:

    在Shape.Export 方法中使用ActivePresentation.PageSetup.SlideWidthActivePresentation.PageSetup.SlideHeight 作为ScaleWidth 和ScaleHeight 来接收具有原始尺寸的图片文件。

    【讨论】:

    • 这个有效。我从 ppt 中导出了 4 个不同大小的图表。由于 pageSetup.SlideHeight 是一个常数,我实际上最终对所有图表使用相同的宽度和高度,有趣的是,这给了我需要的结果:输出文件大小与原始文件大小相同(右纵横比)。因此,如果我需要双倍尺寸,我只需使用 Call shpRng(1).Export("c:\" + CStr(i) + ".png", ppShapeFormatPNG, ActivePresentation.PageSetup.SlideWidth * 2, ActivePresentation.PageSetup.SlideHeight * 2).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多