Looking for advise on coding or alternative approach.
Using Microsoft Word 2013 for spell checking. Getting occasional error returned. Some users get the error every time, some none of the time, and some occasionally.
Here's the gist of the code:
TRY
lole_Spell = CREATE OLEObject // Create an OLE object
ll_RC = lole_Spell.ConnectToNewObject('Word.Application') // Connect to Word
IF ll_RC <> 0 THEN // Handle no-go
DESTROY lole_Spell
RETURN ll_RC
END IF
lole_Spell.Application.Visible = TRUE // Make Word visible
lole_Spell.Application.WindowState = 1 // Maximize Word
lole_Spell.Documents.Add() // Add a Word document
f_BringWindowToTop(' - Word') // Make Word the top window - application function
lole_Spell.Activate // Make Word active
lole_Range = lole_Spell.Selection.Range // Get Word selection range
lole_Range.Text = ls_Block // Copy our text to the Word document
lole_Document = lole_Spell.ActiveDocument // Get reference to our document
lws_State = gw_frame.WindowState // Remember our PB app window state
gw_frame.WindowState = Minimized! // Minimize our PB app
lole_Document.CheckSpelling() // Check the spelling
YIELD() // Wait a bit
gw_frame.WindowState = lws_State // Reset our PB app window state
lole_Range.WholeStory // Select all text in the Word document <<== error occurs here
ls_Block = lole_Range.Text // Copy back to our string
lole_Document.Saved = TRUE // Reset the Word dirty flag
lole_Document.CLOSE(FALSE) // Close the Word document
lole_Spell.Application.Quit // Quit Word
lole_Spell.DisconnectObject() // Disconnect OLE reference object
YIELD() // Wait a bit
DESTROY lole_Spell // Destroy the OLE reference object
CATCH(OLERuntimeError MyOLEError)
MessageBox('Spell Check Error', 'Error when spell checking data in Word' + &
'~r~n' + MyOLEError.GetMessage())
RETURN -1
END TRY
As for when you issue the Activate, my experience has been to always issue one if there is the potential for any time to pass by before you attempt to have another interaction with the OLEObject...just in case the user has done something that throws off the previous activation (opens another doc, closes your doc etc.). You may also wish to use the ActiveDocument property of the Application object to check if the "correct" document, the one you want to work with, is currently active. Keep in mind too that there is an Activate method on the Application object and an Activate method on the Document object (which allows you to specify which document to Activate). You can also cycle through the Documents collection object to ensure that the document you want to work with is in fact still open.
Regards,
Mark
This morning I re-installed 32 bit office to do some testing: The same problem still exists. I also tried to do an Activate of the Application, but that didn't solve the problem either. (BTW. you cannot use that if things are invisible, the activate blows up, so that would cause extra 'flashing').
It all works pretty well for the method used on the richtext edit, but the failing behaviour that I'm describing now, is for the dw control, where I'm doing a .Paste() of the copied text onto the OLE object. That worked pretty well before, but now only sometimes. Even after using my new method of using the Move() function, it failed a few times (after fresh re-install of 365 32 bit and a reboot). Started to work after saving my u_dw object again (not really having any change) and now it always works.
So for now I'm hanging on to the Move() method and not minimizing and setting invisible.
To be honest: I'd rather not use Word at all for this. MS used to break things in newer versions, but now they seem to do it on same versions. I've got an I-pad for work and I'm gettin office updates like every 3 days. What are theys people doing?
:)