Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 03-01-2016, 04:11 PM
macropod's Avatar
macropod macropod is offline Macro to change/convert/delete txt files in folder+subfolders Windows 7 64bit Macro to change/convert/delete txt files in folder+subfolders Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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
Public FSO As Object 'a FileSystemObject
Public oFolder As Object 'the folder object
Public oSubFolder As Object 'the subfolders collection
Public oFiles As Object 'the files object
Public i As Long
 
Sub Main()
' Minimise screen flickering
Application.ScreenUpdating = False
Dim StrFolder As String
' Browse for the starting folder
StrFolder = GetTopFolder
If StrFolder = "" Then Exit Sub
' Search the top-level folder
Call GetFolder(StrFolder & "\")
' Search the subfolders for more files
Call SearchSubFolders(StrFolder)
' Return control of status bar to Word
Application.StatusBar = ""
' Restore screen updating
Application.ScreenUpdating = True
MsgBox i & " files processed.", vbOKOnly
End Sub
 
Function GetTopFolder() As String
GetTopFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetTopFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
 
Sub SearchSubFolders(strStartPath As String)
If FSO Is Nothing Then
  Set FSO = CreateObject("scripting.filesystemobject")
End If
Set oFolder = FSO.GetFolder(strStartPath)
Set oSubFolder = oFolder.subfolders
For Each oFolder In oSubFolder
  Set oFiles = oFolder.Files
  ' Search the current folder
  Call GetFolder(oFolder.Path & "\")
  ' Call ourself to see if there are subfolders below
  SearchSubFolders oFolder.Path
Next
End Sub
 
Sub GetFolder(StrFolder As String)
Dim strFile As String
strFile = Dir(StrFolder & "*.txt")
' Process the files in the folder
While strFile <> ""
  ' Update the status bar is just to let us know where we are
  Application.StatusBar = StrFolder & strFile
  i = i + 1
  Call UpdateFile(StrFolder & strFile)
  Kill StrFolder & strFile
  strFile = Dir()
Wend
End Sub
 
Sub UpdateFile(strDoc As String)
Dim Doc As Document
' Open the document
Set Doc = Documents.Open(strDoc, AddToRecentFiles:=False, ReadOnly:=False, Format:=wdOpenFormatAuto, Visible:=False)
With Doc
  With .Range
    .Font.Name = "Calibri"
    .Font.Size = 12
    With .Find
      .Text = "</p></li> </ul> <p>"
      .Replacement.Text = "^p^p"
      .Execute Replace:=wdReplaceAll
      .Text = "</p></li> <li><p>"
      .Execute Replace:=wdReplaceAll
      .Text = "</p> <ul> <li><p>"
      .Execute Replace:=wdReplaceAll
      .Text = "</li> </ul> <p>"
      .Execute Replace:=wdReplaceAll
      .Text = "</p> <ul> <li>"
      .Execute Replace:=wdReplaceAll
      .Text = "</p> </div>"
      .Replacement.Text = " "
      .Text = "</p> <p>"
      .Replacement.Text = "^p^p"
      .Execute Replace:=wdReplaceAll
      .Text = "</p>"
      .Execute Replace:=wdReplaceAll
      .Text = "</em>"
      .Replacement.Text = ""
      .Execute Replace:=wdReplaceAll
      .Text = "<em>"
      .Execute Replace:=wdReplaceAll
      .Text = "</strong>"
      .Execute Replace:=wdReplaceAll
      .Text = "<strong>"
      .Execute Replace:=wdReplaceAll
      .Text = "<a"
      .Execute Replace:=wdReplaceAll
      .Text = "</a></strong>"
      .Execute Replace:=wdReplaceAll
      .Text = "href=*>"
      .Execute Replace:=wdReplaceAll
      .Text = "<div class=""md""><p>"
      .Replacement.Text = "^p^p"
      .Execute Replace:=wdReplaceAll
      .Text = "'"
      .Replacement.Text = "'"
      .Execute Replace:=wdReplaceAll
      .Text = "&quot;"
      .Replacement.Text = "”"
      .Execute Replace:=wdReplaceAll
    End With
  End With
  .SaveAs FileName:=Split(strDoc, ".txt")(0) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
  .Close SaveChanges:=True
End With
' Let Word do its housekeeping
DoEvents
Set Doc = Nothing
End Sub
I have some doubts about your process, though. One of your Find/Replace finds is to replace all instances of "</a></strong>" with nothing. However, since you've already replaced all instances of "</strong>" with nothing, this latter find will never locate anything. Similarly, you have another Find/Replace that just replaces "'" with itself. I've left both in the code, but you might need to reconsider your Find/Replace sequencing for the first one and its meaningfulness for the second one.

As coded, the macro includes a folder browser so you can choose the top folder to process.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 

Tags
subfolders, vba macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to change/convert/delete txt files in folder+subfolders Macro to change all text color to black in all docx files in a selected folder joewoods Word VBA 13 05-16-2016 06:29 PM
Macro to replace SSN in all files within a folder caj1980 Word VBA 7 09-11-2014 04:17 PM
Macro to change/convert/delete txt files in folder+subfolders macro to change name of files in a folder in order expert4knowledge Word VBA 5 07-10-2014 03:54 PM
Macro to change/convert/delete txt files in folder+subfolders Word Macro - change date in footer for all files in a folder patidallas22 Word VBA 2 03-09-2012 08:14 AM
Macro to change/convert/delete txt files in folder+subfolders Macro to loop in subfolders, change links, export xml data Catalin.B Excel Programming 2 09-08-2011 11:37 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:37 AM.


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