Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-12-2017, 07:44 PM
puff puff is offline Problem with my script to keep all the paragraphs starting by a certain word Windows 7 64bit Problem with my script to keep all the paragraphs starting by a certain word Office 2013
Advanced Beginner
Problem with my script to keep all the paragraphs starting by a certain word
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default Problem with my script to keep all the paragraphs starting by a certain word

My goal is to save all the paragraphs starting with the word "transcript" and after that, delete all those "transcript" at the beginning of all the remaining paragraphs since they all have it.

Here's my code so far:


Code:
Sub Para()
    Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Paragraphs
        If InStr(1, oPara.Range.Words(1).Text, "transcript") = False Then
            oPara.Range.Delete
            oPara.Range.Delete
        ElseIf InStr(1, oPara.Range.Words(1).Text, "transcript") = True Then
            oPara.Range.Words(1).Text = Replace(oPara.Range.Words(1).Text, "Keep ", "")
        End If
    Next
End Sub
The problem is that this code will delete all of the paragraphs. Could someone help me out on this issue? Thank you very much!
Reply With Quote
  #2  
Old 07-12-2017, 08:41 PM
gmayor's Avatar
gmayor gmayor is offline Problem with my script to keep all the paragraphs starting by a certain word Windows 10 Problem with my script to keep all the paragraphs starting by a certain word Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

The following should work
Code:
Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 13 Jul 2017
Dim oPara As Paragraph
Dim oRng As Range
Dim i As Integer
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range.Words(1)
        If LCase(Trim(oRng.Text)) = "transcript" Then
            oRng.Text = "Keep "
        Else
            oPara.Range.Delete
        End If
    Next oPara
End Sub
__________________
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
  #3  
Old 08-14-2017, 06:07 AM
puff puff is offline Problem with my script to keep all the paragraphs starting by a certain word Windows 7 64bit Problem with my script to keep all the paragraphs starting by a certain word Office 2013
Advanced Beginner
Problem with my script to keep all the paragraphs starting by a certain word
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Thank you very much! That works really well. Another problem is that what if the word "transcript" is located at the end of the paragraph (no period after it)?
Like:

XXXXX XXXXX transcript

How do you express the concept of "the last word" in the code? 
Reply With Quote
  #4  
Old 08-14-2017, 06:57 AM
gmayor's Avatar
gmayor gmayor is offline Problem with my script to keep all the paragraphs starting by a certain word Windows 10 Problem with my script to keep all the paragraphs starting by a certain word Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

Perhaps like

Code:
Sub Macro2()
'Graham Mayor - http://www.gmayor.com - Last updated - 14 Aug 2017
Dim oPara As Paragraph
Dim oRng As Range
    For Each oPara In ActiveDocument.Paragraphs
        'set a range to the paragraph
        Set oRng = oPara.Range
        'remove the paragraph break from the end of the range
        oRng.End = oRng.End - 1
        'Clear any spaces from the end of the range
        Do While oRng.Characters.Last = Chr(32)
            oRng.End = oRng.End - 1
        Loop
        'see if the last word is '[transcript'
        If LCase(Trim(oRng.Words.Last)) = "transcript" Then
            'and if it is delete the paragraph
            oPara.Range.Delete
        End If
    Next oPara
End Sub
__________________
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem starting Microsoft Office Apps Ladi Word 1 03-06-2017 01:59 PM
Problem with my script to keep all the paragraphs starting by a certain word Starting page layout problem in Word 2010 - top of page cut off paik1002 Word 2 01-21-2016 03:59 AM
Problem with my script to keep all the paragraphs starting by a certain word Problem with bold numbers in multi-level list styles when having numbered headings AND paragraphs bwofficer Word 2 12-12-2014 12:21 AM
How to unite paragraphs starting with small capital letters with large case paragraph csongi12xme Word 1 04-12-2014 02:25 PM
Problem with my script to keep all the paragraphs starting by a certain word Starting Word Problem GeorgeVoudouris Word 4 09-07-2011 03:39 AM

Other Forums: Access Forums

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