images et commentaires dans userform

  • Initiateur de la discussion titin
  • Date de début
T

titin

Guest
bonjour à tous

je cherche à afficher dans un userform des images listées dans une listbox.
en même temps je voudrais afficher dans un Label un fichier .TXT du même nom que l'image et venant du même dossier.( c'est à dire créer un commentaire)
exemple:
aa.jpg
aa.txt
bb.jpg
bb.txt
dans le dossier images

voici la macro que j'utilise pour afficher l'image:

Dim i As Integer
Nom2 = Me.ListBox2.Value
Fichier = 'H:\\images' & '\\' & Nom2 & '.jpg'

If Dir(Fichier) ‹› '' Then
Me.Image1.Picture = LoadPicture(Fichier)
Else
Me.Image1.Picture = LoadPicture('C:\\monImageDeRemplacement.jpg')
End If

j'aimerais pouvoir y ajouter les codes permettant d'afficher le texte



merci pour votre aides
 
T

Thomas

Guest
Bonjour le Forum,

Je ne pense pas qu'il souhaite afficher le nom du fichier texte mais bien le texte qu'il contient

tu dois utiliser Open for input #1

voici un exemple :
Code:
Private Sub ComboBox1_Change()
afficher_image Me.ComboBox1.Text
lecture Me.ComboBox1.Text
End Sub

Private Sub UserForm_Activate()
recherche_image

End Sub
Sub recherche_image()
Application.FileSearch.NewSearch
Application.FileSearch.LookIn = ThisWorkbook.Path
Application.FileSearch.Filename = '.jpg'
Application.FileSearch.SearchSubFolders = False
Application.FileSearch.Execute
 MsgBox ThisWorkbook.Path & Len(ThisWorkbook.Path)
nbFiches = Application.FileSearch.FoundFiles.Count

Me.Caption = nbFiches & ' Fichier(s) ' & quoi & ' trouvé(s)'


 For i = 1 To nbFiches
Me.ComboBox1.AddItem Mid(Application.FileSearch.FoundFiles(i), Len(ThisWorkbook.Path) + 2, (Len(Application.FileSearch.FoundFiles(i)) - (Len(ThisWorkbook.Path) + 5)))

Next i
End Sub
Sub afficher_image(fichier As String)
Dim lect As String
lect = ThisWorkbook.Path & '\\' & fichier & '.jpg'
Me.Image1.Picture = LoadPicture(lect)
End Sub
Sub lecture(fichier As String)
On Error GoTo fin
Dim lect As String
Dim MyString As String
Dim MyString1 As String
lect = ThisWorkbook.Path & '\\' & fichier & '.txt'
Open lect For Input As #1
Do While Not EOF(1)    ' Effectue la boucle jusqu'à la fin du fichier.
    Input #1, MyString
MyString = MyString & Chr(10)
 MyString1 = MyString1 & MyString
Loop

Close #1
Me.Label1.Caption = MyString1
Exit Sub
fin:
MsgBox 'Pas de fichier texte pour cette image!!'
End Sub

Bien à vous
Thomas

PS: il faut mettre le fichier dans le meme dossier que les images(jpg) et les fichiers txt [file name=image_20060521185852.zip size=12267]http://www.excel-downloads.com/components/com_simpleboard/uploaded/files/image_20060521185852.zip[/file]
 

Pièces jointes

  • image_20060521185852.zip
    12 KB · Affichages: 72

Hervé

XLDnaute Barbatruc
bonjour titin, david, thomas, le forum

une autre syntaxe possible :


Dim i As Integer
Dim chemin As String, texte As String
Dim image As String
Dim fichiertxt As String
Dim lignetxt As String
Dim nom2 As String
Dim premier As Byte

chemin = 'h:\\\\\\\\images\\\\\\\\'
nom2 = Me.ListBox2.Value
image = chemin & nom2 & '.jpg'
fichiertxt = chemin & nom2 & '.txt'


Open fichiertxt For Input As #1
Do While Not EOF(1)
Input #1, lignetxt
       
If premier = 0 Then
                texte = lignetxt
                premier = 1
       
Else
                texte = texte & vbNewLine & lignetxt
       
End If
Loop
Close #1

If Not Dir(image) = '' Then
Me.Image1.Picture = LoadPicture(image)
Me.Label1 = texte
Else
Me.Image1.Picture = LoadPicture('C:monImageDeRemplacement.jpg')
Me.Label1 = ''
End If


salut
 
T

titin

Guest
bonjour à tous
merci Davidc57, Thomas et Hervé
oui c'est bien un texte différent pour chaque image en forme de commentaire que je cherchais à afficher

je vais utiliser le code d'hervé qui s'intègre bien à mon fichier d'origne.

merci à tous, merci Hervé
 
T

titin

Guest
bonjour à tous
merci Davidc57, Thomas et Hervé
oui c'est bien un texte différent pour chaque image en forme de commentaire que je cherchais à afficher

je vais utiliser le code d'hervé qui s'intègre bien à mon fichier d'origne.

merci à tous, merci Hervé
 
T

titin

Guest
re bonjour

je rencontre un problème avec les codes d'Hervé.
si je n'ai pas de fichier texte correspondant à une image le programme plante sur la ligne suivante:

Open fichiertxt For Input As #1

s'il y a un fichier texte tous ce passe bien
comment éviter cela à part d'éditer des fichiers texte vide pour chaque image

merci encore
 

Discussions similaires

Réponses
12
Affichages
526
Réponses
1
Affichages
341
Compte Supprimé 979
C

Statistiques des forums

Discussions
312 682
Messages
2 090 865
Membres
104 680
dernier inscrit
AvPi26