#1
|
|||
|
|||
look for a specific word in document and copy it to the filename
Hello,
Please, help with a macros which could look for a specific word in document, let's say a date in format dd.mm.yyyy, add it to the old filename and save the document. |
#2
|
|||
|
|||
We'll need more information. Where in the document is this date located? Where do you want to save the document to? In what format, .doc? .pdf? Do you want the date added to the end of the original file name?
Also, I believe that those periods in the date will affect saving the document (or make the solution much more tedious). Would you be okay with the format of ddmmyyyy instead of dd.mm.yyyy? |
#3
|
|||
|
|||
The date is located somewhere in document, each time in another place. So the macros should look for it, and once found generate new name and replace the old document. If nothing is found the document should retain its original name, of course. Yes, format ddmmyyyy is ok.
|
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
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 |