Solved

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

Posted on 2011-03-08
4
194 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
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 shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

947 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

19 Experts available now in Live!

Get 1:1 Help Now