Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 01-05-2024, 02:54 AM
hss001 hss001 is offline Selected Text when in a Table - Accessing Sentence Windows 10 Selected Text when in a Table - Accessing Sentence Office 2021
Novice
Selected Text when in a Table - Accessing Sentence
 
Join Date: Dec 2023
Posts: 20
hss001 is on a distinguished road
Default

So my codes below - highlighting where I want to retrieve the sentence. If I modify the selection the code doesn't function correctly.



Code:
    m_oWordDoc.Bookmarks("\StartOfDoc").Select
    m_oWordApp.Selection.Find.ClearFormatting

    With m_oWordApp.Selection.Find
        .Text = sPhraseToFind
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = bMatchCase
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False

        Do While m_oWordApp.Selection.Find.Execute = True

                sSentenceToSearch = m_oWordApp.Selection.Sentences(1)
                *** Above is incorrect if the found text is the last row in a table ***
                
                ' Additional Code

            End If
        Loop
    End With
Reply With Quote
  #17  
Old 01-05-2024, 08:31 AM
vivka vivka is offline Selected Text when in a Table - Accessing Sentence Windows 7 64bit Selected Text when in a Table - Accessing Sentence Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi! The codes in posts 11 & 15 select the sentence, which can be copied. Besides, the code in post 15 adds the sentence to a variable. And again your terminology needs clarification: what does "retrieve" mean? If you need to find all sentences with the key wd in the selected range, you can modify the code to highlight the sentences for further work with the highlights. Also, the code can be modified to select each found sentence one-by-one with a msgbox asking whether you want to procede, but note that if a code selects sth & pauses (not ends!) showing a msgbox, it doesn't mean you can work with the selection because the access to the text/screen will be denied.
Reply With Quote
  #18  
Old 01-10-2024, 02:25 AM
hss001 hss001 is offline Selected Text when in a Table - Accessing Sentence Windows 10 Selected Text when in a Table - Accessing Sentence Office 2021
Novice
Selected Text when in a Table - Accessing Sentence
 
Join Date: Dec 2023
Posts: 20
hss001 is on a distinguished road
Default

Hi, I managed to get this working. I found that extending the selection allowed capturing of the sentence, but was stopping my find working.

I used the below code to save the selection, expand the selection and copy the sentence, then reinstate the selection.

Code:
            Dim selCurrent As Object
            Set selCurrent = m_oWordApp.Selection.Range
        
            m_oWordApp.Selection.Expand Unit:=3 ' sentence
            
            sSentenceToSearch = m_oWordApp.Selection
        
            selCurrent.Select
Reply With Quote
  #19  
Old 01-10-2024, 03:59 AM
vivka vivka is offline Selected Text when in a Table - Accessing Sentence Windows 7 64bit Selected Text when in a Table - Accessing Sentence Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi! Good that you think you have found sth different from the codes proposed before. To retain the original selection, oRng.Duplicate could be used:
Code:
Sub Find_Wd_N_Select_Sent()
'Find the inputboxed wd & select the sentence it is in.
Dim oRng As range
Dim oRngD As range
Dim strWd As String
    
    Set oRng = selection.range
    Set oRngD = oRng.Duplicate
    strWd = InputBox("Enter the word to find", "FIND")
    With oRngD.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = strWd
        .Replacement.text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = False
        If .Execute Then
            oRngD.Select
            selection.Extend: selection.Extend: selection.Extend
'Or using your .Expand:
 '            selection.Expand Unit:=3
'Or:
'            selection.Expand Unit:=wdSentence
         End If
    End With
'This is to see the original selection:
    oRng.Select
 '    Set oRng = Nothing
 End Sub
That's it. As simple as this. Run the code line-by-line pressing F8 to see how it works.
The final note: if you want to go on searching for the next occurrence of the key wd in the original selection, you will keep finding the 1st occurrence. So, collapasing the range is better, I think. Our side view is always better than other people's front view of the same thing. Good luck!
PS. Another challenge: If you try to use .Expand function to select the last or only sentence in a table cell, you will get a wrong selection.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Selected Text when in a Table - Accessing Sentence accessing avery label table in macro littlepeaks Word VBA 10 08-09-2022 12:12 PM
Selected Text when in a Table - Accessing Sentence Footnote formatting>"Apply changes to selected text" not limiting changes to selected text Swarup Word 11 07-26-2022 01:51 PM
Selected Text when in a Table - Accessing Sentence Find and Replace Selected Text or Macro for finding selected text mrplastic Word VBA 4 12-20-2019 01:25 PM
Word for Mac 2011: Problems accessing Text Boxes programmatically techy291 Word VBA 0 07-16-2017 08:41 AM
Selected Text when in a Table - Accessing Sentence Select Text in Table but Table Gets Selected Too RBusiness Word 1 06-07-2011 04:26 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:51 AM.


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