Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
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
  #2  
Old 07-30-2018, 02:51 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,457
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

See the basic code for processing a single folder, in: http://www.msofficeforums.com/word-v...html#post45854
and the modifications needed to loop through sub-folders, in: http://www.msofficeforums.com/word-v...html#post47785
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

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


All times are GMT -7. The time now is 12:46 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft