Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Exit Routine for Error in Paste Image Code - Excel VBA

Status
Not open for further replies.

Randy11

Technical User
Oct 4, 2002
175
CA
Have the following code running in a workbook. This code works great as long as you have copied an image before clicking on the control button to start the code. If you do not, the following occurs:
Believe I have something out of sequence. Would like the message box to come up & exit sub on OK. Assistance appreciated...
Run Time Error 1004
PasteSpecial method of worksheet class failed.

Section of code flagged with error:
ActiveSheet.PasteSpecial Format:="Microsoft Office Drawing Object", Link:= _
False, DisplayAsIcon:=False


Sub Paste_Image()
'
' Paste_Image Macro
'

'

Call UnProtectAll

Call DeletePiccies

Range("B20").Select
ActiveSheet.PasteSpecial Format:="Microsoft Office Drawing Object", Link:= _
False, DisplayAsIcon:=False
On Error GoTo ErrMsg11
Range("I15").Select
On Error GoTo ErrMsg11

ErrMsg11:
Call Select_Clear
MsgBox ("If no Cheque Image Appears, Select & Copy Image and Try Again" & vbCrLf & _
""), , "Image Paste Error"

Call ProtectAll

Exit Sub
Call ProtectAll


End Sub
 
Try

If Err.Number = 1004 Then
MsgBox "Please copy an Image first"
Exit Sub
End If


HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Same error still remains, modified code below.

Sub Paste_Image()
'
' Paste_Image Macro
'

'

Call UnProtectAll

Call DeletePiccies

Range("A39").Select
ActiveSheet.PasteSpecial Format:="Microsoft Office Drawing Object", Link:= _
False, DisplayAsIcon:=False

Call Select_Clear
Call ProtectAll

If Err.Number = 1004 Then
MsgBox "Please copy an Image first"
Exit Sub
End If
End Sub
 
Code:
Same error still remains, modified code below.

Sub Paste_Image()
'
' Paste_Image Macro
'

'
  
 Call UnProtectAll
 
   Call DeletePiccies
  
   on error resume next

    Range("A39").Select
ActiveSheet.PasteSpecial Format:="Microsoft Office Drawing Object", Link:= _
        False, DisplayAsIcon:=False
     
 If Err.Number = 1004 Then
   MsgBox "Please copy an Image first"
   Exit Sub
 End If

      
Call Select_Clear
Call ProtectAll

End Sub


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Code:
Private Sub cmdCancel_Click()
    On Error GoTo Err_Handler
    'Purpose:   Close without transferring date back to calling text box.

    DoCmd.Close acForm, Me.name, acSaveNo

Exit_Handler:
    Exit Sub

Err_Handler:
 If Err.Number = 1004 Then
   MsgBox "Please copy an Image first"
   Exit Sub
Else
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdCancel of Form frmNameHere"
 End If    
 
    Resume Exit_Handler
End Sub
an example of the error handling code with your error added

The On Error GoTo needs only be declared at the beginning of your code once

And remove On Error Resume Next your telling access to ignore the errors. its better to capture them then give the user proper feedback in order to correct the input.

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
sorry not Access but Excel

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top