Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-08
4
Medium Priority
?
208 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 2000 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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

604 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