Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-28-2012, 04:05 AM
AlmostFriday AlmostFriday is offline Insert text at the end of a sentence Find/Replace Windows XP Insert text at the end of a sentence Find/Replace Office 2007
Novice
Insert text at the end of a sentence Find/Replace
 
Join Date: Dec 2011
Posts: 8
AlmostFriday is on a distinguished road
Default Insert text at the end of a sentence Find/Replace

Help - I'm stuck

I'm trying to insert "<end level = "4">" at the end of all the sentences that start with "<start level = "4">"

E.g.
from:


<start level = "4"> The rubber duck went quack-quack.
to:
<start level = "4"> The rubber duck went quack-quack.<end level = "4">

Is there a way of ignoring the text and going straight to the end of the sentence?
Reply With Quote
  #2  
Old 05-29-2012, 04:07 AM
macropod's Avatar
macropod macropod is offline Insert text at the end of a sentence Find/Replace Windows 7 64bit Insert text at the end of a sentence Find/Replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Try the following macro:
Code:
Sub AddTags()
Dim Snt As Range
For Each Snt In ActiveDocument.Sentences
  If InStr(Snt.Text, "<start level = ""4""> ") = 1 Then
    Snt.InsertAfter "<end level = ""4""> "
  End If
Next
End Sub
To use it, see: http://www.gmayor.com/installing_macro.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-14-2012, 08:49 AM
AlmostFriday AlmostFriday is offline Insert text at the end of a sentence Find/Replace Windows XP Insert text at the end of a sentence Find/Replace Office 2007
Novice
Insert text at the end of a sentence Find/Replace
 
Join Date: Dec 2011
Posts: 8
AlmostFriday is on a distinguished road
Default

Thank you Macropod
But, I can't get it to work.
Reply With Quote
  #4  
Old 06-14-2012, 02:24 PM
macropod's Avatar
macropod macropod is offline Insert text at the end of a sentence Find/Replace Windows 7 64bit Insert text at the end of a sentence Find/Replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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 not exactly descriptive.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 06-17-2012, 04:38 AM
gmaxey gmaxey is offline Insert text at the end of a sentence Find/Replace Windows XP Insert text at the end of a sentence Find/Replace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

The problem with the sentence collection is that Word often can't determine what they really are.

Paul, try your code on:


<start level = "4"> Mr. Duck, a small rubber duck, went quack-quack.

AF, are these snippets of text really individual sentences nested in paragraphs or does each constitute a paragraph itself? If so, then this addaption to Pauls code may work:

Code:
Sub AddTags()
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
 If InStr(oPar.Range.Text, "<start level = ""4""> ") = 1 Then
   Set oRng = oPar.Range
   oRng.Collapse wdCollapseEnd
   oRng.Text = "<end level = ""4""> "
 End If
Next
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by macropod; 06-17-2012 at 04:48 AM. Reason: Added code tags & formatting
Reply With Quote
  #6  
Old 06-17-2012, 04:47 AM
macropod's Avatar
macropod macropod is offline Insert text at the end of a sentence Find/Replace Windows 7 64bit Insert text at the end of a sentence Find/Replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Greg,

Yes, I do realise Word's Sentences collection is, shall we say, brain dead (why can't MS employ the sentence logic from it's own grammar checker to inform vba of what a sentence is??).

All the same, a response like 'I can't get it to work' is, as I said, "not exactly descriptive".

I'm not sure that your code will fare any better, as it works at the paragraph level only. So, if the second sentence starts with "<start level = "4">", it won't get processed; and a multi-sentence paragraph that starts with "<start level = "4">" will get the closing tag at the end of the paragraph instead of at the end of the first sentence.

I guess we'll need to wait till the OP says what the issue is.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 06-17-2012, 05:21 AM
gmaxey gmaxey is offline Insert text at the end of a sentence Find/Replace Windows XP Insert text at the end of a sentence Find/Replace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Paul,

I'm thinking that as the OP is using "levels" then perhaps each snippet (sentence) of interest is actually an independent paragraph. If not, then yes we will have to wait on the OP.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert text at the end of a sentence Find/Replace Macro to find text and replace with form field containing that text iiiiifffff Word VBA 16 06-04-2016 01:47 AM
Insert text at the end of a sentence Find/Replace Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
Insert text at the end of a sentence Find/Replace Help with find and replace or query and replace shabbaranks Excel 4 03-19-2011 08:38 AM
Find and replace page numbers in body of text tollanarama Word 3 02-13-2011 06:00 AM
Find & Replace Insert Issue mipacker Word 0 02-18-2009 08:59 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:17 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