PDA

View Full Version : Copy Paste Shapes from one worksheet to another using Clipboard - Excel VBA



sifar786
03-07-2020, 09:26 AM
I am using Windows 10 Enterprise 64 bit, Office 2016 Pro 64 bit. I am trying to copy 2 shapes from one worksheet to another worksheet.


The following API code works sometimes, but most times it shoots an Error 1004 for Copy or Paste (Clipboard timing issue). I have tried using different solutions e.g. Timer, Wait, API etc., but seem slow and buggy most of the times!
Also i am trying to paste the images on opposite ends of each merged cell. I have also attached a sample file (https://docs.google.com/spreadsheets/d/1XQd3OrzMAs0bXeAjiUYvPjv2hGn5gumGVdXLeBK4Fn4/edit?usp=sharing) for inspection.


I don't remember but i read somewhere that if i create separate copy and paste functions/procedures, then it might solve the issue, though not sure!



Option Explicit

Sub DoIT()

Dim Shp1 As Shape, Shp2 As Shape, Shp3 As Shape, Shp4 As Shape, i&, j&
Dim WK1 As Worksheet, WK2 As Worksheet

With ThisWorkbook
Set WK1 = .Worksheets("test1")
Set WK2 = .Worksheets("test2")
Set Shp1 = WK1.Shapes("Arrow")
Set Shp2 = WK1.Shapes("Consumers")

j = 0
For i = 1 To 20

With WK2.Range(WK2.Cells(i + j, 3), WK2.Cells(i + j, 4))
.Merge 'merge 2 cells

On Error Resume Next
Do
ClearClipboard
Shp1.CopyPicture
WaitOnClipboard
Loop Until Err.Number = 0
On Error GoTo 0
' Pause

'copy paste Arrow shape
On Error Resume Next
Do
Err.Clear
WK2.Paste Destination:=WK2.Cells(i + j, 3) ', link:=False
DoEvents
Loop Until Err.Number = 0
On Error GoTo 0
Application.CutCopyMode = False
ClearClipboard

Set Shp3 = WK2.Shapes(WK2.Shapes.Count)
With Shp3
.Top = WK2.Cells(i + j, 3).MergeArea.Top
.Left = WK2.Cells(i + j, 3).MergeArea.Left
End With


' copy paste Consumers shape
On Error Resume Next
Do
ClearClipboard
Shp2.CopyPicture
WaitOnClipboard
Loop Until Err.Number = 0
On Error GoTo 0
' Pause

On Error Resume Next
Do
Err.Clear
WK2.Paste Destination:=WK2.Cells(i + j, 3) ', link:=False
DoEvents
Loop Until Err.Number = 0
On Error GoTo 0
Application.CutCopyMode = False
ClearClipboard

Set Shp4 = WK2.Shapes(WK2.Shapes.Count)
With Shp4
.Top = WK2.Cells(i + j, 3).MergeArea.Top
.Left = WK2.Cells(i + j, 3).MergeArea.Left + WK2.Cells(i, 3).MergeArea.Width - Shp2.Width
End With
j = j + 2
End With
Next i
End With
End Sub



The API code:



Option Explicit

' Windows API declarations
#If VBA7 Or Win64 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Declare PtrSafe Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr
Declare PtrSafe Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Declare PtrSafe Function CountClipboardFormats Lib "user32" () As Long
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Declare Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Declare Function EmptyClipboard Lib "user32" () As Long
Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Declare Function CountClipboardFormats Lib "user32" () As Long
#End If

Public Sub WaitOnClipboard()
Do
DoEvents
' Loop Until IsPicInClipboard
Loop Until IsPicOnClipboard
End Sub

' Wait until PowerPoint shape object is on the Windows clipboard
Public Sub WaitForClipboard()
Do
DoEvents
' Loop Until IsPicInClipboard
Loop Until IsPicOnClipboard
End Sub

Function IsShapeOnClipboard() As Boolean
If OpenClipboard(0&) = 0 Then Exit Function ' Could not open clipboard
IsShapeOnClipboard = IsClipboardFormatAvailable(&HC216&)
EmptyClipboard
CloseClipboard
End Function

Function IsPicInClipboard() As Boolean
If OpenClipboard(0&) = 0 Then Exit Function ' Could not open clipboard
IsPicInClipboard = False
If IsClipboardFormatAvailable(2) <> 0 Or _
IsClipboardFormatAvailable(3) <> 0 Or _
IsClipboardFormatAvailable(9) <> 0 Or _
IsClipboardFormatAvailable(14) <> 0 Or _
IsClipboardFormatAvailable(25) <> 0 Or _
IsClipboardFormatAvailable(29) <> 0 Then IsPicInClipboard = True
End Function

' Check if PowerPoint shape object is on the Windows clipboard
Public Function IsPicOnClipboard() As Boolean

Dim lFormat As Long
Dim sName As String

If OpenClipboard(0&) = 0 Then Exit Function ' Could not open clipboard
Do
lFormat = EnumClipboardFormats(lFormat)
' sName = String(255, 0)
' sName = Space(255)
' GetClipboardFormatName lFormat, sName, Len(sName)
' Debug.Print lFormat, sName
' If sName Like "*PowerPoint 12.0 Internal Shapes*" Then IsPicOnClipboard = True: Exit Do
' If InStr(1, Trim(lFormat), "14", vbTextCompare) > 0 Then IsPicOnClipboard = True: Exit Do
If (lFormat = 2 Or lFormat = 3 Or lFormat = 9 Or lFormat = 14 Or lFormat = 25 Or lFormat = 29) Then IsPicOnClipboard = True: Exit Do
Loop Until lFormat = 0

CloseClipboard

End Function

Public Sub Pause()
Dim t As Double

t = Timer
Do Until Timer - t > 1
DoEvents
Loop
End Sub

Function IsClipboardEmpty() As Boolean
IsClipboardEmpty = (CountClipboardFormats() = 0)
End Function

Public Function ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Function

Public Sub CopyShape(ItemName As String, ByRef CopyDestination As Worksheet, ByRef PasteDestination As Worksheet)
Call ClearClipboard

ThisWorkbook.Sheets(CopyDestination).Shapes(ItemName).Copy
ThisWorkbook.Sheets(PasteDestination).Paste

Do Until IsClipboardEmpty = False
DoEvents
Loop

End Sub

Function Is_Pic_in_Clipboard() As Boolean
If OpenClipboard(0&) = 0 Then Exit Function ' Could not open clipboard

If IsClipboardFormatAvailable(2) <> 0 Or IsClipboardFormatAvailable(14) <> 0 Or IsClipboardFormatAvailable(9) <> 0 Then _
Is_Pic_in_Clipboard = True '2=BMP, 14=JPEG, 9=Picture
End Function

Sub ListClipFormats()
Dim Arr, Fmt

Arr = Application.ClipboardFormats

For Each Fmt In Application.ClipboardFormats
Select Case Fmt
Case xlClipboardFormatBIFF Or 8: Debug.Print "Binary Interchange file format for Excel version 2.x"
Case xlClipboardFormatBIFF12 Or 63: Debug.Print "Binary Interchange file format 12"
Case xlClipboardFormatBIFF2 Or 18: Debug.Print "Binary Interchange file format 2"
Case xlClipboardFormatBIFF3 Or 20: Debug.Print "Binary Interchange file format 3"
Case xlClipboardFormatBIFF4 Or 30: Debug.Print "Binary Interchange file format 4"
Case xlClipboardFormatBinary Or 15: Debug.Print "Binary format"
Case xlClipboardFormatBitmap Or 9: Debug.Print "Bitmap format"
Case xlClipboardFormatCGM Or 13: Debug.Print "CGM format"
Case xlClipboardFormatCSV Or 5: Debug.Print "CSV format"
Case xlClipboardFormatDIF Or 4: Debug.Print "DIF format"
Case xlClipboardFormatDspText Or 12: Debug.Print "Dsp Text format"
Case xlClipboardFormatEmbeddedObject Or 21: Debug.Print "Embedded Object"
Case xlClipboardFormatEmbedSource Or 22: Debug.Print "Embedded Source"
Case xlClipboardFormatLink Or 11: Debug.Print "Link"
Case xlClipboardFormatLinkSource Or 23: Debug.Print "Link to the source file"
Case xlClipboardFormatLinkSourceDesc Or 32: Debug.Print "Link to the source description"
Case xlClipboardFormatMovie Or 24: Debug.Print "Movie"
Case xlClipboardFormatNative Or 14: Debug.Print "Native"
Case xlClipboardFormatObjectDesc Or 31: Debug.Print "Object description"
Case xlClipboardFormatObjectLink Or 19: Debug.Print "Object link"
Case xlClipboardFormatOwnerLink Or 17: Debug.Print "Link to the owner"
Case xlClipboardFormatPICT Or 2: Debug.Print "Picture"
Case xlClipboardFormatPrintPICT Or 3: Debug.Print "Print picture"
Case xlClipboardFormatRTF Or 7: Debug.Print "RTF format"
Case xlClipboardFormatScreenPICT Or 29: Debug.Print "Screen Picture"
Case xlClipboardFormatStandardFont Or 28: Debug.Print "Standard Font"
Case xlClipboardFormatStandardScale Or 27: Debug.Print "Standard Scale"
Case xlClipboardFormatSYLK Or 6: Debug.Print "; SYLK"
Case xlClipboardFormatTable Or 16: Debug.Print "; Table"
Case xlClipboardFormatText Or 0: Debug.Print "Text"
Case xlClipboardFormatToolFace Or 25: Debug.Print "Tool Face"
Case xlClipboardFormatToolFacePICT Or 26: Debug.Print "Tool Face Picture"
Case xlClipboardFormatVALU Or 1: Debug.Print "Value"
Case xlClipboardFormatWK1 Or 10: Debug.Print "Workbook"
End Select
Next Fmt

End Sub

Public Sub ListClipboardFormats()

Dim lFormat As Long
Dim sName As String

If OpenClipboard(0&) = 0 Then Exit Sub ' Could not open clipboard
Do
lFormat = EnumClipboardFormats(lFormat)
sName = String(255, 0)
GetClipboardFormatName lFormat, sName, Len(sName)
If Not lFormat = 0 Then Debug.Print lFormat, sName
Loop Until lFormat = 0
EmptyClipboard
CloseClipboard
End Sub

snb
03-07-2020, 09:50 AM
I'd use:


Sub M_snb()
For Each it In Sheet1.Shapes
it.CopyPicture
Sheet2.Paste Sheet2.Range(it.TopLeftCell.Address)
Next
End Sub

sifar786
03-07-2020, 09:58 AM
26128
Got `Runtime Error 1004` on line
it.CopyPicture

snb
03-07-2020, 11:06 AM
You didn't use the macro I posted.

sifar786
03-07-2020, 11:11 AM
@snb i only added the declaration `Dim it as Shape`.