Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-01-2016, 03:31 PM
NoS NoS 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 2013
Novice
Macro to change/convert/delete txt files in folder+subfolders
 
Join Date: Mar 2016
Posts: 3
NoS is on a distinguished road
Default Macro to change/convert/delete txt files in folder+subfolders

Hello,

I´m trying to create a Macro in Word 2013, but I am completely new to this, so I am hoping somebody can help me.
I have a folder with many subfolders, which are filled with txt-files on a regular basis that have to be formatted in a certain way, saved as pdf and then deleted afterwards.

So, the basic steps of the Macro are:
1. Replace various text parts with predefined alternative words
2. Change the font size to 12, font to Calibri and save as pdf afterwards
3. Delete the txt-files, after they have been saved as pdf

So far, I have been able to collect some code through googling, which allows me to do all these things (in two different macros) but only for the top folder.

So I am hoping that somebody could maybe restructure the code, to have it include the txt-files in the subfolders for all these steps and possibly make one "big" macro out of it, instead of two.

Thank you!!

Kind regards,

NoS


Sub Test()
'
' Test Makro
'
'
Dim File
Dim path As String
path = "C:\Test\"
' note the "\"

File = Dir(path & "*.txt")
Do While File <> ""
Documents.Open FileName:=path & File
With ActiveDocument

With .Range.Find
.Text = "</p></li> </ul> <p>"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll


End With

With .Range.Find
.Text = "</p></li> <li><p>"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</p> <ul> <li><p>"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</li> </ul> <p>"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</p> <ul> <li>"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</p> </div>"
.Replacement.Text = " "
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</p> <p>"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</p>"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</em>"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "<em>"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</strong>"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "<strong>"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "<a"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "</a></strong>"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "href=*>"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "<div class=""md""><p>"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "'"
.Replacement.Text = "'"
.Execute Replace:=wdReplaceAll
End With

With .Range.Find
.Text = "&quot;"
.Replacement.Text = "”"
.Execute Replace:=wdReplaceAll
End With



'Following code changes the font size to 12, the font to Calibri, the extension of the opened txt file to pdf, without displaying the pdf
Selection.WholeStory
Selection.Font.Size = 12
Selection.Font.Name = "Calibri"
ActiveDocument.Save
pdfFileName = ActiveDocument.FullName ' get the current file name
lengthFileName = Len(pdfFileName) ' get the length of the current file name (includes the path)
pdfFileName = Left(pdfFileName, lengthFileName - 3) + "pdf" ' changes the extension (shave off three characters and add three new ones.)


ActiveDocument.ExportAsFixedFormat OutputFileName:=pdfFileName, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
.Close
End With
File = Dir()
Loop
End Sub










Sub Delete_txt_files()
'Be sure that no file is open in the folder
Dim FSO As Object
Dim MyPath As String
Dim File As Object
Dim Folder As Object
Dim SubFolder As Object

Set FSO = CreateObject("scripting.filesystemobject")

MyPath = "C:\Test\" '<< Change

If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If

If FSO.FolderExists(MyPath) = False Then
MsgBox MyPath & " doesn't exist"
Exit Sub
End If

On Error Resume Next
'Delete files
FSO.deletefile MyPath & "\*.txt", True


End Sub
'Option Explicit
Reply With Quote
  #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: 21,962
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
  #3  
Old 03-02-2016, 12:34 PM
NoS NoS 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 2013
Novice
Macro to change/convert/delete txt files in folder+subfolders
 
Join Date: Mar 2016
Posts: 3
NoS is on a distinguished road
Default

Thank you very much, macropod!
As for the first find/replace problem you found, I think you are correct and something got mixed up. I´ll look into it.
As for replacing "'" with itself, it seems that by copying the code here, the forum changed the html-way of writing "'", which I have in the txt files, automatically to the correct way of displaying the character, which is basically the same thing I am doing in the txt files. So it may seem unnecessary but at least to this one there is some logic

As for the code, there still seems to be a problem. I apologize if I did not understand correctly or if am being stupid here, but if I paste it into the VBA editor and then run sub "Main", I get to the part, where it asks me for the top folder. After selecting it, I get a runtime error 424 "object required" though. Do you have any idea why?

Also, could you change the popup asking to select the location to a fixed one in the code? I understand that your solution is much more elegant but the folder will never change and thus I can save a few more clicks during the day.

Once again, thank you for your help!
Reply With Quote
  #4  
Old 03-02-2016, 02:13 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: 21,962
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

I don't get the a runtime error 424 "object required". Perhaps you made an error in copying the code.

To skip the folder browser, replace:
Dim StrFolder As String
with:
Const StrFolder As String = "C:\Test"
and delete:
' Browse for the starting folder
StrFolder = GetTopFolder
If StrFolder = "" Then Exit Sub

You can also delete the GetTopFolder function.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-03-2016, 12:10 PM
NoS NoS 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 2013
Novice
Macro to change/convert/delete txt files in folder+subfolders
 
Join Date: Mar 2016
Posts: 3
NoS is on a distinguished road
Default

So, I figured out what I did wrong. I´d created a new Macro and then copied your code in there, however I had to post it under a new Module instead to work. Once I´d done that, it worked like a charm! Once again, thank you very much!!
Reply With Quote
Reply

Tags
subfolders, vba macro

Thread Tools
Display Modes


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 06:15 PM.


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