Solved

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

Posted on 2011-03-08
4
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
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:

730 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