Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 11-23-2022, 03:18 PM
macropod's Avatar
macropod macropod is offline Loop through all .doc documents in all folders and subfolder, convert, save as to a different folder Windows 10 Loop through all .doc documents in all folders and subfolder, convert, save as to a different folder Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,521
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFolds As String
 
Sub Main()
Application.ScreenUpdating = False
Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
TopLevelFolder = GetFolder
StrFolds = vbCr & TopLevelFolder
If FSO Is Nothing Then
  Set FSO = CreateObject("Scripting.FileSystemObject")
End If
'Get the sub-folder structure
Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
For Each aFolder In TheFolders
  RecurseWriteFolderName (aFolder)
Next
'Process the documents in each "Cannot Upload" folder
For i = 1 To UBound(Split(StrFolds, vbCr))
  If Split(StrFolds, "\")(UBound(Split(StrFolds, "\"))) = "Cannot Upload" Then
    'create the output folder, if necessary
    On Error Resume Next
    MkDir (CStr(Split(StrFolds, vbCr)(i)) & "\Converted Files")
    On Error GoTo 0
   'Convert the documents
    Call ConvertDocuments(CStr(Split(StrFolds, vbCr)(i)))
  End If
Next
Application.ScreenUpdating = True
End Sub

Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
  RecurseWriteFolderName (SubFolder)
Next
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Sub ConvertDocuments(oFolder As String)
Dim strFldr As String, strFile As String, wdDoc As Document
strFldr = oFolder: If strFldr = "" Then Exit Sub
strFile = Dir(strFldr & "\*.doc", vbNormal)
While strFile <> ""
  If Right(strFile, 4) = ".doc" Then
    Set wdDoc = Documents.Open(FileName:=strFldr & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=False, Visible:=False)
    With wdDoc
      'Save as docx or docm, depending on whether the file contains macros.
      If .HasVBProject = False Then
        .SaveAs2 FileName:=strFldr & "\Converted Files\" & .Name & "x", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      Else
        .SaveAs2 FileName:=strFldr & "\Converted Files\" & .Name & "m", FileFormat:=wdFormatXMLDocumentMacroEnabled, AddToRecentFiles:=False
      End If
      'close the document
      .Close False
    End With
    'Delete the old file
    Kill strFldr & "\" & strFile
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop through all .doc documents in all folders and subfolder, convert, save as to a different folder Loop Through all documents in a folder ballpoint Word VBA 10 08-15-2022 05:24 PM
Loop through all .doc documents in all folders and subfolder, convert, save as to a different folder loop through all documents in a folder, search and replace text, save document ONLY IF CHANGE MADE shpkmom Word VBA 1 08-15-2022 05:16 PM
Loop through all .doc documents in all folders and subfolder, convert, save as to a different folder Combine 2 word documents in subfolder and save on same location D1985 Word VBA 5 03-29-2022 01:39 PM
Office 2010 Can't Open Or Save Documents in My Documents Folder trippb Office 1 07-12-2013 07:29 AM
Add folder and subfolder to Favorite ying06 Outlook 0 03-30-2012 10:41 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft