excelguigui11
excelguigui11

Reputation: 313

Excel vba can't release winword.exe

I'm opening multiples Word doc to remove the protection in them. So basically what my code is doing is that if user choose to enter a password then: valeur = 6 and pwd is set to a specific value. My problem is even if I do: set WdApp = nothing, it still won't release the winword.exe process.

Do While Len(F) > 0
    If F = nom_fichier Then                     'file  name 
        nom_original = chemin & "\" & F                     'path
        nom_copie = repertoire_cible & "\" & nom_fichier                'copy of a file
        nom_modifie = repertoire_cible & "\" & nom_en_preparation       'rename of file
        If Dir(nom_original, vbDirectory) = vbNullString Then           
            GoTo fichier_non_trouve
        End If
        If Not Dir(nom_modifie, vbDirectory) = vbNullString Then                                GoTo fichier_deja_existant:
        End If
        FileCopy nom_original, nom_copie      'file copy
        Name nom_copie As nom_modifie         ' file rename
        If valeur = 6 Then                      'protection removal
           Set WdApp = CreateObject("Word.Application")
           Set WdApp = Documents.Open(nom_modifie)
           If Not WdApp.ProtectionType = -1 Then                                            WdApp.Unprotect pwd
                WdApp.Close True
           Else
               WdApp.Close True
           End If

           Set WdApp = Nothing
        End If
        GoTo fichier_copier:
    End If                                   'on copie le fichier dans le dossier en préparation
    F = Dir()
Loop

So at this point since I called Set WdApp = nothing I don't see why it's not releasing the winword.exe. Any kind help will be really appreciate

Upvotes: 1

Views: 142

Answers (1)

Alex K.
Alex K.

Reputation: 175766

Keep a reference to the application and the doc independently and .Quit the application:

Set WdApp = CreateObject("Word.Application")
Set WdDoc = Documents.Open(nom_modifie)

 ... use wdDoc

WdDoc.close True
WdApp.Quit

But why not create WdApp outside the loop and open docs as needed, quitting the application when your done.

Upvotes: 1

Related Questions