Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-07-2016, 03:26 PM
kirkm kirkm is offline Extract a string from a paragraph Windows XP Extract a string from a paragraph Office 2003
Advanced Beginner
Extract a string from a paragraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default Extract a string from a paragraph


Just wondering if this is possible?

The string contains upper case characters and has vbtabs either side.

Thanks. I have a routine that looks through each paragraph and assigns it to a string variable.
Reply With Quote
  #2  
Old 09-08-2016, 01:01 AM
gmayor's Avatar
gmayor gmayor is offline Extract a string from a paragraph Windows 10 Extract a string from a paragraph Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can extract them individually, which begs the question - what do you want to do with these strings having extracted them?

'Contains' upper case characters or 'comprises' upper case characters?

The following assumes only upper case characters & space (no punctuation)


Code:
Sub Macro1()
Dim oRng As Range
Dim oPara As Paragraph
Dim sText As String
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        With oRng.Find
            Do While .Execute(FindText:="^t[A-Z ]{1,}^t", MatchWildcards:=True)
                If oRng.InRange(oPara.Range) Then
                    sText = Replace(oRng.Text, Chr(9), "")
                    'do something with stext e.g.
                    MsgBox sText
                End If
                oRng.Collapse 0
            Loop
        End With
    Next oPara
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 09-08-2016, 01:42 AM
kirkm kirkm is offline Extract a string from a paragraph Windows XP Extract a string from a paragraph Office 2003
Advanced Beginner
Extract a string from a paragraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default

Thanks again Graham. That's very useful and also shows me how Reg Ex is used.
(Once I've identified the string, I'm adding more data about it into the document).
Reply With Quote
  #4  
Old 09-08-2016, 05:51 PM
kirkm kirkm is offline Extract a string from a paragraph Windows XP Extract a string from a paragraph Office 2003
Advanced Beginner
Extract a string from a paragraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default

Graham, been playing around with your routine and not having any luck with a line I've changed:

Do While .Execute(FindText:="^p" & "010" & "^t", MatchWildcards:=False)

It finds nothing, even though the text does exist. If I change MatchWildcards to True it reports error 5692 but no description just one little square shape. I googled for that error but found no definite reason (except perhaps it's the ^ char.)

Obviously I'm doing something wrong... can you see what it is? Possibly that syntax is for RegEx only? Something else is for finding a text match ?

Thanks.
Reply With Quote
  #5  
Old 09-08-2016, 10:06 PM
gmayor's Avatar
gmayor gmayor is offline Extract a string from a paragraph Windows 10 Extract a string from a paragraph Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It has nothing to do with the sequence, but the fact that sequence will never appear in the current paragraph, because of ^p which makes the search across two paragraphs. Use instead

Code:
Sub Macro2()
Dim oRng As Range
Dim oPara As Paragraph
Dim sText As String
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        With oRng.Find
            Do While .Execute(FindText:="010^t")
                If oRng.start = oPara.Range.start Then
                    sText = Replace(oRng.Text, Chr(9), "")
                    'do something with stext e.g.
                    MsgBox sText
                End If
                oRng.Collapse 0
            Loop
        End With
    Next oPara
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 09-08-2016, 11:51 PM
kirkm kirkm is offline Extract a string from a paragraph Windows XP Extract a string from a paragraph Office 2003
Advanced Beginner
Extract a string from a paragraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default

> because of ^p which makes the search across two paragraphs

I should have figured that! Thanks for the new code. It's working as it should but isn't yet doing what I want. I should be able to figure it out (but haven't yet).
In your example sText is "010" but what I'm after is the whole paragraph starting with 010 + vbtab.
Been hunting around on Google all day... but Find gets you info about Find and Replace currently selected text, not find something in a paragraph. And I expect the next hurdle will be how to handle multiple finds.
Anyway I will visit your website after tea and ';m sure I'll pick up some useful tips. Many thanks.
Reply With Quote
  #7  
Old 09-09-2016, 12:41 AM
gmayor's Avatar
gmayor gmayor is offline Extract a string from a paragraph Windows 10 Extract a string from a paragraph Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you want the whole paragraph then it is opara
Code:
Sub Macro2()
Dim oRng As Range
Dim oPara As Paragraph
Dim sText As String
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        With oRng.Find
            Do While .Execute(FindText:="010^t")
                If oRng.start = oPara.Range.start Then
                    sText = oPara.Range
                    'do something with stext e.g.
                    MsgBox sText
                End If
                oRng.Collapse 0
            Loop
        End With
    Next oPara
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Exit Sub
End Sub
alternatively
Code:
Sub Macro3()
Dim oRng As Range
Dim sText As String
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:="010^t")
            If oRng.start = oRng.Paragraphs(1).Range.start Then
                sText = oRng.Paragraphs(1).Range.Text
                'do something with stext e.g.
                MsgBox sText
            End If
            oRng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #8  
Old 09-11-2016, 06:13 PM
Guessed's Avatar
Guessed Guessed is offline Extract a string from a paragraph Windows 10 Extract a string from a paragraph Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Graham's Macro3 code would be faster than the earlier alternatives of stepping through every paragraph.

I would suggest using InStr instead of .Start to get a bit more flexibility in where the found string might appear.
Code:
Sub Macro4()
  Dim oRng As Range
  Dim sText As String, sFind As String
  
  sFind = "010" & Chr$(9)
  Set oRng = ActiveDocument.Range
  With oRng.Find
    Do While .Execute(FindText:=sFind)
      sText = oRng.Paragraphs(1).Range.Text
      If InStr(sText, sFind) = 1 Then
        MsgBox sText
      End If
      oRng.Collapse 0
    Loop
  End With
lbl_Exit:
  Set oRng = Nothing
  Exit Sub
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract a string from a paragraph How to extract a word from an excel string with various lengths keywestsue Excel 3 09-18-2015 07:32 AM
How to extract only numbers from a STRING? Learner7 Excel 3 07-02-2013 06:25 AM
Extract a string from a paragraph Extract Numbers from Alphanumeric String OTPM Excel 6 05-13-2011 12:52 AM
Extract numbers from a text string aleale97 Excel 4 02-10-2011 10:33 AM
Extract from String using Wildcard whousedmy Word 0 05-21-2009 01:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft