![]() |
#1
|
|||
|
|||
![]()
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? |
#2
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thank you Macropod
But, I can't get it to work. |
#4
|
||||
|
||||
![]()
That's not exactly descriptive.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
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 Last edited by macropod; 06-17-2012 at 04:48 AM. Reason: Added code tags & formatting |
#6
|
||||
|
||||
![]()
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] |
#7
|
|||
|
|||
![]()
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. |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
iiiiifffff | Word VBA | 16 | 06-04-2016 01:47 AM |
![]() |
paulkaye | Word | 4 | 12-06-2011 11:05 PM |
![]() |
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 |