Sauver ac un nom de fichier variable (Débutant!)

Résolu
jule29 Messages postés 10 Date d'inscription dimanche 17 février 2008 Statut Membre Dernière intervention 23 novembre 2008 - 8 avril 2008 à 17:16
jule29 Messages postés 10 Date d'inscription dimanche 17 février 2008 Statut Membre Dernière intervention 23 novembre 2008 - 8 avril 2008 à 22:17
Bonjour,

J'utilise le VBA sous excel pour un probleme d'optimisation. je genere des formes de maniere aléatoire (en utisant l'agorithme d'halton) et je les test en les exportant sur un autre logiciel.
Je voudrais sauver chacun de mes fichiers exportés ac un nom s'incrementant a partir de la valeur d'une cellule. la fin de mon code et de cette forme:

msApp.Design.SaveAs "C:\Documents and Settings\moi\Mes documents\Project\Formes\Forme.msd", True

Je veux donc obtenir des fichiers ayant un nom du type Forme1.msd; Forme2.msd....

J'espere ne pas m'etre trop mal expliqué!

Merci de bien vouloir m'aider!

2 réponses

bigfish_le vrai Messages postés 1835 Date d'inscription vendredi 13 mai 2005 Statut Membre Dernière intervention 20 novembre 2013 15
8 avril 2008 à 21:59
Salut,

voici un bout de code que j'utilise deja et que j'ai adapté a ton besoin.
ce code cherche dans le repertoire specifié le nom du fichier qui contient le numero le plus grand puis utilise ce
numero + 1 pour creer un nouveau nom.

Sub NouveauFichier()
    Dim PartFileName As String, FolderPath As String, i As Long
    Dim NbFiles As Long, FileNumber As Long, NomFichier As String
    'get the path of the files that we need
    FolderPath = "C:\Documents and Settings\moi\Mes documents\Project\Formes"
    If Right(FolderPath, 1) <> "" Then FolderPath = FolderPath & "" 'ne sert a rien ici mais si tu veux definir le chemin d'une autre facon, comme par exemple via une cellule, il faudra s'assurer que ce chemin ce termine toujours par un \.
    'file name structure
    PartFileName = "Forme"
    Extention = ".msd"
    ' search for the files
    FileNumber = 0
    On Error Resume Next
    With Application.FileSearch
        .NewSearch
        .LookIn = FolderPath 'Look in the path
        .FileName = PartFileName & "*" & Extention 'all files that contain this string will be selected
        .SearchSubFolders = False
        .Execute
        NbFiles = .FoundFiles.Count 'how many file ?
        If NbFiles = 0 Then 'if finally no file found
            MsgBox "Cannot find any file. Make sure the path is correct. ", vbCritical, "Files error..."
            End
        End If
        For i = 1 To NbFiles 'for each file found
            NomFichier = Replace(LCase(.FoundFiles(i)), LCase(PartFileName), "")
            NomFichier = Replace(LCase(NomFichier), Extention, "")
            If CLng(NomFichier) > FileNumber Then FileNumber = CLng(NomFichier) + 1
        Next i
    End With
    NomFichier = PartFileName & FileNumber & Extention
    MsgBox NomFichier
End Sub

Vala

A+
3
jule29 Messages postés 10 Date d'inscription dimanche 17 février 2008 Statut Membre Dernière intervention 23 novembre 2008
8 avril 2008 à 22:17
Parfait tout ça!
Merci beaucoup!
0
Rejoignez-nous