Copy from Clipboard to Sheet and check for empty clipboard

oldguy51

New Member
Joined
Mar 6, 2013
Messages
7
Hi, first post and I would consider myself low range of intermediate user but not brand new.

I have a program that produces a picture output of data. I need to copy that picture to clipboard and then paste to a sheet to a defined cell as the upper left quadrant of the picture, it then resizes and pastes

The code to do all this works fine but if the user forgets to load the picture to clipboard nothing happens. I need to check the clipboard to see if data is there, if not produce error message to load picture to clipboard, then proceed. Also, if data is loaded I don't want the error code to appear, which is was for me.

First code is what works but will show error code if clipboard is empty or not.

Second code is my attempt to fix after reading many posts but my lack of experience has hacked that up.

Thank you for your time and thoughts,

Working Code:

Sub CopyPicX()
'
' CopyPicOpenFreq Macro
'
'
Sheets("Front Sign Off").Activate
ActiveSheet.Unprotect ("XXX")
On Error GoTo Opps 'Error Message Box
Range("Q45").Select
ActiveSheet.Paste
Selection.ShapeRange.LockAspectRatio = msoFalse 'allows to change size
Selection.ShapeRange.ScaleWidth 1.38, msoTrue, msoScaleFromTopLeft ' lock the aspect ratio (do not distort picture)
Selection.ShapeRange.ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
Application.CutCopyMode = False ' clear the clipboard
ActiveSheet.Protect ("XXX")
Opps:
MsgBox ("You have not copied picture to clipboard yet!") 'Warning messege, then finishes macro
' GoTo Done
'Done:
End Sub


Hacked Code:

Sub CopyPicOpenFreq()
'
' CopyPicOpenFreq Macro
'
'
Dim DataObj As New MSForms.DataObject
Dim S As Picture 'Not sure what to call "S" here, was text but I am copying /pasting a picture


Sheets("Front Sign Off").Activate
ActiveSheet.Unprotect ("XXX")


DataObject.GetFromClipboard
S = DataObj.Getpicture


If Err.Number <> 0 Then
MsgBox ("You have not copied picture to clipboard yet!") 'Warning messege clipboard empty, then finishes macro
On Error GoTo 0
Exit Sub
End If

If Len(S) <> 0 Then
Range("Q45").Select
ActiveSheet.Paste
Selection.ShapeRange.LockAspectRatio = msoFalse 'allows to change size
Selection.ShapeRange.ScaleWidth 1.38, msoTrue, msoScaleFromTopLeft ' lock the aspect ratio (do not distort picture)
Selection.ShapeRange.ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
Application.CutCopyMode = False ' clear the clipboard
ActiveSheet.Protect ("XXX")
End If

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Finally figured it out! Maybe will help others... By putting the line in to "Exit Sub" it stopped the normal execution of code until there was an empty clipboard.

Rich (BB code):
Sub CopyPicOpenFreq()
'
' CopyPicOpenFreq Macro
'
'
Sheets("Front Sign Off").Activate
ActiveSheet.Unprotect ("rideMTV")
On Error GoTo Opps         'Error Message Box
    Range("Q45").Select
    ActiveSheet.Paste
    Selection.ShapeRange.LockAspectRatio = msoFalse 'allows to change size
    Selection.ShapeRange.ScaleWidth 1.38, msoTrue, msoScaleFromTopLeft          ' lock the aspect ratio (do not distort picture)
    Selection.ShapeRange.ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
    Application.CutCopyMode = False                                             ' clear the clipboard
    ActiveSheet.Protect ("rideMTV")
Exit Sub    'This will stop normal execution of Error box unless there is an error
Opps:
   MsgBox ("You have not copied picture to clipboard yet!") 'Warning messege, then finishes macro
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,510
Messages
6,125,228
Members
449,216
Latest member
biglake87

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top