For a simple replacement of the last day of the month with text of your choice, you could use code like:
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document, StrRep As String
strFolder = GetFolder
If strFolder = "" Then Exit Sub
StrRep = InputBox("What is the replacement text?")
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
With .Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Replacement.Text = StrRep
.Text = "[ADJMO][abceghlmnorstuy]{2,7} 31"
.Execute Replace:=wdReplaceAll
.Text = "February 2[89]"
.Execute Replace:=wdReplaceAll
.Text = "[AJSN][beilmnoprtuv]{4,8} 30"
.Execute Replace:=wdReplaceAll
End With
.ExportAsFixedFormat OutputFileName:="Somefolder\" & Left(strFile, InStrRev(strFile, ".")) & "pdf", _
ExportFormat:=wdExportFormatPDF, OptimizeFor:=wdExportOptimizeForPrint
.Close SaveChanges:=False
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
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
The macro includes its own folder browser, so all you need to do is to select the folder to be processed. All the processed documents be saved as PDFs in that folder, without changing the originals.
However, you now say:
Quote:
I am trying to replace the whatever date is on the document with the last day of the previous month
|
so it's not clear whether you're trying to replace whatever:
(a) month ends; or
(b) dates anywhere within a month,
the code finds with whatever happens to be the last day of the:
(c) preceding month; or
(d) month prior to when the code is run.
It's quite easy to code for either (a) or (b) and for (d), but (c) would require some more elaborate code.
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab. That way, your code can be displayed with a proper structure, as per the above.