在 PowerPoint 中将形状粘贴为 PNG 格式时出现错误:“指定的数据类型不可用”

2023-12-29

我需要编写一个宏来将 PPT 演示文稿中的所有分组图(形状、箭头和文本)转换为 PNG。 (我正在使用一些电子学习软件转换 PPT,但图表最终损坏;我需要它们是 PNG,因为增强型图元文件也会出现问题)。

我一直在使用一些稍微修改过的宏代码,将图片(增强元文件)转换为 PNG。我所做的就是将 msoPicture 更改为 msoGroup:

Sub ConvertAllPicsToPNG()
    Dim oSl As Slide
    Dim oSh As Shape

    For Each oSl In ActivePresentation.Slides
        For Each oSh In oSl.Shapes
            ' modify the following depending on what you want to
            ' convert
            Select Case oSh.Type
                Case msoGroup
                    ConvertPicToPNG oSh
                Case Else

            End Select
        Next
    Next

End Sub

Sub ConvertPicToPNG(ByRef oSh As Shape)
    Dim oNewSh As Shape
    Dim oSl As Slide

    Set oSl = oSh.Parent
    oSh.Copy
    Set oNewSh = oSl.Shapes.PasteSpecial(ppPastePNG)(1)

    With oNewSh
        .Left = oSh.Left
        .Top = oSh.Top
        Do
            .ZOrder (msoSendBackward)
        Loop Until .ZOrderPosition = .ZOrderPosition
    End With

    oSh.Delete

End Sub

我收到错误“形状(未知成员)”

Set oNewSh = oSl.Shapes.PasteSpecial(ppPastePNG)(1)

我怀疑我在使用 VBA 的对象引用模型时遇到了问题,正如研究告诉我的 GroupItems 和 GroupShapes 的问题,但我无法理解它。


我在 PPT 2010 中收到此错误:“形状(未知成员):请求无效。剪贴板为空或包含可能无法粘贴到此处的数据。”

当您缩小或使用选择窗格时,我们都注意到有“形状 125”:

经过大量的试验和错误(我认为嵌套可能是一个问题,并尝试取消嵌套它们 - 成功,但错误仍然发生)我注意到它们每个都有一个高度0。如果我将其更改为任何正值,那就成功了!

所以这里是修复方法——调用一个新函数来确保形状的高度 > 0:

    For Each oSh In oSl.Shapes
        ' modify the following depending on what you want to
        ' convert
        Select Case oSh.Type
            Case msoGroup
                'Ensure each grouped shape has h/w of at least "1"
                FixShape oSh
                ConvertPicToPNG oSh
            Case Else

这是函数:

Function FixShape(ByRef oSh As Shape)

Dim s As Shape
'## Iterate the GroupItems collection and ensure minimum height/width
'   for converion to png/jpg/etc.
For Each s In oSh.GroupItems
    If s.Height = 0 Then s.Height = 1
    If s.Width = 0 Then s.Width = 1
    'Recursive
    If s.Type = msoGroup Then
        Set s = FixShape(s)
    End If
Next

Set FixShape = oSh

End Function

这是将形状转换为 PNG 的最终输出:

此错误的根本原因

您似乎无法将高度/宽度为 0 的形状粘贴为 PNG 格式(尽管您可以将它们粘贴为形状)。这似乎是一个有意的限制,但不幸的是错误消息不明确。

此错误的解决方案

在尝试粘贴为图像格式(PNG、JPG 等)之前,请确保形状的最小尺寸为 1x1

虽然您能够通过删除有问题的形状来解决问题,但这应该会对您有所帮助,这样您就不必搜索这些窗格外的形状或在将来再次尝试解决此问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 PowerPoint 中将形状粘贴为 PNG 格式时出现错误:“指定的数据类型不可用” 的相关文章

随机推荐