![]() |
#1
|
|||
|
|||
![]()
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 = """ .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 |
#2
|
||||
|
||||
![]()
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 = """ .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 As coded, the macro includes a folder browser so you can choose the top folder to process.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
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! |
#4
|
||||
|
||||
![]()
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] |
#5
|
|||
|
|||
![]()
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!!
|
![]() |
Tags |
subfolders, vba macro |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
expert4knowledge | Word VBA | 5 | 07-10-2014 03:54 PM |
![]() |
patidallas22 | Word VBA | 2 | 03-09-2012 08:14 AM |
![]() |
Catalin.B | Excel Programming | 2 | 09-08-2011 11:37 PM |