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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

In VBA, any way to capture the screen to clipboard? 3

Status
Not open for further replies.

TomCarnahan

Programmer
Dec 7, 2002
123
US
Is there anyway to capture the screen (and hopefully error messages) to the clipboard and then to a JPEG file that could then be emailed?

There are various times when I have descriptive error messages come up that are meant only for me as the developer for unanticipated errors. The error messages tell the user to contact me. The message contains info that helps me to find out where in the code the problem occurred, but is often meaningless to the user. Unfortunately, the users are overwelmed by the error message and do not write down the info.

My thought is to have the error info and an attached screen capture file (.JPG?) sent to me by email.

Regardless of whether or not screen capture is a good way to do this, I would like to know how to capture the screen for several other apps I have in mind. However, getting error info emailed to me is the most important thing.

I work with a group of 15 people scattered around a building and we are all on the same intranet, so it is not a case of being overwelmed by email, however, it would save me running around the building asking the user to duplicate the steps leading up to the error.

Thanks ahead of time for your help!

--- Tom
 

Instead of having the error message sent to you as a picture, wouldn't it be easier to have it sent to you as text? Why not just add a bit to your Error Handler that sends the error text to you in an email in the background?

Just a suggestion, because that is what I do for my apps.

;-)




Peace!! [peace]

Mike

Didn't get the answers that you wanted? Take a look at FAQ219-2884
 
This is an example of my standard code for my error mailing process:
Code:
Sub ErrorMailTest()
On Error GoTo ErrorHandler:
' Cause an Error
Exit Sub
ErrorHandler:
Dim ErrorText As String
ErrorText = "Error Number: " & Err.Number & vbLf & _
            "Description: " & Err.Description & " " & Err.Source
MailError (ErrorText)
End Sub
-----------------------------------------------------------
Sub MailError(ErrorText As String)
Dim MailSubject As String, MailBody As String
Dim newOL As New Outlook.Application, newMail As MailItem
Set newOL = New Outlook.Application
Set newMail = newOL.CreateItem(olMailItem)
MailSubject = Environ("username") & " - AUTOMATED ERROR MESSAGE."
MailBody = "An error occured in Workbook - " & ThisWorkbook.Name _
            & " - (at " & Time & " on " & Date & "). " _
            & vbLf & vbLf & "The error message read:" & vbLf _
            & vbLf & ErrorText
With newMail
    .Importance = olImportanceHigh
    .To = "noone@nowhere.com"
    .Subject = MailSubject
    .Body = MailBody
    .Display ' This will display the Email for editing
    ' .Send ' This will send the Email directly
End With
Set newMail = Nothing
Set newOL = Nothing
End Sub

I hope this helps!



Peace!! [peace]

Mike

Didn't get the answers that you wanted? Take a look at FAQ219-2884
 
Mike,

I really appreciate your method and code for error reporting. I will probably go that route to solve my immediate problem.

There are some other situations where I want to use a VBA-initiated screen capture. I appreciate the links to the screen capture utilities that DeCojute and Skip recommended. While I did not look at them in detail, I will later ... I would look at them primarily from the standpoint of "can they be manipulated from another program using VBA?"

OBTW, the screen capture program I use is called "5 clicks" at It is really easy and sends output to printer, clipboard, or to file (BMP, JPG, and PNG) It has "guidelines" that you drag around the screen to select the part of the screen you want. As its name implies, it takes very few clicks to have your captured image put into the file you want it in.

I just went back to the 5-click web site and found out that they do have a screen capture via VB interface for developers, but I'm not sure I can manipulate it via VBA as opposed to VB. I will look into it.

In the meantime, would anyone know if one could use a Windows API to capture the screen?



--- Tom
 
Here is a routine that I got from a German webpage ( I am not sure if it will work for VBA though, but it uses API calls to create a screenshot (don't ask me how though). Maybe you can play around with it to get it to work.
Code:
Private Declare Function GetDesktopWindow Lib "user32" () _
        As Long

Private Declare Function GetDC Lib "user32" (ByVal hwnd As _
        Long) As Long

Private Declare Function GetWindowRect Lib "user32" (ByVal _
        hwnd As Long, lpRect As RECT) As Long
        
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc _
        As Long, ByVal x As Long, ByVal y As Long, ByVal _
        nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC _
        As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
        ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, _
        ByVal dwRop As Long) As Long
        
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd _
        As Long, ByVal hdc As Long) As Long

Private Type RECT
  Left As Long
  Top As Long
  Width As Long
  Height As Long
End Type

Const SRCCOPY = &HCC0020

Private Sub Form_Load()
  Picture2.Top = 0
  Picture2.Left = 0
  VScroll1.LargeChange = Picture1.Height / 4
  
  VScroll1.SmallChange = 120
  HScroll1.LargeChange = Picture1.Width / 4
  HScroll1.SmallChange = 120
End Sub

Private Sub Command1_Click()
  Call ScreenShot
End Sub

Private Sub Command2_Click()
  SavePicture Picture2.Image, App.Path & "\Test.bmp"
End Sub

Private Sub Command3_Click()
  Printer.Print
  Printer.PaintPicture Picture2.Image, 0, 0, _
                       Picture2.Width, Picture2.Height, _
                       0, 0, Picture2.Width * 2, _
                       Picture2.Height * 2
  Printer.EndDoc
End Sub

Private Sub HScroll1_Change()
  Picture2.Left = -HScroll1.Value
End Sub

Private Sub VScroll1_Change()
  Picture2.Top = -VScroll1.Value
End Sub


Private Sub ScreenShot()
  Dim Result&, DesktopHwnd&, DesktopHdc&
  Dim Desktop As RECT
    
    Picture2.AutoRedraw = True
    
    '### Desktopgröße in Pixeln ermitteln
    DesktopHwnd = GetDesktopWindow()
    DesktopHdc = GetDC(DesktopHwnd)
    Result = GetWindowRect(DesktopHwnd, Desktop)
    
    '### Zielbild und Scrollbalken der Desktopgröße anpassen
    Picture2.Width = Desktop.Width * 15
    Picture2.Height = Desktop.Height * 15
    VScroll1.Max = Picture2.Height - Picture1.Height + 15
    HScroll1.Max = Picture2.Width - Picture1.Width + 15
    
    '### Der eigentliche Screenshot
    Result = StretchBlt(Picture2.hdc, Desktop.Left, Desktop.Top, _
                        Desktop.Width, Desktop.Height, DesktopHdc _
                        , 0, 0, Desktop.Width, Desktop.Height, _
                        SRCCOPY)
    
    
    '### Gerätekontext löschen
    Result = ReleaseDC(DesktopHwnd, DesktopHdc)
     
    Picture2.Refresh
    Picture2.AutoRedraw = False
End Sub

I hope that you get it to work, and if you do, then please post back with the resulting code!



Peace!! [peace]

Mike

Didn't get the answers that you wanted? Take a look at FAQ219-2884
 
Check out AllAPI.com (now at They are an excellent resource for the Windows API. If you download their API guide, you can do almost everything locally without having to go back to their site. Also, there is a VB Windows API tek-tips formum. Good Luck!

Have a great day!

j2consulting@yahoo.com
 

And those forums would be forum711 and forum713!

;-)



Peace!! [peace]

Mike

Didn't get the answers that you wanted? Take a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top