PDA

View Full Version : [SOLVED:] Insert Image in user form via vba



ccollins48
11-14-2008, 07:43 AM
I have an excel 2003 user form with an image control. I would like to give the user the ability to insert an image by browsing folders.

I currently have LoadPicture code in place, but this requires the user to type the file path and image extension.

My goal is to have the image control to function the the same as MenuBar>Insert>Picture>FromFile.

Dr.K
11-14-2008, 08:18 AM
So all you need is a file browser?

There are a bunch of ways to do that, why don't you start with this one: Application.GetOpenFilename

The help file has examples.

ccollins48
11-14-2008, 08:44 AM
Thanks for the quick reply Dr.K.
Here is my current code for the image control

Private Sub Image1_Click()
Dim PicPath As String
Dim PicName As String
PicName = InputBox("Please Provide Image path Name Including Extension")
PicPath = PicName '("C:\Documents and Settings\Credit Desk\My Documents\My Pictures\") & "" & (PicName)
If Len(Dir(PicPath)) = 0 Then
MsgBox PicPath & " does not exist."
Else
Me.Image1.Picture = LoadPicture(PicPath)
Me.Repaint
End If
End Sub

I ran the Application.GetOpenFile by itself in the Image1_click event, It opened the dialog box, but when I selected an image and clicked open, nothing happened.

Bob Phillips
11-14-2008, 08:51 AM
GetOpenFilename doesn't open the file, as stated in the method description in help, it gets the full file path and name of the selected file. You still have to handle it yourself

ccollins48
11-14-2008, 09:48 AM
I would like to thank both of you for your input.

Here is my Code revision, which functions just by clicking on the image file and clicking open. It imports the image to the image object as desired.


Private Sub Image1_Click()
Dim PictFileName As String
PictFileName = Application.GetOpenFilename
PicPath = PictFileName
If Len(Dir(PicPath)) = 0 Then
MsgBox PicPath & " does not exist."
Else
Me.Image1.Picture = LoadPicture(PicPath)
Me.Repaint
End If
End Sub

For someone who wishes to use this method, Please remember to set the PictureSizeMode in the Properties for the image object to 3-fmPictureSizeModeZoom. This will resize the image to fit your image object borders.

Thanks Again.

Dr.K
11-17-2008, 08:16 AM
The other thing to remember about using .GetOpenFilename is that you should use a Variant for the return value. If the user click's "Cancel"
then the return value is False.

Here is a wrapper function I use for .GetOpenFilename. It has built in "File Filter" strings, and it allows you to open a file from a specific location without changing the current default path. The Windows API function is required for setting the initial location to a shared/network location, as the VBA ChDir can't do this.

This is the API function, it goes up top in your module:


'function for changing path ("CD") to any folder, regardless of its location
Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" _
(ByVal lpPathName As String) As Long

Here is the main function:


Function GetOpenFileNameFrom(DefaultDir As String, _
Optional FileType As Variant, Optional Title As String, _
Optional MultiSelect As Boolean) As Variant
'Wrapper function for: Application.GetOpenFilename
'Function Purpose: To get a file name for a file, with the browse window
'opening in a specified directory. Returns either the full path including
'the filename, or False if nothing selected
Dim strCurrentDir As String
Dim strFileType As String
'Set the file type desired to show in the dialog box
Select Case LCase(FileType)
Case 1, "xls"
Let strFileType = "Excel Workbooks (*.xls), *.xls"
Case 2, "xlt"
Let strFileType = "Excel Templates (*.xlt), *.xlt"
Case 3, "csv"
Let strFileType = "CSV (Comma Delimited) (*.csv), *.csv"
Case 4, "txt"
Let strFileType = "Text Files (*.txt), *.txt"
Case 5, "doc"
Let strFileType = "Word Documents (*.doc), *.doc"
Case 6, "pdf"
Let strFileType = "Adobe Acrobat Document (*.pdf), *.pdf"
Case 7, "fdf"
Let strFileType = "Adobe Acrobat Data File (*.fdf), *.fdf"
End Select
'Record the current irectory
Let strCurrentDir = CurDir
'If DefaultDir path does not exist, set it = to current directory
If Len(Dir(DefaultDir, vbDirectory)) = 0 Then
Let DefaultDir = strCurrentDir
End If
'Change the directory to the desired folder
SetCurrentDirectory DefaultDir
'Get the file's name & path
GetOpenFileNameFrom = Application.GetOpenFilename(strFileType, , _
Title, , MultiSelect)
'Change the directory back to whatever it was
SetCurrentDirectory strCurrentDir
End Function

fhimage
06-20-2013, 09:08 PM
I have an excel 2003 user form with an image control. I would like to give the user the ability to insert an image by browsing folders.

I currently have LoadPicture code in place, but this requires the user to type the file path and image extension.

My goal is to have the image control to function the the same as MenuBar>Insert>Picture>FromFile.
i am looking everwhere for the image control too