Copy whole sheet to clipboard as picture when sheet protected

MarkieVBA

New Member
Joined
Nov 4, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have some simple code to copy a sheet (an invoice form) as a picture to the clipboard for pasting into Outlook emails through a command button:

Sub ButtonCopy()
ActiveSheet.Range("A1:DG182").CopyPicture xlScreen, Format:=xlPicture
ActiveSheet.Paste
Selection.Width = 500
Selection.Height = 500
Selection.Copy
Selection.Delete
MsgBox "Invoice copied to clipboard." & vbNewLine & "" & vbNewLine & "Please paste (Rt Click or CTRL+V) in your existing email (with documents attached) from the supplier/office"
End Sub

It works exactly how I want it BUT only if the 'edit objects' option of the Protect Sheet function is unticked (which leaves the command buttons editable and a backdoor to the VBA which is bad news).

When ticked and fully protected it produces runtime error 1004 .. 'cannot paste the data' and the debugger highlights the 'ActiveSheet.Paste' line.

I've seen similar examples online but they all seem to deal with pasting the sheet into another sheet which doesn't help. I also tried using 'On Error Resume Next' and 'Application.CutCopyMode = False' which did allow me to fully protect the sheet without the 1004, but it only copied the currently highlighted box as a picture.

I feel the solution is close, but I've run out of ideas. Can anyone please help?

Many thanks in advance.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I've seen similar examples online but they all seem to deal with pasting the sheet into another sheet which doesn't help.

Have you considered using a temporary worksheet? After all, you can easily remove it afterwards, like this:

VBA Code:
Sub ButtonCopy()

    ActiveSheet.Range("A1:DG182").CopyPicture xlScreen, Format:=xlPicture

    Application.ScreenUpdating = False
    With Workbooks.Add.ActiveSheet
        .Paste
        With .Shapes(1)
            .Width = 500
            .Height = 500
            .Copy
        End With
        .Parent.Close SaveChanges:=False
    End With
    Application.ScreenUpdating = True

    MsgBox "Invoice copied to clipboard." & vbNewLine & "" & vbNewLine & "Please paste (Rt Click or CTRL+V) in your existing email (with documents attached) from the supplier/office"
End Sub
 
Upvote 0
Solution
Glad to help and welcome to this board!
 
Upvote 0
Thanks again. I was wondering if this method could be expanded to copy 2 sheets and merge them (side by side or up and down shouldn't matter) in the clipboard?

I tried:

VBA Code:
Worksheets("Sheet1").Range("A1:DG182").CopyPicture xlScreen, Format:=xlPicture
Worksheets("Sheet2").Range("A1:DG182").CopyPicture xlScreen, Format:=xlPicture

and also placing this 2nd line before the parent.close and repeating the With & End With section but neither worked.

Then I tried a union merging:

VBA Code:
Sub ButtonCopy()
Dim range1 As Range, range2 As Range, multiplerange As Range
    Set range1 = Sheets("Sheet1").Range("A1:DG182")
    Set range2 = Sheets("Sheet2").Range("A1:DG182")
    Set multiplerange = Union(range1, range2)
    multiplerange.CopyPicture xlScreen, Format:=xlPicture
    Application.ScreenUpdating = False
    With Workbooks.Add.ActiveSheet
        .Paste
        With .Shapes(1)
            .Width = 500
            .Height = 500
            .Copy
        End With
        .Parent.Close SaveChanges:=False
        End With
Application.ScreenUpdating = True
MsgBox "Invoices copied to clipboard." & vbNewLine & "" & vbNewLine & "Please paste (Rt Click or CTRL+V) in your existing email (with documents attached) from the supplier/office"
End Sub

But this just produces a subscript error 9.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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