Jump to content
WeardeQuay

Search document for today's date

Recommended Posts

I am trying to make a macro that will open a document and put the curser on today's date, I thought that would be an easy first macro for me to write but it has not worked out that way. I am attempting to purchase a book dealing with as it's the reason for the macro a complete macro guide for Office 365, has anyone heard of one.Alan

Share this post


Link to post
Share on other sites

This is as far as I can get with the macro, I do not understand the find a string part. Can anyone help.

 

Sub Diary2016()'' Diary2016 Macro'' ChangeFileOpenDirectory "X:WORD Documents" Documents.Open FileName:="X:WORD DocumentsDiary 2016.docx", _ ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _ PasswordDocument:="", PasswordTemplate:="", Revert:=False, _ WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _ wdOpenFormatAuto, XMLTransform:=""

 

Dim ToDaysDate As String

ToDaysDate = "& Day(Now) & Month(Now) & Year(Now)"

Find: Selection.Find.ClearFormatting With Selection.Find .Text = "ToDaysDate" .Replacement.Text = " " .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute

End Sub

Share this post


Link to post
Share on other sites

First, this line:

ToDaysDate = "& Day(Now) & Month(Now) & Year(Now)"
should be this:
ToDaysDate = Day(Now) & Month(Now) & Year(Now)
That would make a date string like "25032015" for today. If your date is in a different format then you'll need to adjust that string. And on this line:
.Text = "ToDaysDate"
You shouldn't quote the variable name.

Share this post


Link to post
Share on other sites

At last the code works thanks for the help with the date formatting.

 

Sub Dairy2015()'' Dairy2015 Macro ChangeFileOpenDirectory "X:WORD Documents" Documents.Open FileName:="X:WORD DocumentsDiary 2015.docx", _ ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _ PasswordDocument:="", PasswordTemplate:="", Revert:=False, _ WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _ wdOpenFormatAuto, XMLTransform:="" DaysDate = Format(Now(), "Long Date")

Find: Selection.Find.ClearFormatting With Selection.Find .Text = DaysDate .Replacement.Text = " " .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute End Sub

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×