Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-24-2020, 12:08 PM
Matrix2021 Matrix2021 is offline How can I extract text question between characters Windows 10 How can I extract text question between characters Office 2019
Novice
How can I extract text question between characters
 
Join Date: Jul 2020
Posts: 6
Matrix2021 is on a distinguished road
Default How can I extract text question between characters

Hi Microsoft World.

I have a Concern. I am not sure if you can help me. I just don't know...
My concern is. How can I extract text question between characters, on a 300 page word document.

Example:

. How are you today?

To

How are you today



How do you wan me to proceed?

Thanks in advance
Reply With Quote
  #2  
Old 12-24-2020, 09:37 PM
gmayor's Avatar
gmayor gmayor is offline How can I extract text question between characters Windows 10 How can I extract text question between characters Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

Looking for text between two very common characters such as '.' and '?' is bound to produce lots of false positives. It is necessary to narrow the search criteria.


In this example I would simply search for the string and remove the unwanted characters e.g.
Code:
Sub Macro1()
Const strFind As String = ". How are you today?"
Dim sText As String
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        Do While .Execute(findText:=strFind, _
                          MatchWildcards:=False, _
                          Wrap:=wdFindStop, _
                          Forward:=True) = True
            sText = Replace(Selection.Range, ". ", "")
            sText = Replace(sText, "?", "")
            'do something with the found text e.g.
            MsgBox sText
        Loop
    End With
lbl_Exit:
    Exit Sub
End Sub
but see Replace using wildcards
__________________
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 12-25-2020, 02:50 AM
Matrix2021 Matrix2021 is offline How can I extract text question between characters Windows 10 How can I extract text question between characters Office 2019
Novice
How can I extract text question between characters
 
Join Date: Jul 2020
Posts: 6
Matrix2021 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Looking for text between two very common characters such as '.' and '?' is bound to produce lots of false positives. It is necessary to narrow the search criteria.


In this example I would simply search for the string and remove the unwanted characters e.g.
Code:
Sub Macro1()
Const strFind As String = ". How are you today?"
Dim sText As String
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        Do While .Execute(findText:=strFind, _
                          MatchWildcards:=False, _
                          Wrap:=wdFindStop, _
                          Forward:=True) = True
            sText = Replace(Selection.Range, ". ", "")
            sText = Replace(sText, "?", "")
            'do something with the found text e.g.
            MsgBox sText
        Loop
    End With
lbl_Exit:
    Exit Sub
End Sub
but see Replace using wildcards
Hi Gmayor.

I am sorry. Thanks for your reply.

The problem is this. The above question is an example (. How are you today?) I am not sure if that question "how are you today?" is in the word document. (However, it's only one question).

What I'm trying to accomplish is this? How can I extract all types of questions between 2 types and 2 groups of characters?
  1. . Question #1?
  2. , question #2?
  3. . Question #107? and so on...

Usually, questions in a paragraph have a period "." or a commo "," before the question and ends with a question mark "?".

I am sorry Gmayor, you mention. that the 2 characters "is bound to produce lots of false positives".

The question is, how am I going to work around this? What are other solutions do I have? How can I prevent "false positives"?

Thanks in advance

Cheers

Happy holiday
Reply With Quote
  #4  
Old 12-26-2020, 09:54 PM
gmayor's Avatar
gmayor gmayor is offline How can I extract text question between characters Windows 10 How can I extract text question between characters Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

Your further explanation doesn't help I'm afraid. What else is in the document besides the questions? Are the questions automatically numbered? Are the questions each in a paragraph. Is there other text in the paragraph that you don't want? Where do you want to extract the questions to? Without seeing a sample of the actual document, it is not possible to provide a solution.
__________________
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
  #5  
Old 12-27-2020, 04:45 AM
Matrix2021 Matrix2021 is offline How can I extract text question between characters Windows 10 How can I extract text question between characters Office 2019
Novice
How can I extract text question between characters
 
Join Date: Jul 2020
Posts: 6
Matrix2021 is on a distinguished road
Default

Hello Gmayor.

Thanks for your feed back.

I have extract some of the paragraphs of the book. The questions are between the characters. The characters are highlighted the in red.

I have discovered more characters during the search in the book... Examples

. Question?
" Question?
Question? Note: this one do not have a character in front of the question.
  • Question? Note: the character in front is a bullet point.

and lastly.
, Question? Please Note: I am sorry, I have change my mind as most of the comm "," is path of the questions...

I am not sure if the sample word document would help. the book have 779 questions.

What do you suggest I do to move forward?

Thanks in advance Gmayor

Have a blessed one.
Attached Files
File Type: docx Several questions.docx (17.8 KB, 7 views)
Reply With Quote
  #6  
Old 12-27-2020, 09:37 PM
gmayor's Avatar
gmayor gmayor is offline How can I extract text question between characters Windows 10 How can I extract text question between characters Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

The following works with your example:


Code:
Sub ExtractQuestions()
'Graham Mayor - https://www.gmayor.com - Last updated - 28 Dec 2020
Dim oDoc As Document, oTarget As Document
Dim oRng As Range
Dim sQuestion As String
    Set oDoc = ActiveDocument
    Set oTarget = Documents.Add
    Set oRng = oDoc.Range
    With oRng.Find
        Do While .Execute(findText:=Chr(63))
            sQuestion = oRng.Sentences(1).Text
            sQuestion = Replace(sQuestion, Chr(63), "")    'eliminate question mark
            sQuestion = Replace(sQuestion, Chr(147), "")    'eliminate opening smart quote
            sQuestion = Replace(sQuestion, Chr(148), "")    'eliminate closing smart quote
            sQuestion = Replace(sQuestion, Chr(13), "")    'eliminate paragraph break
            oTarget.Range.InsertAfter sQuestion & vbCr    ' write to new document
            oRng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Set oDoc = Nothing
    Set oTarget = 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
  #7  
Old 12-28-2020, 09:57 AM
Matrix2021 Matrix2021 is offline How can I extract text question between characters Windows 10 How can I extract text question between characters Office 2019
Novice
How can I extract text question between characters
 
Join Date: Jul 2020
Posts: 6
Matrix2021 is on a distinguished road
Default

Solution Solved

Hello Gmayor.

The code you have provide for me have worked wonderful. I appreciate the extra step that you have given to me by allowing the list of words to transferred to another word document.

I just want to say.

Thank you very much.

Last edited by Charles Kenyon; 12-28-2020 at 02:04 PM. Reason: remove spam
Reply With Quote
Reply

Tags
vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I extract text question between characters Extract all characters from the nth number of a string Marcia Excel 2 02-24-2020 05:15 PM
How can I extract text question between characters How to extract text between <> LearnerExcel Excel 4 02-07-2018 06:11 AM
Formula to Extract text from a text string Haha88 Excel 2 11-14-2017 01:32 AM
How can I extract text question between characters Extract Line of Text w/ specific characters up to the paragraph character, send to Excel dmarie123 Word VBA 10 07-20-2015 12:16 AM
Macro to find text in between two characters and then format selected text? qcom Word 5 02-19-2015 11:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:37 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