View Single Post
 
Old 03-08-2017, 10:44 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Is the date inserted with a field - if so which. Is it the only date in the document. Is it on its own or scattered somewhere willy nilly in the document. You are not making this any easier.

The following may work for you. It should find the first date in the format dd.MM.yyyy

Code:
Option Explicit

Sub MySave()
'Graham Mayor - http://www.gmayor.com - Last updated - 09/03/2017
Dim oStory As Range
Dim strDate As String
Dim strName As String
    For Each oStory In ActiveDocument.StoryRanges
        strDate = FindDate(oStory)
        If Not strDate = "" Then Exit For
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                strDate = FindDate(oStory)
                If Not strDate = "" Then Exit For
            Wend
        End If
    Next oStory
    If strDate = "" Then
        MsgBox "Date not found"
        GoTo lbl_Exit
    End If
    strName = strDate & Chr(32) & ActiveDocument.Name
    ActiveDocument.SaveAs2 strName, 12
    Set oStory = Nothing
lbl_Exit:
    Exit Sub
End Sub

Function FindDate(oRng As Range) As String
'Graham Mayor - http://www.gmayor.com - Last updated - 09/03/2017
    With oRng.Find
        Do While .Execute(FindText:="[0-9]{2}.[0-9]{2}.[0-9]{4}", _
                          MatchWildcards:=True)
            FindDate = oRng.Text
            Exit Do
        Loop
    End With
lbl_Exit:
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote