![]() |
|
|
|
#1
|
||||
|
||||
|
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 |
|
#2
|
|||
|
|||
|
Dear Graham,
the date is part of the text, not a field. The macros works as expected, thank you for help. ![]() One more question. How to modify the code to look for the second (third etc.) date in the text? Now the first date is copied to the name. |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Filename after splitting document
|
Julian | Word VBA | 5 | 12-08-2015 04:51 AM |
How can I save a Word Document as a PDF file with a merged field filename?
|
kp2009 | Word VBA | 5 | 08-27-2015 11:45 PM |
| Document Filename Generator for Referencing | john.adams | Word | 10 | 03-26-2013 12:14 AM |
Save Filename using Document Text
|
Knawl | Word | 11 | 10-10-2011 03:00 AM |
| Word: The document 'Filename' caused a serious error the last time ... | martincruise | Word | 0 | 02-25-2010 01:47 AM |