PowerPoint VBA - Copy shape to a slide

余生长醉 提交于 2019-12-30 13:57:11

问题


I have developed a Powerpoint VBA function to which I pass a Shape and Slide object.

The function finds for a shape with text LOGO inside it, if it finds, it replaces that shape with the shape I passed to the function.

Function works perfectly on office 2013 but not on Office 2016.

Can anybody please suggest a work around for this?

Public Sub AddLogo_ONE(shLogo As Shape, oSlide As PowerPoint.Slide)
    Dim sh As Shape

    For Each sh In oSlide.Shapes
        If sh.HasTextFrame Then
            If UCase(sh.TextFrame2.TextRange.Text) = "LOGO" Then
                oSlide.Select
                DoEvents: DoEvents
                shLogo.Copy
                With oSlide.Shapes.Paste
                    .LockAspectRatio = msoFalse
                    .Left = sh.Left
                    .Top = sh.Top - ((.Height - sh.Height) / 2)
                    .AlternativeText = "LogoMacro"
                    sh.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 255, 255)
                End With
                Exit For
            End If
        End If
    Next
End Sub

Below is the error message I get on Powerpoint 2016:


回答1:


That's the dreaded machine dependent timing issue with VBA/Clipboard/WinOS. I have personally spent hours trying to devise a clever solution for this, even using WinAPIs to check and wait for a PowerPoint type of content to be available in the clipboard before proceeding with a Paste operation, all to no avail.

The only solution I have found that works is to slow VBA down with a delay. Nasty workaround as it's still machine dependent. This is the function I use:

Public Sub Delay(Seconds As Single, Optional DoAppEvents As Boolean)
  Dim TimeNow As Long
  TimeNow = Timer
  Do While Timer < TimeNow + Seconds
    If DoAppEvents = True Then DoEvents
  Loop
End Sub

If you call this as follows (reduce the time from 1 second until it fails and then double it again!), it should solve your issue:

shLogo.Copy
Delay 1, True
With oSlide.Shapes.Paste


来源:https://stackoverflow.com/questions/36262403/powerpoint-vba-copy-shape-to-a-slide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!