Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-08-2017, 02:42 PM
sylvio sylvio is offline look for a specific word in document and copy it to the filename Windows 7 64bit look for a specific word in document and copy it to the filename Office 2010 32bit
Novice
look for a specific word in document and copy it to the filename
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 03-08-2017, 02:46 PM
dwirony dwirony is offline look for a specific word in document and copy it to the filename Windows 7 64bit look for a specific word in document and copy it to the filename Office 2003
Advanced Beginner
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default

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?
Reply With Quote
  #3  
Old 03-08-2017, 02:52 PM
sylvio sylvio is offline look for a specific word in document and copy it to the filename Windows 7 64bit look for a specific word in document and copy it to the filename Office 2010 32bit
Novice
look for a specific word in document and copy it to the filename
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 03-08-2017, 10:44 PM
gmayor's Avatar
gmayor gmayor is offline look for a specific word in document and copy it to the filename Windows 10 look for a specific word in document and copy it to the filename Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 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
  #5  
Old 03-09-2017, 05:53 PM
sylvio sylvio is offline look for a specific word in document and copy it to the filename Windows 7 64bit look for a specific word in document and copy it to the filename Office 2010 32bit
Novice
look for a specific word in document and copy it to the filename
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default

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.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
look for a specific word in document and copy it to the filename Filename after splitting document Julian Word VBA 5 12-08-2015 04:51 AM
look for a specific word in document and copy it to the filename 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
look for a specific word in document and copy it to the filename 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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:14 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft