Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-24-2022, 12:14 PM
Stephen Ray Stephen Ray is offline Range For Last Line/Paragraph - To Find Date Windows 10 Range For Last Line/Paragraph - To Find Date Office 2016
Advanced Beginner
Range For Last Line/Paragraph - To Find Date
 
Join Date: Sep 2018
Location: Kansas
Posts: 34
Stephen Ray is on a distinguished road
Default Range For Last Line/Paragraph - To Find Date

My friends, from years ago, this forum helped me with the code below.


In the Word Document, It finds a date in a format like this: 9/24/2022 10:04:36 AM
It works great.
But now there is more than one date in the document. So I want it to find only the date on the last line of the document, the last paragraph.

How can I change this to find the date in the last line/paragraph of the document?

This is the old code:

Code:
Dim oRng As Word.Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "[0-9]{1,2}\/"
    .MatchWildcards = True
    If .Execute Then
      oRng.End = oRng.Paragraphs(1).Range.End - 1
      If IsDate(oRng.Text) Then
         ActiveDocument.Range.InsertAfter "It took this long: " & fcnCalcSpanStart_Finish(oRng.Text, Now)
      End If
    End If
 End With

Last edited by macropod; 09-24-2022 at 03:26 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 09-24-2022, 03:28 PM
macropod's Avatar
macropod macropod is offline Range For Last Line/Paragraph - To Find Date Windows 10 Range For Last Line/Paragraph - To Find Date Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

That's as simple as:
Set oRng = ActiveDocument.Paragraphs.Last.Range
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-24-2022, 03:50 PM
Stephen Ray Stephen Ray is offline Range For Last Line/Paragraph - To Find Date Windows 10 Range For Last Line/Paragraph - To Find Date Office 2016
Advanced Beginner
Range For Last Line/Paragraph - To Find Date
 
Join Date: Sep 2018
Location: Kansas
Posts: 34
Stephen Ray is on a distinguished road
Default

Macropad, Thanks, I tried several things with "Last" in there but nothing worked.
I am sure yours will work.
I will try it Monday at work and let you know.
Reply With Quote
  #4  
Old 09-26-2022, 09:29 AM
Stephen Ray Stephen Ray is offline Range For Last Line/Paragraph - To Find Date Windows 10 Range For Last Line/Paragraph - To Find Date Office 2016
Advanced Beginner
Range For Last Line/Paragraph - To Find Date
 
Join Date: Sep 2018
Location: Kansas
Posts: 34
Stephen Ray is on a distinguished road
Default

Macropad, It did not work.

The Word document is two pages long and ends like this:

XXXX action taken by XXX personnel.
Active Directory Record already enabled. No Action Taken by XXX Personnel.
User has been updated in the Remedy System
Start Date & Time 9/22/2022 10:36:45 AM

The code I tested looks like this:

Code:
Dim oRng As Word.Range
  ‘Set oRng = ActiveDocument.Range
   Set oRng = ActiveDocument.Paragraphs.Last.Range
       ActiveDocument.Range.InsertAfter "Just After The Set" & oRng.Text 
  With oRng.Find
    .Text = "[0-9]{1,2}\/"
    .MatchWildcards = True
    If .Execute Then
      oRng.End = oRng.Paragraphs(1).Range.End - 1
      If IsDate(oRng.Text) Then
         ActiveDocument.Range.InsertAfter "It took this long: " & fcnCalcSpanStart_Finish(oRng.Text, Now)
      End If
    End If
 End With

The output is this:

XXXX action taken by XXX personnel.
Active Directory Record already enabled. No Action Taken by XXX Personnel.
User has been updated in the Remedy System
Start Date & Time 9/22/2022 10:36:45 AM
Just After The Set
Monday, September 26, 2022 10:53:57 AM


So the new code is not finding the Date in the document.
Somehow the date and time when the code is run gets into ORng.
And the: With oRng.Find does not find the date. We know it does not find it because the If.Execute statements below it do not run.


I have the book, "Mastering VBA for Microsoft Office 2016" -Latest Version. And I cannot find anywhere that shows the way to do this.

Thanks Macrpod.
Reply With Quote
  #5  
