Option Explicit Private Sub CommandButton1_Click() Dim chemindestination, cheminfichier As String 'Declare a variable as a FileDialog object. Dim fd As FileDialog 'Create a FileDialog object as a File Picker dialog. Set fd = Application.FileDialog(msoFileDialogFilePicker) 'Declare a variable to contain the path 'of each selected item. Even though the path is a String, 'the variable must be a Variant because For Each...Next 'routines only work with Variants and Objects. Dim vrtSelectedItem As Variant 'Use a With...End With block to reference the FileDialog object. With fd 'Add a filter that includes GIF and JPEG images and make it the first item in the list. .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1 .Title = "Choisissez une image" .InitialFileName = "C:\Mes Documents\Mes images" .InitialView = msoFileDialogViewThumbnail 'afficher les miniatures 'Use the Show method to display the File Picker dialog box and return the user's action. 'If the user presses the action button... If .Show = -1 Then 'Step through each string in the FileDialogSelectedItems collection. For Each vrtSelectedItem In .SelectedItems 'vrtSelectedItem is a String that contains the path of each selected item. 'You can use any file I/O functions that you want to work with this path. 'This example simply displays the path in a message box. cheminfichier = vrtSelectedItem Next End If End With 'Set the object variable to Nothing. Set fd = Nothing If cheminfichier = "" Then Exit Sub 'affiche l'image Image1.Picture = LoadPicture(cheminfichier) chemindestination = "C:\chemin du dossier destination\monimage.jpg" 'sauvegarde l'image SavePicture Image1.Picture, chemindestination 'coller la macro du lien hypertexte End Sub Private Sub UserForm_Initialize() Image1.PictureSizeMode = fmPictureSizeModeZoom End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionIf .Show = -1 Then 'Si je comprend bien ici la condition indique la fermeture de la fenetre 'Step through each string in the FileDialogSelectedItems collection. For Each vrtSelectedItem In .SelectedItems 'vrtSelectedItem is a String that contains the path of each selected item. 'You can use any file I/O functions that you want to work with this path. 'This example simply displays the path in a message box. cheminfichier = vrtSelectedItem 'ici pour chaque image selectionnée on définit a la variable cheminfichier ' le path complet du fichier (meme si il y en a qu'un seul a chaque fois) Next End If End With 'Set the object variable to Nothing. Set fd = Nothing ' je comprend pas pk on le vide? If cheminfichier = "" Then Exit Sub 'si jamais on selectionne rien on stop la procedure 'affiche l'image Image1.Picture = LoadPicture(cheminfichier) ' je comprend pas ce que c'est que Image1.Picture 'j'ai déclarer Image1 comme un HeaderFooter (c'est ce que VBA me demandait de déclarer mais je ne sais pas ce que c'est) ' j'ai essayer de le déclarer en object, picture mais il me met: "object variable or withblock not set (run-time erreur 91) ' et si j'ai bien compris, on assigne l'image de cheminfichier dans Image1? chemindestination = "C:\chemin du dossier destination\monimage.jpg" 'sauvegarde l'image SavePicture Image1.Picture, chemindestination ' la ca va :D 'coller la macro du lien hypertexte End Sub Private Sub UserForm_Initialize() Image1.PictureSizeMode = fmPictureSizeModeZoom End Sub
Sub LoadImage() 'Declare a variable as a FileDialog object. Dim fd As FileDialog 'Create a FileDialog object as a File Picker dialog. Set fd = Application.FileDialog(msoFileDialogFilePicker) 'Declare a variable to contain the path must be a variant because For each...next Dim vrtSelectedItem As Variant 'Use a With...End With block to reference the FileDialog object. With fd 'Add a filter that includes GIF and JPEG images and make it the first item in the list. .Filters.Clear .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1 .Title = "Choisissez une image" .InitialFileName = "C:\Mes Documents\Mes images\*.*" .InitialView = msoFileDialogViewThumbnail 'afficher les miniatures 'Use the Show method to display the File Picker dialog box and return the user's action. 'If the user presses the action button... If .Show = -1 Then 'Step through each string in the FileDialogSelectedItems collection. For Each vrtSelectedItem In .SelectedItems 'vrtSelectedItem is a String that contains the path of each selected item. cheminfichier = vrtSelectedItem 'extraire nom du fichier seul Dim posit Do posit = InStr(1, cheminfichier, "") cheminfichier = Right(cheminfichier, Len(cheminfichier) - posit) Loop Until posit = 0 nomDisplay = cheminfichier Next vrtSelectedItem Else 'If the user presses Cancel... 'MsgBox "Opération annulée par l'utilisateur", vbCritical, "Ouverture fichier" Exit Sub End If End With 'Set the object variable to Nothing. Set fd = Nothing If cheminfichier = "" Then Exit Sub 'affiche l'image ProjetConta.Image1.Picture = LoadPicture(cheminfichier) DateFormaté = Format(Date, "dd-mm-yy") chemindestination = ThisWorkbook.Path & "\Image\F" & ProjetConta.N°Four.Value & " " & DateFormaté & ".bmp" fichiersansext = Left(nomDisplay, Len(nomDisplay) - 4) End Sub
oClasseur.Sheets("Data").Hyperlinks.Add Anchor:=Cells_ (3, 14), Address:=chemindestination, TextToDisplay:=fichiersansext 'sauvegarde l'image SavePicture ProjetConta.Image1.Picture, chemindestination
C'est normal que l'ouverture ne fonctionne pas si tu as des images .bmp. Mets ceci:
.Filters.Add "Images", "*.bmp; *.gif; *.jpg; *.jpeg", 1
cheminfichier = Application.GetOpenFilename( _ "Fichiers Image (*.jpg;*.gif;*.png;*.tif;*.bmp),*.jpg;*.gif;*.png;*.tif;*.bmp") If cheminfichier = False Then Exit Sub Filtre = Right(cheminfichier, 4)
.InitialFileName = "C:\Mes Documents\Mes images"
.InitialFileName = "C:\Mes Documents\Mes images"
'Mettre la propriété de l'UserForm1.ShowModal = False Option Explicit Dim chemindestination, cheminfichier, nomfichier, fichiersansext As String Private Sub CommandButton1_Click() 'Declare a variable as a FileDialog object. Dim fd As FileDialog 'Create a FileDialog object as a File Picker dialog. Set fd = Application.FileDialog(msoFileDialogFilePicker) 'Declare a variable to contain the path 'of each selected item. Even though the path is a String, 'the variable must be a Variant because For Each...Next 'routines only work with Variants and Objects. Dim vrtSelectedItem As Variant 'Use a With...End With block to reference the FileDialog object. With fd 'Add a filter that includes GIF and JPEG images and make it the first item in the list. .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1 .Title = "Choisissez une image" .InitialFileName = "C:\Mes Documents\Mes images" .InitialView = msoFileDialogViewThumbnail 'afficher les miniatures 'Use the Show method to display the File Picker dialog box and return the user's action. 'If the user presses the action button... If .Show = -1 Then 'Step through each string in the FileDialogSelectedItems collection. For Each vrtSelectedItem In .SelectedItems 'vrtSelectedItem is a String that contains the path of each selected item. 'You can use any file I/O functions that you want to work with this path. 'This example simply displays the path in a message box. cheminfichier = vrtSelectedItem 'extraire nom du fichier seul Dim posit Do posit = InStr(1, cheminfichier, "") cheminfichier = Right(cheminfichier, Len(cheminfichier) - posit) Loop Until posit = 0 nomfichier = cheminfichier Next vrtSelectedItem Else 'If the user presses Cancel... MsgBox "Opération annulée par l'utilisateur", vbCritical, "Ouverture fichier" Exit Sub End If End With 'Set the object variable to Nothing. Set fd = Nothing If cheminfichier = "" Then Exit Sub 'affiche l'image Image1.Picture = LoadPicture(cheminfichier) chemindestination = ThisWorkbook.Path & "\Nouveau dossier" & nomfichier 'sauvegarde l'image SavePicture Image1.Picture, chemindestination 'coller la macro du lien hypertexte fichiersansext = Left(nomfichier, Len(nomfichier) - 4) ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=chemindestination _ , TextToDisplay:=fichiersansext End Sub Private Sub UserForm_Initialize() MsgBox "Sélectionnez la cellule pour recevoir le lien Hypertexte", vbInformation, "Lien Hypertexte" Image1.PictureSizeMode = fmPictureSizeModeZoom Image1.BorderStyle = fmBorderStyleNone CommandButton1.Caption = "Ouvrir image" End Sub