Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-22-2023, 01:26 AM
RobiNew RobiNew is offline Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2016
Competent Performer
Deleting specific strings of characters in specific positions
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default Deleting specific strings of characters in specific positions

I devised (part of) a macro to delete certain strings of characters in specific positions. It works, but can someone suggest a more efficient way to obtain the same results? Thanks!


Code:
Sub DeleteCertainStrings()

Set oRng = ActiveDocument.StoryRanges(1)
 
With oRng.Find
.Text = "Nnnnnnnnnnnn/Nnnnnnnnn^p" '1: text to delete except ^p, which is the 1st para of the 1st page
.Execute
End With
If oRng.Find.Found = True Then
oRng.Delete
End If

With oRng.Find '2: text to delete (including final space) except ^p, the 1st para of the 2nd page
 .Text = "Nn Nnn Nnnnn. Nnnn nn NNN Nnnnnnnnn ^p"
 .Execute
End With
If oRng.Find.Found = True Then
oRng.Delete
 End If

With oRng.Find '3: to delete everything except ^p, followed by 10 ^p's, which must remain
.Text = "Nnn nnnnnnnnnn nnnnn nn nnn nnnnn nn nnnnnnnnnn^l
nnnn nnn nnnnnnnn nn Nnnnn Nnnnnnn’n “Nnnnnnn” (G3-1v).^p"
.Execute
End With
If oRng.Find.Found = True Then
oRng.Delete
 End If
End Sub
Reply With Quote
  #2  
Old 11-22-2023, 09:59 AM
gmaxey gmaxey is online now Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Paragraphs(1).Range
  'To delete the text of the first paragraph, you don't need find and replace
  If oRng.Text = "Nnnnnnnnnnnn/Nnnnnnnnn" & vbCr Then oRng.Text = vbCr
  Set oRng = ActiveDocument.Range
  'When using .Execute, you don't have to use If .Find.Found = True
  With oRng.Find
    .Text = "Nn Nnn Nnnnn. Nnnn nn NNN Nnnnnnnnn"
    If .Execute Then oRng.Delete
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-22-2023, 10:24 AM
vivka vivka is offline Deleting specific strings of characters in specific positions Windows 7 64bit Deleting specific strings of characters in specific positions Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! What about this:
Code:
Sub DeleteCertainStrings()

Set oRng = ActiveDocument.StoryRanges(1)
 
    With oRng.Find
        .text = "Nnnnnnnnnnnn/Nnnnnnnnn^p"
        .Replacement.text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = False
        .Execute Replace:=wdReplaceAll
        .text = "Nn Nnn Nnnnn. Nnnn nn NNN Nnnnnnnnn ^p"
        .Replacement.text = ""
        .Execute Replace:=wdReplaceAll
        .text = "Nnn nnnnnnnnnn nnnnn nn nnn nnnnn nn nnnnnnnnnn^|nnnn nnn nnnnnnnn nn Nnnnn Nnnnnnn’n “Nnnnnnn” (G3-1v).^p"
        .Replacement.text = ""
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Reply With Quote
  #4  
Old 11-22-2023, 11:12 AM
RobiNew RobiNew is offline Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2016
Competent Performer
Deleting specific strings of characters in specific positions
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks,Gmaxey, for the code and explanations! And thanks a lot, Vivka, for the concise alternative to my code!
Reply With Quote
  #5  
Old 11-22-2023, 12:02 PM
vivka vivka is offline Deleting specific strings of characters in specific positions Windows 7 64bit Deleting specific strings of characters in specific positions Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

You are welcome!
Reply With Quote
  #6  
Old 11-23-2023, 03:14 AM
RobiNew RobiNew is offline Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2016
Competent Performer
Deleting specific strings of characters in specific positions
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi, Gmaxey! I was trying to use part of your code, but I failed somehow. I cannot force it to insert the new line. Thanks!
Code:
Set oRng = ActiveDocument.Range
    With oRng.Find
        .Text = " (about"
        .Replacement.Text = ""
If .Execute Then MsgBox "Found"
If .Execute Then oRng.InsertBefore "^l"
    End With
