Consulting

Results 1 to 8 of 8

Thread: Delete leading characters

  1. #1
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location

    Delete leading characters

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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-".
    Attached Files Attached Files
    Last edited by Kilroy; 12-22-2016 at 05:55 AM. Reason: manage attachments

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Last edited by gmaxey; 12-22-2016 at 08:47 AM.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Last edited by gmaxey; 12-22-2016 at 08:45 AM.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Guys both macros work perfectly. Really I can use each one of them in different situations. Thank so much

  8. #8
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •