Solved

Search for a number, copy it, exit the sub.

Posted on 2011-03-08
4
193 Views
Last Modified: 2012-05-11
Dear Experts:

I would like to run a macro that performs the following tasks:

- Search for a number with the following make-up (##-###-##-##) in ALL STORY RANGES
- Only the CURRENT section (i.e. where the cursor currently resides) is to be searched
- As soon as the first occurrence of this string/number (##-###-##-##) is found, the number is to be copied and the macro is to exit.

Thank you very much in advance for your professional help.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 35070259
Hi Andreas,
I think that this should do it.
Sub FindNumber()
    Dim rng As Range
    Dim rng2 As Range
    Dim s As Integer
    
    s = Selection.Sections(1).Index
    ' Go through all story ranges in the document, including shapes,
    ' headers & footers.
    For Each rng In ActiveDocument.StoryRanges
        Set rng2 = rng 'set rng2 to first range in story
        Do
            If rng2.Sections(1).Index = s Then
                With rng2.Find
                    .MatchWildcards = True
                    .Text = "[0-9]{2}\-[0-9]{3}\-[0-9]{2}\-[0-9]{2}"
                    If .Execute() Then
                        rng2.Copy
                        Exit Sub
                    End If
                End With
            End If
            Set rng2 = rng2.NextStoryRange
        Loop Until rng2 Is Nothing
    Next rng

End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:royhsiao
ID: 35070701
To select only the CURRENT section (i.e. where the cursor currently resides)  and search the (##-###-##-##).

You could do the following:
Sub Currnet_Paragraph_Find_Copy()
Dim r As Range
Set r = ActiveDocument.Range(Start:=0, _
End:=Selection.Start)
Dim i As Integer
i = r.ComputeStatistics(wdStatisticParagraphs) + 1

ActiveDocument.Paragraphs(i).Range.Select
     With Selection.Find
        .Text = "^#^#" & "-" & "^#^#^#" & "-" & "^#^#" & "-" & "^#^#"
    End With
    
    Selection.Find.Execute
    Selection.Copy

End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 35073689
Dear royhsia:

thank you very much for your kind help. I am afraid to tell you that your kind is not producing the desired result. I will award all the points to Graham. Nevertheless, thank you very much for the time taken and your professional efforts.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 35073696
Graham,

as always, your codes work like a charm.

Thank you very much for your great and professional support.

Regards, Andreas
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now