#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
Many thanks,Gmaxey, for the code and explanations! And thanks a lot, Vivka, for the concise alternative to my code!
|
#5
|
|||
|
|||
You are welcome!
|
#6
|
|||
|
|||
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 |
#7
|
|||
|
|||
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 |
#8
|
|||
|
|||
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 |
#9
|
|||
|
|||
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 |
#10
|
|||
|
|||
Thanks, Gmaxey, for the code and the always instructive comments. I wonder why "First Part - End" remains undelined despite 'oRng.Font.Underline = wdUnderlineNone'.
|
#11
|
|||
|
|||
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 |
#12
|
|||
|
|||
Thanks, Gmaxey! These two lines solved the problem for me:
Set oRng = ActiveDocument.Range.Paragraphs.Last.Range oRng.Font.Underline = wdUnderlineNone |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 Characters in a specific location in Word 2010 | ppayaw | Word VBA | 8 | 12-13-2016 08:11 AM |
update style of all strings available between two specific strings | vikrantkale | Word | 1 | 03-28-2011 06:13 PM |