View Single Post
 
Old 07-30-2018, 02:20 AM
shu shu is offline Windows 10 Office 2016
Novice
 
Join Date: Jun 2018
Posts: 6
shu is on a distinguished road
Default open word documents in folder and subfolders

I have a userform where i can choose the desired folder. Then i try with the following script to run through the folder and subfolders, open the word docs, perform the procedure and close them after. However, I always receive the message "Document may be damaged" and i don't know why. Can someone pls help me?

BR shu


Sub BrowseDocuments()

Dim fsob As Object
Dim Verz As Object
Dim Datei As Object
Dim Folder As Object
Dim Subfolder As Object
Dim File As Object
Dim Dateien As Object
Dim Worddatei As Word.Document

'desired Folder
Set fsob = CreateObject("scripting.filesystemobject")
Set Verz = fsob.GetFolder(Ort)
Set Dateien = Verz.Files

'open files in Folder
For Each Datei In Dateien
If InStr(1, Datei.Path, "doc") Then
Set Worddatei = Documents.Open(Datei.Path)
NameWorddatei = Worddatei.Name
Debug.Print NameWorddatei
Call Main
Worddatei.Close SaveChanges:=wdSaveChanges
End If
Next

'run through Subfolder
For Each Folder In Verz.Subfolders
Set Subfolder = fsob.GetFolder(Folder.Path)

'open files in Subfolder
For Each File In Subfolder.Files
If InStr(1, File.Path, "doc") Then
Set Worddatei = Documents.Open(File.Path)
NameWorddatei = Worddatei.Name
Debug.Print NameWorddatei
Call Main
Worddatei.Close SaveChanges:=wdSaveChanges
End If
Next File
Next Folder

UserForm1.dp_st.Value = "Procedure is completed - Press the close button"

End Sub
Reply With Quote