Reply With Quote
  #7  
Old 11-23-2023, 05:16 AM
gmaxey gmaxey is online now Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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

What specifically are you trying to do? If you want to insert a line break before " (about" then:


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = " (about"
    If .Execute Then
      oRng.Characters(1).Delete
      oRng.InsertBefore Chr(11)
    End If
  End With
lbl_Exit:
  Exit Sub
End Sub

In your existing code, the .Execute is only going return True once because after the first find, oRng IS the found text and the found text is the text you want to find.



Type this sentence in a select a word document and then run:


Code:
Sub Demo()
Dim oRng As Range
  Set oRng = Selection.Range
  With oRng.Find
    .Text = Selection.Text
    If .Execute Then
      MsgBox "Found"
    Else
      MsgBox "I can't find myself when I AM the defined search range."
    End If
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 11-24-2023, 02:03 AM
RobiNew RobiNew is offline Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2016
Competent Performer
Deleting specific strings of characters in specific positions
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks a lot, Gmaxey! All this is very instructive. Your code works like charm.
At present I'm trying to remove underline in a specific postion (lastChr). But the code removes underline in the whole document. Please help!

Code:
With ActiveDocument
    LastChr = .GoTo(wdGoToPage, wdGoToLast).End
    .Range(LastChr, ActiveDocument.Range.End).Font.Underline = False
    .Range(LastChr, ActiveDocument.Range.End).InsertAfter Chr(13) & "Firt Part - End"
End With
Reply With Quote
  #9  
Old 11-24-2023, 04:45 PM
gmaxey gmaxey is online now Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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

RobiNew,


Firstly, what is LastChr? It is not declared as a variable.
Secondly, if it is intended as a range, then stop treating it like a selection.


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  oRng.Collapse wdCollapseEnd
  oRng.Start = oRng.Start - 1
  oRng.Font.Underline = wdUnderlineNone
  oRng.InsertAfter vbCr & "First Part - End"
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 11-25-2023, 09:31 AM
RobiNew RobiNew is offline Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2016
Competent Performer
Deleting specific strings of characters in specific positions
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks, Gmaxey, for the code and the always instructive comments. I wonder why "First Part - End" remains undelined despite 'oRng.Font.Underline = wdUnderlineNone'.
Reply With Quote
  #11  
Old 11-25-2023, 11:36 AM
gmaxey gmaxey is online now Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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

That isn't happening here. Not sure what you document looks like, but you could try:

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
Dim oRng2 As Range
  Set oRng = ActiveDocument.Range
  oRng.Collapse wdCollapseEnd
  oRng.Start = oRng.Start - 1
  oRng.Font.Underline = wdUnderlineNone
  oRng.InsertAfter vbCr & "First Part - End"
  Set oRng = ActiveDocument.Range.Paragraphs.Last.Range
  oRng.Font.Underline = wdUnderlineNone
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 11-26-2023, 12:59 AM
RobiNew RobiNew is offline Deleting specific strings of characters in specific positions Windows 10 Deleting specific strings of characters in specific positions Office 2016
Competent Performer
Deleting specific strings of characters in specific positions
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks, Gmaxey! These two lines solved the problem for me:

Set oRng = ActiveDocument.Range.Paragraphs.Last.Range
oRng.Font.Underline = wdUnderlineNone
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting specific strings of characters in specific positions Word macro for deleting a line that starts with a specific character + deleting the line before eduardb Word 1 08-10-2022 03:17 AM
Search Word for specific strings and replace with hyperlinks from a list jlive24 Word VBA 2 06-26-2022 08:14 PM
Extracting Specific Text Strings PWH68 Word VBA 6 10-08-2019 02:07 AM
Deleting specific strings of characters in specific positions Deleting Characters in a specific location in Word 2010 ppayaw Word VBA 8 12-13-2016 08:11 AM
Deleting specific strings of characters in specific positions update style of all strings available between two specific strings vikrantkale Word 1 03-28-2011 06:13 PM

Other Forums: Access Forums

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