• Selecting from text to number

    Author
    Topic
    #476563

    Hi!
    I want to find and select some pharases every click a macro button.
    How can I select from Explanation: to1001. (except 1001)
    I’ll do something manualy and then clicking the button again, I want to find from Explanation: to1002. (except 1002)
    Explanation: to1003. (except 1003)

    —–My document——————–

    Explanation:
    1. Bla bla bla. Bla bla bla. Bla bla bla.[/COLOR]
    Bla bla bla.

    2. Bla bla bla. Bla bla bla.[/COLOR] Bla bla bla.
    Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.[/COLOR][/COLOR][/COLOR]

    1001. Bla bla bla. Bla bla bla.[/COLOR] Bla bla bla.
    Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.[/COLOR][/COLOR][/COLOR]

    Explanation:
    1. Bla bla bla. Bla bla bla. Bla bla bla.[/COLOR]
    Bla bla bla.

    3. Bla bla bla. Bla bla bla.[/COLOR] Bla bla bla.
    Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.[/COLOR][/COLOR][/COLOR]

    5. Bla bla bla. Bla bla bla.[/COLOR] Bla bla bla.
    Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.[/COLOR][/COLOR][/COLOR]

    [/COLOR]1002. Bla bla bla. Bla bla bla.[/COLOR] Bla bla bla.
    Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.[/COLOR][/COLOR][/COLOR]

    …..[/COLOR][/COLOR]
    …..
    [/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]

    Viewing 8 reply threads
    Author
    Replies
    • #1279125

      Could you restate the problem in anothe way or more clearly?

    • #1279136

      Hi yken,

      You could put ‘Explanation’ into Word’s ‘Find’ box and do a Find for it. If you then click on the double Down/Up arrows in the bottom right corner of the screen, that will take you to the next instance.

      Alternatively:
      By adding the following macros tor Word’s ‘Normal’ template and assigning the keyboard shortcuts the Alt-N (for next) and the othe to Alt-P (for previous) to them, navigating forward or backward for any selected word or string is as simple as pressing Alt-N or Alt-P.

      Code:
      Sub NavigateNext()
      Dim StrFnd As String, bDirection As Boolean
      StrFnd = Trim(Selection.Text)
      If StrFnd = vbNullString Then
        MsgBox "No Text Selected: Please select a find string!", vbOKOnly, "Navigation Error"
        Exit Sub
      End If
      Selection.Collapse wdCollapseEnd
      bDirection = True
      Call Navigator(StrFnd, bDirection)
      End Sub
      Code:
      Sub NavigatePrev()
      Dim StrFnd As String, bDirection As Boolean
      StrFnd = Trim(Selection.Text)
      If StrFnd = vbNullString Then
        MsgBox "No Text Selected: Please select a find string!", vbOKOnly, "Navigation Error"
        Exit Sub
      End If
      Selection.Collapse wdCollapseStart
      bDirection = False
      Call Navigator(StrFnd, bDirection)
      End Sub
      Code:
      Sub Navigator(StrFnd As String, bDirection As Boolean)
      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Text = vbNullString
        .Text = StrFnd
        .Forward = bDirection
        .Wrap = wdFindStop
        .Format = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .Execute
        If .Found = False Then
          MsgBox Chr(34) & StrFnd & Chr(34) & " not found", vbExclamation
        End If
      End With
      End Sub

      As you can see, there’s actually three macros. The last one (Navigator) is called by the other two and does the actual work of finding the next/previous instance.
      As coded, the ‘NavigateNext’ and ‘NavigatePrev’ subs look for matches of only the actual string selected, which might only be a single character. Telling Word to use a whole word is as simple as double-clicking on that word before using the Alt-N or Alt-P.

      If you’d rather have Word act upon the whole of the currently-selected word if the selection is less than the complete word, you could add the following code to the ‘NavigateNext’ and ‘NavigatePrev’ subs between the ‘Dim’ and ‘StrFnd’ lines in each:

      Code:
      With Selection.Range
        If Len(Trim(.Text)) > 0 Then
          If .Words.Count <= 2 Then
            If Len(Trim(.Text)) <= Len(.Text) Then
              .Words.Last.Select
            End If
          End If
        End If
      End With

      Enjoy.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1279174

      When I click the Sub Navigator macro, “Argument not optional” message shows.
      When I click the Sub NavigateNext macro, It finds only “Explanation:” words.
      I want to find and select from Explanation: to 1001 (except 1001) and then delete selected block.
      it will continue to do so.

      • #1279241

        When I click the Sub Navigator macro, “Argument not optional” message shows.
        When I click the Sub NavigateNext macro, It finds only “Explanation:” words.
        I want to find and select from Explanation: to 1001 (except 1001) and then delete selected block.
        it will continue to do so.

        As explained in my original post, the only macros you use directly are the NavigateNext and NavigatePrev subs. They call the Navigator macro with the required arguments.

        Your original post said nothing about deleting – only about finding. The macros I’ve supplied are for finding only. All you do is install them and assign the keyboard shortcuts as described, then select a block of text and press Alt-N or Alt-P to go to the next or previous occurrence. If you delete the ‘found’ text, the macro I’ve provided then won’t have anything to use as the selected text for the next navigation.

        Cheers,
        Paul Edstein
        [Fmr MS MVP - Word]

    • #1279189

      Do you want to delete every paragraph from the document that doesn’t begin with a number greater than 1000?

      If so, is the number typed in or is it an automatic paragraph number?

    • #1279236

      No I don’t want to delete every paragraph . They are coming “PDF to word” document . I want to do like Below.
      Find Explanation:
      extend
      find 1001
      moveleft 5 Character (because 1001 except)
      delete selection
      1001+1 (next number)
      end

    • #1279263

      O.K. I tried. If there are only the same blocks it run . But each blocks are different.
      Having an error below.
      .Text = StrFnd
      “Run-time error 5854”

    • #1279334

      That error means you’re trying to Find a string that is longer than 256 characters. Word’s find operation can’t do that.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1279428

      Am I correct that you want to end up with:

      Explanation:

      1001. Bla bla bla. Bla bla bla. Bla bla bla.
      Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.

      Explanation:

      1002. Bla bla bla. Bla bla bla. Bla bla bla.
      Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.

      If that’s what you want, I have recorded a macro using the Extend Selection function (F8). It will need to be edited by someone who knows VBA to make it into a swish macro though. (VBA and me have never seen eye to eye!)

      What it basically does is starts with the insertion point immediately following the colon, press F8, press 1, press 1 (the selection now includes the 1 of 100x). Escape (exits extend), enter (inserts a paragraph return) and finally 1 (replaces the deleted 1 from 100x).

      I’m sure that one of the VBA gurus can make that work for you.

      Sub xsel()

      ‘ xsel Macro


      Selection.Extend
      Selection.Extend Character:=”1″
      Selection.Extend Character:=”1″
      Selection.EscapeKey
      Selection.TypeParagraph
      Selection.TypeText Text:=”1″
      End Sub

      • #1279462

        Am I correct that you want to end up with:

        Explanation:

        1001. Bla bla bla. Bla bla bla. Bla bla bla.
        Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.

        Explanation:

        1002. Bla bla bla. Bla bla bla. Bla bla bla.
        Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.

        If that’s all the task is, a wildcard Find/Replace will probably do the job, where:

        Code:
        Find = (Explanation:)*([0-9]{4})
        Replace = 1^p2

        You don’t even need a macro for that!

        Cheers,
        Paul Edstein
        [Fmr MS MVP - Word]

    • #1279496

      That’s enough for now. Thank you to everyone.

    Viewing 8 reply threads
    Reply To: Selecting from text to number

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: