I would like to save userform as an image inside .xlsx file. I have button in userform to save as .xlsx. So my code should take a screenshot, create new Excel file, paste screenshot in there and save newly created Excel file as and .xlsx file on users desktop. For some reason my code does not take a screenshot and not pasting anything to Excel file. What is possibly wrong?
This part comes first:
Code for button on userform:
---------------------
Error handler is pointing on `ActiveSheet.PasteSpecial Format:="Bitmap"` and says that "PasteSpecial method of Worksheet class failed. It seems it actually takes a screenshot but can't paste it for some reason. Maybe it does not understand what is ActiveSheet?
Same question asked here: https://stackoverflow.com/questions/55574557
This part comes first:
Code:
Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const VK_SNAPSHOT = 44
Const VK_LMENU = 164
Const KEYEVENTF_KEYUP = 2
Const KEYEVENTF_EXTENDEDKEY = 1
Code for button on userform:
Code:
Private Sub CommandButton5_Click()
Application.ScreenUpdating = False
On Error Resume Next
Application.DisplayAlerts = False
keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0
keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0
Application.PrintCommunication = False
Path = Environ("USERPROFILE") & "\Desktop\"
Application.SendKeys "(%{1068})"
DoEvents
Workbooks.Add
ActiveSheet.PasteSpecial Format:="Bitmap"
ActiveSheet.Range("A1").Select
ActiveSheet.SaveAs FileName:=Path & ThisWorkbook.Sheets("Other Data").Range("P14").Value & "," & " " & "Summary" & "_" & Format(Now, "dd.mm.yyyy") & ".xlsx"
ActiveWorkbook.Close False
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
---------------------
Error handler is pointing on `ActiveSheet.PasteSpecial Format:="Bitmap"` and says that "PasteSpecial method of Worksheet class failed. It seems it actually takes a screenshot but can't paste it for some reason. Maybe it does not understand what is ActiveSheet?
Same question asked here: https://stackoverflow.com/questions/55574557