View Single Post
 
Old 01-21-2021, 01:37 PM
rekent rekent is offline Windows 10 Office 2016
Novice
 
Join Date: May 2014
Posts: 23
rekent is on a distinguished road
Default

Tracked it down. GetDocRange was returning an incorrect value for oRng because when the SUMMARY section was missing, "SUMMARY" was not found and lngStart remained at zero. But the next section header "DRAWINGS" was found, creating a range from document start to the beginning of "DRAWINGS." The nested If statements at the end of this function seem to do the trick, and I haven't been able to come up with a scenario yet in which they break other functionality.

Code:
Function GetDocRange(startWord As String, endWord As String) As Range
Dim oRng As Word.Range, lngStart As Long, lngEnd As Long
  Set oRng = m_oDocCurrent.Range
  With oRng.Find
    .Wrap = wdFindStop
    .MatchCase = True
    .Text = startWord
    If .Execute Then lngStart = oRng.End
    oRng.End = m_oDocCurrent.Range.End
    .Text = endWord
    If .Execute Then lngEnd = oRng.Start
    If startWord = "ABSTRACT" Then lngEnd = m_oDocCurrent.Range.End
    If lngStart = "0" Then
      Set GetDocRange = m_oDocCurrent.Range(lngStart, lngStart)
    Else
      If lngEnd > lngStart Then
        Set GetDocRange = m_oDocCurrent.Range(lngStart, lngEnd)
      End If
    End If
  End With
End Function
Reply With Quote