Results 1 to 6 of 6
  1. #1
    Join Date
    May 2010
    Posts
    4

    Unanswered: Code that saves a bmp file from a screen shot returns error the first time it's run.

    Hi.

    I use MS Access 2003.

    My code puts a screen shot of a form into the clipboard and saves it to a bitmap file.

    If the clipboard is empty or contains text when the code runs, it actually does add a screen shot to the clipboard but it fails on the line:
    stdole.SavePicture IPic, strFileName ' Save the file so it doesn't save the file.

    After it fails, if I run the code again (now the clipboard holds a bitmap at the start), it adds the screen shot to the clipboard and saves the file as it should do.

    I have attached a sample database that demonstrates the error. To recreate the error open the database and click the 'Save Screen Shot' button. It will give an out of memory error. Click 'End' and click the 'Save Screen Shot' button again and it will save a screen shot of the form as a file called "C:\Test.bmp". Now if you copy some text and click the 'Save Screen Shot' button again the error returns.

    Any help would be greatly appreciated.

    Thanks.
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You should try to empty the clipboard first.
    Have a nice day!

  3. #3
    Join Date
    May 2010
    Posts
    4
    Quote Originally Posted by Sinndho View Post
    You should try to empty the clipboard first.
    Thanks for your reply.

    I have tried that and still get identical results.

  4. #4
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    I can't recall the sendkeys command to copy to the clipboard but to paste, it's a simple:

    SendKeys ("^v")

    You could avoid all the API module coding and perhaps just use the sendkeys method. Be careful though when using the Sendkeys command in code.

    I used to automatically copy to the clipboard and paste objects into an ole object field so I'm not sure how you'd utilize it to save it as a file.
    Last edited by pkstormy; 05-03-10 at 20:07.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  5. #5
    Join Date
    May 2010
    Posts
    4
    Quote Originally Posted by pkstormy View Post
    I can't recall the sendkeys command to copy to the clipboard but to paste, it's a simple:

    SendKeys ("^v")

    You could avoid all the API module coding and perhaps just use the sendkeys method. Be careful though when using the Sendkeys command in code.

    I used to automatically copy to the clipboard and paste objects into an ole object field so I'm not sure how you'd utilize it to save it as a file.
    Thanks pkstormy but I have read somewhere that SendKeys cannot be used for print screen (PRTSCN).

    I need to save a screen shot of the active form because it has calculations on data that gets over-written each week.

  6. #6
    Join Date
    May 2010
    Posts
    4

    Thumbs up

    Hi. I'd like to let everyone know that I managed to find a work around for this issue. I'm not quite sure why this was necessary for it to work but here it is.

    I changed the opening of the clipboard to be inside the 'With Pic' block.
    I then added a msgbox that will ask the user to click 'OK'. When the user clicks ok, the code goes back and takes the screen shot again and then runs to the end.

    Code:
    Public Function SaveBitmap()
    Dim Pic As PicBmp, IPic As IPicture, IID_IDispatch As Guid, strFileName As String
    Dim theCnt As Integer, theMsg As String
    
    theCnt = 0
    strFileName = "C:\Test.bmp" ' Set the filename variable.
    
    startOver:
    theCnt = theCnt + 1
    keybd_event VK_MENU, 0, 0, 0                    'press Alt
    keybd_event VK_SNAPSHOT, 0, 0, 0                'press PrintScrn
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0  'release it
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0      'release it
    
    DoEvents
    
    With IID_IDispatch
        .Data1 = &H20400
        .Data4(0) = &HC0
        .Data4(7) = &H46
    End With
    
    With Pic
    Call OpenClipboard(0&)
        .Size = Len(Pic)
        .Type = 1
        .hBmp = GetClipboardData(CF_BITMAP)
    End With
    
    OleCreatePictureIndirect Pic, IID_IDispatch, 1, IPic ' Create the picture object
    
    If theCnt = 1 Then
        theMsg = MsgBox("Click ok to save the file.", vbOKOnly + vbInformation)
        DoEvents
        If theMsg = 1 Then GoTo startOver
    End If
    
    On Error GoTo errorEncountered
    stdole.SavePicture IPic, strFileName ' Save the file
    
    errorEncountered:
    If Err.Number <> 0 Then
    MsgBox " Error# " & Err & " " & """" & Err.Description & """"
    End If
    
    Call EmptyClipboard ' Empty the clipboard
    Call CloseClipboard ' Close the clipboard
    
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •