1

I'm trying to write a macro, and I need to skip to the next paragraph, where I will test the first letter for capitalization. I've spent hours and have only found inaccurate or hard to follow documentation for something that I think should be simple. Any direction would be appreciated. So far I have:

SUB FIND_PARAGRAPHS

Dim vDescriptor
dim Doc as object
dim Replace as object 
dim oCursor 
dim Proceed as Boolean
dim CapTest as String

vDescriptor = ThisComponent.createSearchDescriptor()
doc = ThisComponent
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

' test para begin; if capital test previous end 

oCursor = Doc.Text.createTextCursor()
Do 
    oCursor.gotoNextParagraph(false) 'NW
    CapTest = oCursor.goRight(1, true) 'NW
    if CapTest = ucase(CapTest) Then goto TestPreviousEnd
Loop While CapTest

TestPreviousEnd:

END SUB
Paul B.
  • 69

1 Answers1

0

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.

Jim K
  • 4,439