How do I wait for spell check to finish in a macro

Hi all. I have a macro that I use in Writer for formatting a document. One of the tasks I do is to call the built-in spell checker, then lastly allow the user to save the document as something else.

The problem is, the saveas dialogue box is called as soon as the spell check in called, it doesn’t wait for it to be closed first.

Is there any document property that will allow me to see that it is in spell check mode, or a way to query the spell check window state?

Version: 25.2.3.2, Windows 10

I only know of intricate ways.
One of them:

  1. Add a top-level window listener.
  2. Open a dialog with a macro.

The listener can then determine the moment the dialog box is opened and closed.

The Useful Macro Information For OpenOffice.org By Andrew Pitonyak book has examples of this technique from A. Pitonyak and @ms777 (section “Manipulating the Options dialog”)

@sokol92, thanks for this, I’ll have a look at it

@sokol92, this worked a treat, thanks. I used the “Finding an open window” code from his pdf file. For everyone else, here’s my code that calls it and Andrew Pitonyak’s function.

'Wait for the Spellcheck to finish before continuing
windowTitle = GetOpenWindow("Spelling")
While Not IsNull(windowTitle)
	windowTitle = GetOpenWindow("Spelling")
	Wait(250)
wend
pdfFilePath = SaveFinalFile 'return an empty string if it was cancelled
'------------------- GetOpenWindow
REM Iterate through the open dialogs and find the one that starts with
REM sTitle.
Function GetOpenWindow(sTitle as String) As Object
Dim oToolkit
Dim lCount As Long
Dim k As Long
Dim oWin

	oToolkit = Stardesktop.ActiveFrame.ContainerWindow.Toolkit
	lCount = oToolkit.TopWindowCount
	
	For k = 0 To lCount - 1
		oWin = oToolkit.getTopWindow(k)
		If HasUnoInterfaces(oWin, "com.sun.star.awt.XDialog") Then
			If left(oWin.Title, len(sTitle)) = sTitle Then
				GetOpenWindow = oWin
				Exit Function
			EndIf
		EndIf
	Next k
	
End Function
2 Likes

Lifehack: replacing EndIf with End If will improve the color scheme of your message. :slight_smile:

Thanks for the code.
When searching for a dialog by title, there is a localization issue that English-language macro writers do not always take into account.
It is better to use a locale-independent approach when searching for a dialog box.

Function GetOpenWindow2(Byval accessibleId as String) As Object
  Dim oToolkit as Object
  Dim k As Long
  Dim oWin as Object
  
  oToolkit = Stardesktop.ActiveFrame.ContainerWindow.Toolkit
  On Error Goto ErrLabel
  For k = 0 To oToolkit.TopWindowCount - 1
    oWin = oToolkit.getTopWindow(k)
    If HasUnoInterfaces(oWin, "com.sun.star.awt.XDialog") Then
      If oWin.AccessibleContext.AccessibleId = accessibleId Then
         GetOpenWindow2 = oWin
         Exit Function
      End If
    End If
ErrLabel:		
  Next k
End Function

Sub Test
  Dim oWin as Object
  oWin = GetOpenWindow2("SpellingDialog")
  If Not (oWin is Nothing) Then
    Msgbox "Found dialog with title: " & oWin.Title
  Else
    Msgbox "Dialog not found"
  End If  
End Sub 
3 Likes