Search code examples
excelvbaimageuserform

Creating an image from a userform of exported charts


I have written a code that charts various data. The charts are then exported as .gif files. The image files are then loaded as pictures into image controls in a userform as shown below.

How do I go about exporting the whole userform (preferably without the 3 buttons on the right) as an image for use in a report?

Currently I am using manual printscreen but it is quite time consuming..

Chart Example

Currently I am manually using printscreen but it is quite time consuming.. Is there a way to access the alt-printscreen button perhaps?

The chart creation code itself isn't really relevant but I can share if required.

I expect an image file that I can then use within a word document. Preferably cropped to remove the 3 buttons on the right of the userform.


Solution

  • Steven,

    In the below sub, I used the SendKeys method to send Alt+PrintScreen ("%{1068}") and Ctrl+V ("^v") on the current application. The SendKeys method simulates the key presses of a keyboard, so it would replicate what you're doing right now.

    Sub PrintTheScreen()
        Application.SendKeys ("%{1068}")
        DoEvents
        Application.SendKeys ("^v")
        DoEvents
    End Sub
    

    Reference for this method can be found here

    Having the sendkeys applied to the userform will simulate your button press. By then, you could set a word application object and paste the printed screen, for example.

    Sub CreatingWDDoc() Dim appWD As Word.Application

    Set appWD = CreateObject("Word.Application")
        appWD.Visible = True
        appWD.Documents.Add
        appWD.SendKeys ("^v")
        ' Change the string to the path you wish to use for seving the file
        appWD.SaveAs (“”)
        appWD.Quit
    End Sub
    

    Keep in mind that to use such functions, you need to have the word library referenced in your project, under Tools >> References. More information can be found here.