PDA

View Full Version : [SOLVED:] Delete leading characters



Kilroy
12-20-2016, 09:54 AM
I modified a code I use to get rid of leading tabs but it kills all in whole document not just leading. I'm wondering if it can be modified to delete certain other leading text? sometimes I need to go through hundreds of paragraphs to delete text that is there before a number. example: NCA-1000 and delete it (NCA-). sometimes it may NF-2300 in each case I need to delete the letters before the numbers. The catch is that there are other occurrences in the document where I do not want to delete the prefix (It's a reference to another clause). I suppose ideally an input box where I could enter what I what to delete (leading characters) followed by the option of do I want to delete this one? would be amazing. I tried the recorder, tried searching and can't find anything close to this. Any help or guidance appreciated.



Sub RemoveLeadingCharacters()
If ActiveDocument.Range(0, 1).Text = "NCA-" Then
ActiveDocument.Range(0, 1).Delete
End If
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "NCA-"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub

gmayor
12-22-2016, 12:28 AM
The following will select each occurrence and prompt for a decision. Change strFind as required


Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 22/12/2016
Const strFind as String = "NCA-"
Dim orng As Range
Set orng = ActiveDocument.Range
With orng.Find
Do While .Execute(FindText:=strFind)
orng.Select
If MsgBox("Delete this occurrence?", vbYesNo) = vbYes Then
orng.Text = ""
End If
orng.Collapse 0
Loop
End With
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub

Kilroy
12-22-2016, 05:54 AM
Thanks Graham works great but is still looking at all occurrences of "NCA-". Is there a way for it to just look at lines that have the leading characters? I'm sorry for not being very clear. I have attached a sample doc showing highlighted text of where I would need to remove "NCA-".

gmaxey
12-22-2016, 07:18 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim strFind As String
Dim lngIndex As Long
strFind = InputBox("String to find.", "Find", "NCA-")
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "([^12^13])NCA-"
.Replacement.Text = "\1"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
'While not applicable in your example, _
'deal with the document first paragraph.
If Left(oRng.Paragraphs(1).Range.Text, Len(strFind)) = strFind Then
For lngIndex = Len(strFind) To 1 Step -1
oRng.Paragraphs(1).Range.Characters(lngIndex).Delete
Next
End If
lbl_Exit:
Exit Sub
End Sub

gmayor
12-22-2016, 07:37 AM
You could add an extra check to ensure the occurrence is at the start of the paragraph e.g.


Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 22/12/2016
Const strFind As String = "NCA-"
Dim orng As Range
Set orng = ActiveDocument.Range
With orng.Find
Do While .Execute(FindText:=strFind)
If orng.Start = orng.Paragraphs(1).Range.Start Then
orng.Select
If MsgBox("Delete this occurrence?", vbYesNo) = vbYes Then
orng.Text = ""
End If
End If
orng.Collapse 0
Loop
End With
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub

gmaxey
12-22-2016, 07:53 AM
Your decision now is do you want a macro to just do what you first said you wanted done or do you want to look and verify that it is doing what you want done. You have sort of asked for both and Graham and I have given you both.

Kilroy
12-22-2016, 08:30 AM
Guys both macros work perfectly. Really I can use each one of them in different situations. Thank so much

Kilroy
12-22-2016, 09:45 AM
Guys I wanted to give some feed back on the application of this macro. 1. They work perfectly. 2. since my document was a PDF opened in word I had all these little specs from scanning that word changed into objects. These objects usually get anchored to a few characters in a sentence. This happened twice in the full version of my doc so those lines were not searched for "NCA-" and were not changed. I just thought you may find this interesting. I have a macro that searches and gives the option of deleting (also from Greg and Graham) these objects so a simple reordering of my macros solved the issue for me.

Thanks again fellas