PDA

View Full Version : [SOLVED:] Controlling word spell checker via excel



Andy2011
02-28-2015, 04:17 AM
Hi

Does anyone know if you can run the spell checker function in word from a userform in excel?

gmayor
02-28-2015, 10:21 PM
You can call Word VBA from Excel, but what would be the point of this? Under what circumstances would it be necessary to check the spelling of a Word document from Excel?



Dim wdApp As Object
Dim wdDoc As Object
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wdDoc = wdApp.Documents.Add
wdDoc.Range.Text = "Thisss is a tessst"
wdDoc.CheckSpelling
Set wdDoc = Nothing
Set wdApp = Nothing

Andy2011
03-01-2015, 07:48 AM
Thanks gmayor.

I have a userform in excel that exports data into a word userform and then spell checks the entries made. Whilst some are taken from listboxes etc. and so are 'fixed', some are 'free text' textboxes and so require spellchecking. I'm sure it would probably be better checking them in excel first and then sending across but I don't know how to and this is working for me at the moment....

Andy2011
03-01-2015, 10:32 AM
The code above causes excel to freeze and so I doctored it a little and it works. As per usual though one solution leads to another question...

I can't get excel to close once the data passes to word, at least not until the spellcheck has run its course, which in itself isn't an issue as such. The problem is that in the meantime the focus remains on excel and the user manually has to click on the newly created document to view it.

How can shift the focus onto the word document?

gmayor
03-01-2015, 11:05 PM
You won't be able to close Excel while you are running code in Excel, however the following should bring Word to the top (provided the VBA editor is closed).



Option Explicit
Dim THandle As Long
Private Declare Function BringWindowToTop Lib "user32" (ByVal _
hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As Any, _
ByVal lpWindowName As Any) As Long

Sub Test()
Dim wdApp As Object
Dim wdDoc As Object
Dim iRet As Long

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wdDoc = wdApp.Documents.Add
THandle = FindWindow(vbEmpty, "Microsoft Word")
If THandle = 0 Then
MsgBox "Word is not running"
Else
iRet = BringWindowToTop(THandle)
End If
wdDoc.Range.Text = "Thisss is a tessst"
wdDoc.CheckSpelling
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub

Andy2011
03-02-2015, 01:11 AM
Thanks again gmayer.

Ironically I am writing this code in Excel 2010 but the machines it will be run on use Excel 2003. When I tested it last night the word document automatically came to the top??? and Excel closed as expected once the code was complete. I shall surely keep the code above for reference though. And sorry my previous post was a little badly written and rambling, I kinda had an idea that Excel wouldn't close until the code had finished, it was only ever about bringing Word to the top.

I don't suppose you could help with my other query on this forum, about an alternative to
.selection.wholestory that works! I'm sure it's got something to do with the fact that I'm late binding...but i'm not that good to work it out...;-)