Old 09-26-2022, 10:32 AM
Stephen Ray Stephen Ray is offline Range For Last Line/Paragraph - To Find Date Windows 10 Range For Last Line/Paragraph - To Find Date Office 2016
Advanced Beginner
Range For Last Line/Paragraph - To Find Date
 
Join Date: Sep 2018
Location: Kansas
Posts: 34
Stephen Ray is on a distinguished road
Default

Macropad, I found a work around.
I selected and highlighted the date in the last paragraph: 9/22/2022 10:36:45 AM
Then I used this: Set oRng = Selection.Range
That works.

But if you can find a way to do it using "Last" Please let me know.
And where the devil can I read about all the ways to use Range?

Somebody like you should write a book and make lots of money.
We really do need somebody to write a book with VBA syntax with lots and lots of examples.

The best way to learn any language is by reading good literature.
Reply With Quote
  #6  
Old 09-26-2022, 03:10 PM
macropod's Avatar
macropod macropod is offline Range For Last Line/Paragraph - To Find Date Windows 10 Range For Last Line/Paragraph - To Find Date Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Your latest code isn't the same as what you posted the first time. This line:
ActiveDocument.Range.InsertAfter "Just After The Set" & oRng.Text
inserts content after the range you just defined with:
Set oRng = ActiveDocument.Paragraphs.Last.Range
Hence, oRng no longer points to the last paragraph!
Your code should be something like:
Code:
With ActiveDocument.Range
  .InsertAfter vbCr & "Just After The Set " & .Paragraphs.Last.Range.Text
  With .Find
    .Text = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"
    .MatchWildcards = True
    .Execute
    .Forward = False
  End With
  If .Find.Found = True Then
    .End = .Paragraphs.Last.Range.End - 1
    If IsDate(.Text) Then ActiveDocument.Range.InsertAfter "It took this long: " & fcnCalcSpanStart_Finish(oRng.Text, Now)
  End If
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 09-27-2022, 05:52 AM
Stephen Ray Stephen Ray is offline Range For Last Line/Paragraph - To Find Date Windows 10 Range For Last Line/Paragraph - To Find Date Office 2016
Advanced Beginner
Range For Last Line/Paragraph - To Find Date
 
Join Date: Sep 2018
Location: Kansas
Posts: 34
Stephen Ray is on a distinguished road
Default

Macropod, Thank you for your reply. I will try your code.
And I see now that I was inserting stuff after the defined range.
I will go experiment with all this until I get the results I want.

But I have a question, If the text is this:
XXXX action taken by XXX personnel.
Active Directory Record already enabled. No Action Taken by XXX Personnel.
User has been updated in the Remedy System
Start Date & Time 9/22/2022 10:36:45 AM

And with the code: Set oRng = ActiveDocument.Paragraphs.Last.Range

The contents of oRng = Start Date & Time 9/22/2022 10:36:45 AM

Is that right?
Reply With Quote
  #8  
Old 09-27-2022, 04:21 PM
macropod's Avatar
macropod macropod is offline Range For Last Line/Paragraph - To Find Date Windows 10 Range For Last Line/Paragraph - To Find Date Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Stephen Ray View Post
I have a question, If the text is this:
XXXX action taken by XXX personnel.
Active Directory Record already enabled. No Action Taken by XXX Personnel.
User has been updated in the Remedy System
Start Date & Time 9/22/2022 10:36:45 AM

And with the code: Set oRng = ActiveDocument.Paragraphs.Last.Range

The contents of oRng = Start Date & Time 9/22/2022 10:36:45 AM

Is that right?
When it is set, yes.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Range For Last Line/Paragraph - To Find Date Find and replace only paragraph marks in blank line laith93 Word 4 04-27-2022 12:18 PM
Range For Last Line/Paragraph - To Find Date How to resize a paragraph range to include just a single line of text Peterson Word VBA 5 07-08-2019 04:36 PM
Using DateValue to find if a date exists within a certain range OfficeAssociate99 Excel Programming 3 07-13-2017 11:06 AM
Range For Last Line/Paragraph - To Find Date Find if Date range falls within another range Triadragon Excel 3 05-02-2016 11:48 AM
Find a Date in a Range rspiet Excel 3 02-15-2016 08:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:03 PM.


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