Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2018, 07:20 PM
Helend269 Helend269 is offline Delete certain characters from a line Windows 7 32bit Delete certain characters from a line Office 2003
Novice
Delete certain characters from a line
 
Join Date: Aug 2017
Posts: 15
Helend269 is on a distinguished road
Default Delete certain characters from a line

I have a situation where I need to go through a document, find a certain word and when that word has been found go down one line and if that line contains either opening or closing parenthesis then delete those two characters.

So:

Word
(sample text)

becomes

Word
sample text

All I have so far is:

Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Wrap = wdFindStop
.Text = "WORD"
Do While .Execute
Selection.MoveDown Unit:=wdLine, Count:=1


'delete any instances of ( and ) from that line
Loop
End With
Application.ScreenUpdating = True

How can I do this, please? Many thanks.
Reply With Quote
  #2  
Old 01-07-2018, 09:55 PM
gmayor's Avatar
gmayor gmayor is offline Delete certain characters from a line Windows 10 Delete certain characters from a line Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,103
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

This sounds fairly straightforward. Can we assume that the 'lines' you refer to are in fact paragraph's? There are no 'lines' in a Word document, other than as an artefact of text flow.
Code:
Sub Macro1()
Const strWord As String = "word to find"
Dim oRng As Range
Dim oFind As Range
    Set oFind = ActiveDocument.Range
    With oFind.Find
        Do While .Execute(FindText:=strWord, MatchCase:=True, MatchWholeWord:=True)
            oFind.End = oFind.Paragraphs(1).Range.End
            Set oRng = oFind.Next.Paragraphs(1).Range
            oRng = Replace(oRng, "(", "")
            oRng = Replace(oRng, ")", "")
            oFind.Collapse 0
        Loop
    End With
lbl_Exit:
    Set oFind = Nothing
    Set oRng = Nothing
    Exit Sub
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 01-08-2018, 06:44 AM
Helend269 Helend269 is offline Delete certain characters from a line Windows 7 32bit Delete certain characters from a line Office 2003
Novice
Delete certain characters from a line
 
Join Date: Aug 2017
Posts: 15
Helend269 is on a distinguished road
Default

Yes, that worked beautifully. Thank you!

I was looking up about working with lines and didn't know about the paragraphs thing which might put me on the right track. Now I've done what I needed to I'll pick this apart and see how it works...
Reply With Quote
  #4  
Old 01-08-2018, 08:16 PM
Helend269 Helend269 is offline Delete certain characters from a line Windows 7 32bit Delete certain characters from a line Office 2003
Novice
Delete certain characters from a line
 
Join Date: Aug 2017
Posts: 15
Helend269 is on a distinguished road
Default

One last question on this one: how can I restrict the running of this macro to the current section? I've played around with Set oFind = ActiveDocument.Range(Sections(wdActiveEndSectionNu mber)) but I just keep getting errors.

Thanks.
Reply With Quote
  #5  
Old 01-08-2018, 10:14 PM
gmayor's Avatar
gmayor gmayor is offline Delete certain characters from a line Windows 10 Delete certain characters from a line Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,103
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

Restricting to the current section is only a little more complicated.
Code:
Sub Macro2()
Const strWord As String = "word to find"
Dim oRng As Range
Dim oFind As Range
Dim oSection As Section
Set oSection = Selection.Sections(1)    'the section the cursor is in
Set oFind = oSection.Range    'set a range to the section
    With oFind.Find
        'look for each occurrence of the word in turn
        Do While .Execute(FindText:=strWord, MatchCase:=True, MatchWholeWord:=True)
            If oFind.InRange(oSection.Range) Then    'the word is in the section
                oFind.End = oFind.Paragraphs(1).Range.End    'move the end of the range to the end of the paragraph
                'containing the word.
                Set oRng = oFind.Next.Paragraphs(1).Range    'set a range to the following paragraph
                oRng.End = oRng.End - 1    'omit the paragraph break character (and/or the section break)
                oRng = Replace(oRng, "(", "")    'delete the first unwanted character
                oRng = Replace(oRng, ")", "")    'delete the second unwanted character
                oFind.Collapse 0    ' collapse the found text range to its end
            Else
                GoTo lbl_Exit    'the found word is not in the section
            End If
        Loop
    End With
lbl_Exit:
    Set oSection = Nothing
    Set oFind = Nothing
    Set oRng = Nothing
    Exit Sub
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
  #6  
Old 01-09-2018, 12:08 PM
Helend269 Helend269 is offline Delete certain characters from a line Windows 7 32bit Delete certain characters from a line Office 2003
Novice
Delete certain characters from a line
 
Join Date: Aug 2017
Posts: 15
Helend269 is on a distinguished road
Default

That worked brilliantly. Thank you! :-)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete certain characters from a line Word 2010 characters per line glitch VelmaD Word 1 03-18-2014 05:41 PM
Delete certain characters from a line How do I delete strange characters from a document. Stokkers Word 1 06-12-2013 04:21 AM
Delete certain characters from a line Word VBA: Cannot Edit Range (Delete characters except the first in a table cell) tinfanide Word VBA 3 04-27-2012 09:48 AM
Delete certain characters from a line getting additional characters in email subject line namishtiwari Outlook 1 04-21-2009 11:21 PM
Delete certain characters from a line Help please: Keeping spaced characters on one line EtienneOBrien Word 3 12-24-2008 07:05 AM

Other Forums: Access Forums

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