al4629740
asked on
Prompt user to close Word document if its open
The following code is a spell check program and periodically I get an error that says "Cannot open clipboard"
The only way I have seen to avoid this error when I run it from my form is to check to see if MS Word is currently open and force the user to close it. Is there a way to check to see if word is currently open and then prompt the user to close their documents before proceeding?
The only way I have seen to avoid this error when I run it from my form is to check to see if MS Word is currently open and force the user to close it. Is there a way to check to see if word is currently open and then prompt the user to close their documents before proceeding?
Public Function CheckSpelling() As String
Dim objWord As Object
Dim objDoc As Object 'Word.Document
Dim strResult As String
Dim ctl As Control
On Error GoTo ErrHandler
Const QUOTE = """"
' Create a Word document object
If TypeName(objWord) <> "Nothing" Then
' Word is already open
Set objWord = GetObject(, "Word.Application")
Else
' Create an instance of Word
Set objWord = CreateObject("Word.Application")
End If
CoAllowSetForegroundWindow objWord, 0
Select Case objWord.Version
'Office 2000 and later
Case "9.0", "10.0", "11.0", "12.0", "13.0", "14.0", "15.0", "16.0"
Set objDoc = objWord.Documents.Add(, , 1, True)
'Office 97
Case "8.0"
Set objDoc = objWord.Documents.Add
Case Else
MsgBox "Sorry but your version of Word seems to be " & QUOTE & objWord.Version _
& QUOTE & " and that version is not currently supported.", vbOKOnly + vbExclamation, "Spelling Checker"
Exit Function
End Select
Set objDoc = objWord.Documents.Add
objWord.WindowState = 2 ' wdWindowStateMinimize
objWord.Visible = True
objWord.Activate
' Assign the text to the document and check spelling
With objDoc
.Content.Paste
.Activate
.CheckSpelling
' After the user has made changes, use the clipboard to
' transfer the contents back to the text box
Clipboard.Clear
.Content.Copy
CheckSpelling = Clipboard.GetText
' Close the document and exit Word
.Saved = True
strResult = Left(.Content, Len(.Content) - 1)
' Reformat carriage returns
strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))
For Each ctl In AddRegistration.Controls
If TypeOf ctl Is TextBox Then
If gstrControlName = ctl.Name Then
' If ctl.Text = strResult Then
If AddRegistration.Controls(gstrControlName).Text = strResult Then
' There were no errors, so let the user know that the spelling was checked
'MsgBox "No changes made", vbInformation + vbOKOnly, "Spelling Checker"
Exit For
End If
End If
End If
Next
.Close
End With
Set objDoc = Nothing
objWord.Visible = False
objWord.Quit
Set objWord = Nothing
Exit Function
Exit Function
ErrHandler:
Screen.MousePointer = vbNormal
Select Case err
Case 91, 429
MsgBox "MS Word cannot be found!", vbExclamation
Case Else
Call Error_Handle_Spell_Check(DataGridOrganization, "AddHours", "Spell Check", err.Description, err.HelpFile, ServerTime)
End Select
End Function
In order to run this code, you would need to have Word open, so you would actually need to check to see if any other Word files are open (besides the file containing this code).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The code ran ok but I opened a word doc ahead of time and it stayed open when the code finished.
Does that matter as long as you don't get any errors?
ASKER
I was thinking the same. I’ll give this a shot and see how it goes.