View Single Post
 
Old 06-06-2020, 03:16 AM
scienceguy scienceguy is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default Determine if a Phrase is on a Line by Itself

Hello,

I am trying to determine by code if a phrase is on a line by itself. The phrase will be “QUESTION n”, where "n" is a number, and the phrase is on a line by itself. Sample text would look like:

QUESTION 1
What is the distance between the earth and the moon?


I can use “. MatchWildcards” to find the phrase, Question n, with no issues:

Code:
'total QUESTIONS
Dim wdApp As Object
Dim wdDoc As Object
Dim intQuesCount As Integer
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = False
Set wdDoc = wdApp.Documents.Open(FileName:="C:\Users\roy\Desktop\sample.docx", AddToRecentFiles:=False, Visible:=False)
intQuesCount = 0
Set oRng = wdDoc.Range
With oRng.Find
    .Text = "<QUESTION [0-9]{1,}"
    .MatchWildcards = True
    .MatchCase = True
    While .Execute
        intQuesCount = intQuesCount + 1 'count the number of occurrences
    Wend
End With
MsgBox "There are " & intQuesCount & " occurrences."
It appear that using “^p” to find a paragraph mark is not compatible with “.MatchWildcards”. So, how do you determine if your found phrase is on a line by itself? Many thanks any advance for guidance!

Thanks,
Roy
Reply With Quote