Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-15-2023, 08:29 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 Selected Text when in a Table - Accessing Sentence

Hi,

my VBA code is running in Excel. It runs a word search in a Word document, and from the results returns the Sentence of each match.

So, the below code works fine if the found text is in a paragraph:



Code:
oWordApp.Selection.Sentences(1)
But, when the found text is in a table it just returns 2 characters - a space and a newline (I think)

I can't figure out how to access the sentence of the current selection, when it's in a table.
Can anyone help?

Thanks
Reply With Quote
  #2  
Old 12-15-2023, 03:52 PM
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! 'oRng.Sentences(1).Select' works both in text and tables IF (according to my tests) there is more than one sentence in a paragraph. Note that Word has an unclear idea of a sentence, e.g.: "The book was written by J. Smith, ..." can be treated as two sentences: (1) "The book was written by J." and (2) "Smith, ..."

Keeping in mind the IF described above, you can also use:
oRng.Select
selection.Expand Unit:=wdSentence

Another method (although not a one-liner), which "doesn't care about" the IF, is:
selection.Extend
selection.Extend
selection.Extend
Note that three lines must be used: line 1 activates the extend function, line 2 extends the selection to the current word, line 3 extends the selection to the current sentence.
If you wanted to select the current paragraph, the fourth 'selection.Extend' would have to be added.
Reply With Quote
  #3  
Old 12-16-2023, 12:28 AM
gmayor's Avatar
gmayor gmayor is offline Selected Text when in a Table - Accessing Sentence Windows 10 Selected Text when in a Table - Accessing Sentence Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 issue relates to how many sentences there are in the table cell and which sentence the selection occurs in. If the selection is in the last sentence or only sentence in the cell, the whole cell is selected. You can test for this and make the necessary adjustment e.g.


Code:
Dim orng As Object
    Set orng = oWordApp.Selection.Range
    If orng.Information(12) Then
        If orng.Sentences(1) = orng.Cells(1).Range.Sentences.Last Then
            Set orng = orng.Cells(1).Range.Sentences.Last
            orng.End = orng.End - 1 'omit the table cell end character
            orng.Select
         Else
            orng.Sentences(1).Select
        End If
    Else
        orng.Sentences(1).Select
    End If
__________________
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
  #4  
Old 12-19-2023, 09:22 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

Thanks for your replies.

@gmayor, I couldn't get your code to work.

So where I currently have the line below, how would this fit in with your code:
Code:
sStringVariable = oWordApp.Selection.Sentences(1)
Reply With Quote
  #5  
Old 01-03-2024, 09:22 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

Is anyone else able to advise on this?

I don't really understand the replies.

Basically, all I want to do is use the below code to get the whole sentence of the selected word. BUT - this doesn't work if the word is in a table

Code:
sStringVariable = oWordApp.Selection.Sentences(1)
Reply With Quote
  #6  
Old 01-03-2024, 09:32 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 gmayor.

I tried this and it works fine if the sentence is not the last cell. But if the sentence is the last cell it returns weird characters (I think they are end of table chars).

Thanks


Quote:
Originally Posted by gmayor View Post
The issue relates to how many sentences there are in the table cell and which sentence the selection occurs in. If the selection is in the last sentence or only sentence in the cell, the whole cell is selected. You can test for this and make the necessary adjustment e.g.


Code:
Dim orng As Object
    Set orng = oWordApp.Selection.Range
    If orng.Information(12) Then
        If orng.Sentences(1) = orng.Cells(1).Range.Sentences.Last Then
            Set orng = orng.Cells(1).Range.Sentences.Last
            orng.End = orng.End - 1 'omit the table cell end character
            orng.Select
         Else
            orng.Sentences(1).Select
        End If
    Else
        orng.Sentences(1).Select
    End If
Reply With Quote
  #7  
Old 01-04-2024, 03:40 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, hss001! I'm repeating my answer: after finding & selecting your word use the following three lines:
Code:
selection.Extend
selection.Extend
selection.Extend
Reply With Quote
  #8  
Old 01-04-2024, 07:28 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, this method works perfectly in isolation. The issue is that the code I've inherited does a Find using a Selection. So, modifying the selection by extending it breaks the Find.

Quote:
Originally Posted by vivka View Post
Hi, hss001! I'm repeating my answer: after finding & selecting your word use the following three lines:
Code:
selection.Extend
selection.Extend
selection.Extend
Reply With Quote
  #9  
Old 01-04-2024, 07:54 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

hss001, do you want the code to msgbox, select or highlite the found sentence(s)? If the found senetence is selected the original selection will be always broken. Sorry, but I don't quite understand your aim.
Reply With Quote
  #10  
Old 01-04-2024, 08:08 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

Ideally, I want to "select" the "sentence" of the selected text - this will work without needing to modify any other code.

So the below line works fine, EXCEPT when the text is in a table and also the last row - as it picks up end of row characters.

Code:
sStringVariable = oWordApp.Selection.Sentences(1)
@gmayor's code - it doesn't appear to work correctly, in that it doesn't detect if the cell is in the last row.

So, all I need to do is detect if the selection is in the last row of a table, and then strip off the last 2 chars.




Quote:
Originally Posted by vivka View Post
hss001, do you want the code to msgbox, select or highlite the found sentence(s)? If the found senetence is selected the original selection will be always broken. Sorry, but I don't quite understand your aim.
Reply With Quote
  #11  
Old 01-04-2024, 08:22 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

Hss001, try this one:
Code:
Sub Find_Wd_N_Select_Sent()
'In the selected range, find the inputboxed wd & select the 1st sentence it is in.

Dim oRng As range
Dim strWd As String
    
    Set oRng = selection.range
    strWd = InputBox("Enter the word to find", "FIND")
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = strWd
        .Replacement.text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = False
        If .Execute Then
            oRng.Select
            selection.Extend: selection.Extend: selection.Extend
       End If
    End With
    Set oRng = Nothing
End Sub
Reply With Quote
  #12  
Old 01-04-2024, 08:36 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

Thanks for your help with this.

I have something similar, but using the Find command on the Selection.
So if the Selection is modified the Find doesn't work 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
  #13  
Old 01-04-2024, 08:49 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

I'm glad I've managed to help you!
Reply With Quote
  #14  
Old 01-04-2024, 08:53 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

I still have the issue yet but I think a solution is nearer..

Quote:
Originally Posted by vivka View Post
I'm glad I've managed to help you!
Reply With Quote
  #15  
Old 01-04-2024, 10:45 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

You are closer to what?
Code:
Sub Find_Wd_N_Select_Sent()
'In the selected range, find the inputboxed wd & select & msgbox the 1st sentence it is in.

Dim oRng As range
Dim strWd As String
Dim rSentToSearch As range
    
    Set oRng = selection.range
    strWd = InputBox("Enter the word to find", "FIND")
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = strWd
        .Replacement.text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = False
        If .Execute Then
            oRng.Select
            selection.Extend: selection.Extend: selection.Extend
       End If
       Set rSentToSearch = selection.range
    End With
    MsgBox rSentToSearch
    Set oRng = Nothing
    Set rSentToSearch = Nothing
End Sub
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 02:26 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