#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
See the basic code for processing a single folder, in: https://www.msofficeforums.com/word-...html#post45854
and the modifications needed to loop through sub-folders, in: https://www.msofficeforums.com/word-...html#post47785
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Extend Unlink Macro to reach documents in subfolders | Rsloo | Word VBA | 3 | 06-14-2017 08:15 PM |
Macro to change/convert/delete txt files in folder+subfolders | NoS | Word VBA | 4 | 03-03-2016 12:10 PM |
How to open Documents folder directly from CTRL+O of Open folder on QAT | scvjudy | Word | 2 | 08-11-2014 10:58 PM |
Microsoft Word 2013 Documents open as full version documents on two computers sporadically | Jaydenc-Fortress | Word | 2 | 08-07-2014 12:34 AM |
Office 2010 Can't Open Or Save Documents in My Documents Folder | trippb | Office | 1 | 07-12-2013 07:29 AM |