Create a new archive file If Len(Dir(ZipFile)) > 0 Then Kill ZipFile' deletes the archive if it already exists Open FileZip For Output As #1 Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0) Close #1
'Moves the file to be archived to the archive Set ApplicationArchiving = CreateObject("Shell.Application") ApplicationArchive.Namespace(ZipFile). MoveHere FileAArchiver
you are missing the reference "Microsoft Shell Controls And Automation". ? But it's difficult to diagnose your problem without declaring your variables or without the error code....
I tried this code (clever Excel) which works perfectly (including with MoveHere instead of CopyHere:
Sub ArchiverUnFichier()
'par Excel-Malin.com ( https://excel-malin.com )
'---------------------------------------------------------
'gestion des erreurs
On Error GoTo ErreurCompression
'définition des variables
Dim ApplicationArchivage As Object
Dim FichierAArchiver, FichierZip
'informations sur les fichiers (chemins & noms)
FichierAArchiver = "C:\Test\MonFichierWord.docx"
FichierZip = "C:\Test\Archives\MonArchive_1.zip"
'créer un nouveau archive
If Len(Dir(FichierZip)) > 0 Then Kill FichierZip 'supprime l'archive s'il existe déjà
Open FichierZip For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
'copier le fichier à archiver dans l'archive
Set ApplicationArchivage = CreateObject("Shell.Application")
ApplicationArchivage.Namespace(FichierZip).CopyHere FichierAArchiver
'Message final
MsgBox "L'archivage a été lancé..."
Exit Sub
ErreurCompression:
MsgBox "Une erreur s'est produite..."
End Sub
Wouldn't it be your variable ZipFile that has an inappropriate character for a path? Try with C:\Temp\Essai.zip for example.
Whether or not extensions are displayed should not influence the path of a file. It seems strange to me.
If for some obscure reason this is really the case, you have to see if it is possible to test the display state of the extensions from the script and if it is hidden, switch it to visible for the time it is executed.
I found the origin of the problem. The VBA is too fast compared to Winzip. The files don't have time to finish being zipped before another one arrives.
So I placed a messagebox with yes/no so that the user gives the start for the next file.
I could also make a verification condition.
Otherwise solution make a single command line with several files but I don't know how to do it ( FileAArchiverSTEP = the file . WWTP )
I created another variable FileAArchiverDWG, FileAArchiverDXF for each file and each line sends it to the ZIP
Thank you for your link. In this example, it ZIPS the folder. If I change the beginning of my program to create a folder with the DXF, DXG and STEP, it can work:)
Sub testing()
Dim ZipFile As String
Dim FolderToAdd As String
Dim objShell As Object
Dim varZipFile As Variant
ZipFile = "C:\ZipFile_Images\images.zip"
FolderToAdd = "C:\Images"
Set objShell = CreateObject("Shell.Application")
varZipFile = ZipFile
If Right$(FolderToAdd, 1) <> "\" Then
FolderToAdd = FolderToAdd & "\"
End If
objShell.NameSpace(varZipFile).CopyHere (FolderToAdd)
End Sub