익명 05:09

In LibreOffice Basic, how can I stop my macro crashing when a control is absent?

In LibreOffice Basic, how can I stop my macro crashing when a control is absent?

In a previous post I presented macro code to capture the contents of a text frame. I am trying to add error processing, the aim is to fail silently if anything is missing.

I now have this code fragment:

On Error GoTo End
try:
    
    oFrame = document.TextFrames
    if IsError(oFrame) or isNull(oFrame) then exit sub

    oField = oFrame.getByName("xxDocTitle") ‘ Exception here
    
    if IsError(oField) then exit sub
    if isNull(oField) or isNull(oField.String) or oField.String = "" then exit sub
    filename = oField.String & ".odt" 
Catch:
    ' Exit silently
end sub

it works if the text frame is empty but always produces an error dialogue if the frame is absent:

An exception occurred

Type: com.sun.star.container.NoSuchElementException

Message:.

Can I do anything to suppress the exception message?



Top Answer/Comment:

In LibreOffice Basic, exception handling is done with an On Error GoTo code block, possibly with Resume.

The following is an outline, rather than actual working script; you'll need to modify it for your purposes. Note that Resume might not be used.

Sub ExampleSetFilename
On Error GoTo MyErrorHandler  ' This looks for a specific label
    oFrame = document.TextFrames
    oField = oFrame.getByName("xxDocTitle") ‘ Error might occur here    
    filename = oField.String & ".odt" 
    exit sub
MyErrorHandler: ` Label for location to go on error
    MsgBox "Oops... there was an error, which was ignored.",  0,  "Error"
    Resume Next ` continue in code block where error occurred, e.g., inside loop
End Sub
상단 광고의 [X] 버튼을 누르면 내용이 보입니다