Results 1 to 12 of 12
  1. #1
    Lounger
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Selecting from text to number

    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.
    Bla bla bla.

    2. Bla bla bla. Bla bla bla. Bla bla bla.
    Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.

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

    Explanation:
    1. Bla bla bla. Bla bla bla. Bla bla bla.
    Bla bla bla.

    3. Bla bla bla. Bla bla bla. Bla bla bla.
    Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.

    5. Bla bla bla. Bla bla bla. Bla bla bla.
    Bla bla bla.Bla bla bla.Bla bla bla.Bla bla bla.

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

    .....
    .....

  2. #2
    Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    49
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Could you restate the problem in anothe way or more clearly?

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,899
    Thanks
    0
    Thanked 188 Times in 172 Posts
    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
    [MS MVP - Word]

  4. #4
    Lounger
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,515
    Thanks
    3
    Thanked 143 Times in 136 Posts
    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?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    Lounger
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,899
    Thanks
    0
    Thanked 188 Times in 172 Posts
    Quote Originally Posted by yken View Post
    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
    [MS MVP - Word]

  8. #8
    Lounger
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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"

  9. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,899
    Thanks
    0
    Thanked 188 Times in 172 Posts
    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
    [MS MVP - Word]

  10. #10
    4 Star Lounger
    Join Date
    Jan 2010
    Location
    Chichester, UK
    Posts
    400
    Thanks
    1
    Thanked 10 Times in 9 Posts
    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

  11. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,899
    Thanks
    0
    Thanked 188 Times in 172 Posts
    Quote Originally Posted by TerFar View Post
    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^p\2
    You don't even need a macro for that!
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  12. #12
    Lounger
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That's enough for now. Thank you to everyone.

Posting Permissions

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