open an image from a userform

B

Brad

I have a user form with a command button, that when clicked I would like to
open a picture in the users default picture viewer. Any help on how to
accomplish this would be greatly appreciated.

Would it be better to just creat another userform with the image on it and
then just show the form when the button is clicked?
 
R

Rick Rothstein

Copy/Paste this code line into a Module (click Insert/Module from the VB
editor's menu bar)...

Public Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

You can then show a picture by specifying its full path to the ShellExecute
function as shown in this example macro...

Sub ShowPicture()
Dim FileName As String
FileName = "D:\Programming\VB Code\3.bmp"
ShellExecute Application.hwnd, "Open", FileName, "", "", vbNormalFocus
End Sub

Just change my example full path/filename assigned to the FileName variable
to the full path/filename for your picture file. Of course, you can query
the user for the filename and path if that is what you are ultimately
wanting to do.

As for your "Would it be better to just create another userform with the
image on it and then just show the form when the button is clicked?"
question... that depends on how you want the code to work... it is your
choice. One thing that may be a consideration for you is that the default
viewer can display many more picture file formats than can be done using a
UserForm's (or Image control's) Picture property.
 
Q

quaz

Rick,

Thanks for the snippet below, it works great for an application I am
writing. Do you have any idea how to do a similar thing to open an MPEG file
in the users default movie player?
 
Q

quaz

Rick,

Never mind, I figured it out. Your code works based on the file extension.
Just had to get the right one. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top