PDA

View Full Version : Solved: Form with List Box and Image



ABrown
04-04-2008, 09:11 AM
I am developing a form which needs to populate a list box with the contents of a folder. This part I have managed. I now need to be able to click on the file name in the list and open a jpg (which is a screen shot of the signature block contained in the word file) for the user to see. This will ensure they have the correct file. I then need to be able to get them to click on an insert button to insert the file into the current doc at the cursor position. I have the list in the list box and I have been able to open the file so I know that the populating of the list box is working, but I am now stuck.

This is the code I have for populating the list box:


Private Sub UserForm_Initialize()
Dim List As Variant
UserForm3.Caption = "Double click on the file that you wish to open"
With Application.FileSearch
.NewSearch
.LookIn = "H:\New Templates\Provision Bank"
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
For i = 1 To .FoundFiles.Count
UserForm3.ListBox1.AddItem Dir(.FoundFiles(i))
Next i
Else
Unload Me
MsgBox "There were no matching files found."
End If
End With
End Sub

Can anyone point me in the right direction?

many thanks.
Edit: VBA tags added to code. Select your code when posting and hit the vba button

fumei
04-04-2008, 02:29 PM
"I now need to be able to click on the file name in the list and open a jpg (which is a screen shot of the signature block contained in the word file) for the user to see. "

Clarify this. Open (the jpg?)...where? How?

Define your requirements, and describe them explicitly, EXACTLY.

ABrown
04-05-2008, 06:03 AM
Apologies for the ambiguity - to clarify - I have two folders
- one containing Word documents
- one containing .jpgs (these files have identical names (they are a screen shot of the content of the Word file which are signature blocks for legal documents)

The Screen shot (almost like a photo if it were a photo dierctory) will assist the user in clarifying they have the correct signature block.

With the list box populated with the file names of the Word documents, I need to be able to display the .jpg file of the same name.

Once the user has identified they have the correct file, I need a button the user presses that will insert the currently selected file in the list (Word doc) into the Active window at the cursor point. All of these objects will be on a form which gets loaded from a menu in the Word template called "Provision Bank".

I hope the above clarifies better what I am trying to do. As previously posted, I have managed to populate the list box with the file names, it is being able to display the .jpgs that I have been unable to do.

Many thanks. Annette

fumei
04-07-2008, 09:53 AM
"it is being able to display the .jpgs"

I will ask again. Display where?

I will repeat: Define your requirements, and describe them explicitly, EXACTLY.

It seems to me that IF - and personally I would have trouble trusting such (which I take it is exactly the point) - the jpg files are the same name as the doc file...what is the problem?

GeorgeClooney.doc matches to GeorgeClooney.jpg

WHY do you need to verify? Not that I think this is wrong, but IF (as you say):

"they are a screen shot of the content of the Word file "

AND

"these files have identical names"

Then, either they do...or they don't.

As you have not really responded, I am not sure what I can suggest. To "display" a jpg can mean:

1. insert the jpg image onto the userform

2. display it in some other application

I have no idea which you are trying to do.

Inserting an image onto a userform is fairly easy, but inserting nicely can be tricky.

Use a Image control, and I would recoomend you set AutoSize = True. Why? Otherwise you may end up with an image you only see part of. You do not mention what size the images are, or how big your userform is etc etc.

When I suggest telling us EXACTLY...I mean just that. Exactly. For example:

"Once the user has identified they have the correct file, I need a button the user presses that will insert the currently selected file in the list (Word doc)"

Now, if the purpose is to insert a signature...why are you inserting a .doc file, when you have an image file?

Perhaps you have a very good reason for doing that....but I do not know what that may be....exactly.

Is there some reason you are not using AutoText? You could have a bunch of AutoText (each a signature block), then the user simply selects one.

ABrown
04-07-2008, 09:59 AM
Thanks for your response, and apologies again for the ambiguity. The jpg is being displayed in an image box in the form next to the list, but I broke it down into smaller pieces of code which all fitted together today perfectly. Thanks for your time.

fumei
04-07-2008, 11:18 AM
As a consideration, could you post what you did?

Also, if this is Solved, could you please mark the thread as Solved. Thanks.

ABrown
04-07-2008, 11:46 PM
This is the final code I used for displaying as a jpg image in a picture window on the form the content of a folder containing Word docs which are listed in a listbox on the form. A button is used to either insert or create a new file depending on the type of document it is. This is a provision bank that will be used to store standard legal documents and signature blocks.

Thanks.

Public fPathSig
Private Sub cbChoice_Change()
Dim List As Variant
cmdInsert.Caption = "Create"
ListBox1.Clear
Select Case (cbChoice.Value)
Case "Signature Blocks"
fPathSig = "H:\New Templates\Forms\Provision Bank"
cmdInsert.Caption = "Insert"
Case "UK Forms"
fPathSig = "H:\New Templates\Forms\Forms"

End Select
UserForm3.Caption = "Provision Bank - Click the file you wish to " & cmdInsert.Caption
With Application.FileSearch
.NewSearch
.LookIn = fPathSig
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles

If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
For i = 1 To .FoundFiles.Count
UserForm3.ListBox1.AddItem Dir(.FoundFiles(i))
Next i
End If
ListBox1.ListIndex = 0
End With

End Sub
Private Sub cmdCancel_Click()
Unload Me
End Sub
Sub cmdInsert_click()
If cbChoice.Value = "Signature Blocks" Then
Selection.InsertFile FileName:=(fPathSig & "\" & ListBox1.Value)

Else

Documents.Add (fPathSig & "\" & ListBox1.Value)

End If
Unload Me
End Sub
Private Sub ListBox1_Change()
Dim i As Integer
If ListBox1.ListIndex = -1 Then Exit Sub
For i = ListBox1.ListCount - 1 To 0 Step -1
If ListBox1.Selected(i) = True Then
Image1.Picture = LoadPicture(fPathSig & "\jpg\" & ListBox1.Value & ".jpg")
End If
Next i
End Sub
Private Sub UserForm_Activate()
cbChoice.AddItem "Signature Blocks" 'ListIndex = 0
cbChoice.AddItem "UK Forms" 'ListIndex = 1
cbChoice.AddItem "US Forms" 'ListIndex = 2
cbChoice.AddItem "LMA Forms" 'ListIndex = 3
End Sub