There are several problems:
- goRight() returns a boolean to indicate success, not the selected string.
CapsTest
is a string, not a boolean, so it cannot be used as the loop condition.- How did you know the code wasn't working? Perhaps you intended to use the view cursor, which would cause the visible cursor to move. (However a text cursor is probably better).
- The code always ignores the first paragraph, which may be intentional but seems strange.
- There are a lot of unused variables, and the capitalization is inconsistent.
Here is working code:
' Find the first paragraph in the document that begins with a capital letter. Sub Find_Capitalized_Paragraph Dim oDoc As Object Dim oCursor As Object Dim Proceed As Boolean Dim CapTest As String oDoc = ThisComponent oCursor = oDoc.Text.createTextCursor() oCursor.gotoStart(False) Do oCursor.goRight(1, True) CapTest = oCursor.getString() If CapTest <> "" And CapTest = UCase(CapTest) Then Goto TestPreviousEnd oCursor.gotoNextParagraph(False) Loop While CapTest <> "" MsgBox("No results.") Exit Sub TestPreviousEnd: MsgBox("Found result: """ & CapTest & """") End Sub
So if the document contains:
a b C d
Then the macro prints Found result: "C"
.
Be sure to check out Andrew Pitonyak's macro document. It contains many excellent examples.