0

I'm trying to run a spell check on a string using word, but I don't want to have to go through each word manually and determine the correct spelling. Is there a way to do this automatically, so that it corrects all the misspelled words on its own? Here is my code so far, this works but I have to go through each word and hit change...

If Address.Length > 0 Then
    Dim word_server As New Word.Application
    word_server.Visible = False
    Dim doc As Word.Document = word_server.Documents.Add()
    Dim rng As Word.Range

    rng = doc.Range()
    rng.Text = Address
    doc.Activate()
    doc.CheckSpelling()
    Address = doc.Range().Text
    doc.Close(SaveChanges:=False)
    word_server.Quit()
    doc = Nothing
    rCell.Value = Address
End If
Vector
  • 3,066
  • 5
  • 27
  • 54
Chrisetiquette
  • 311
  • 1
  • 4
  • 22

2 Answers2

1

Use the GetSpellingSuggestions function to bypass the GUI. to see if there are any spelling suggestions, and then you can set it to the first suggestion (which could be a bad idea).

How do you want to determine what the correct spelling is? Should "spon" be spoon, span, spin, spun, or son? This code optimistically assumes that the first suggestion (if one exists) is the correct solution.

Declare:

Dim oError As Word.Range

And then replace:

  doc.Activate()
  doc.CheckSpelling()

with:

For Each oError In rng.SpellingErrors
    If oError.GetSpellingSuggestions.Count > 0 Then
        oError.Text = oError.GetSpellingSuggestions().Item(1).Name
    Else
        'Uh oh, no suggestions, do something here.
    End If
Next
GuitarPicker
  • 316
  • 2
  • 11
  • Your code gets this error: An unhandled exception of type 'System.InvalidCastException' occurred in CSGaddresser.exe Additional information: Unable to cast COM object of type 'System.__ComObject' to class type 'System.String'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface. – Chrisetiquette Jun 30 '16 at 16:48
  • I take it from the tags that you are making a .NET app to call this. The code I posted is for Word's internal VBA. .NET isn't my forte, but I assume you may need to include some more object references. Does declaring oError as Word.Range help? (I'm editing my answer accordingly) – GuitarPicker Jun 30 '16 at 16:57
  • Tried changing it to this: oError.Text = oError.GetSpellingSuggestions().Item(1).ToString() But that just replaces the string with "System.__ComObject" – Chrisetiquette Jun 30 '16 at 17:03
  • and no, changing it to Word.Range did not help. – Chrisetiquette Jun 30 '16 at 17:05
  • Looking over the object model, .Item(1).Name is a string (answer edited again). If that doesn't work, perhaps someone can edit this answer to convert the code snippet from Word VBA to .NET. – GuitarPicker Jun 30 '16 at 17:16
  • I just found [this site](http://www.codeproject.com/Tips/878805/Interoperability-with-NET-Platform) where someone is using GetSpellingSuggestions from .NET and doing almost exactly what you are attempting. – GuitarPicker Jun 30 '16 at 17:24
  • It worked! But for whatever reason its putting in hidden characters. For, example, the field is now enclosed in quotes "field" but the quotes are not visible, and there is also a carriage return??? Can suggestions on how to fix that? – Chrisetiquette Jun 30 '16 at 17:56
  • I'm not sure I understand. Can you give a sample and the data type of your input (`Address`) and the resulting output data? – GuitarPicker Jun 30 '16 at 18:00
  • I need to save the excel file as a text tab delimited in order to import the data into my postal software. However, when I open the text file, the address field (which i am spell checking) is now enclosed in qutoes, the quotes are not visible in excel only when i save to text file. I can do a find and replace but i would like to prevent this altogether. Also, its putting in a carriage non-visible carriage return. So, when i import the data into my postal program it see's one record as two because of the carriage return.... – Chrisetiquette Jun 30 '16 at 18:08
  • That sounds like a separate question ([see this one](http://stackoverflow.com/questions/11501531/saving-a-excel-file-into-txt-format-without-quotes)). You're going to have to clean up your text file, find a different export format, or find a different way to grab it. Perhaps you can skip the export altogether if you can grab it through automation, much like how you are putting the text directly into Word. – GuitarPicker Jun 30 '16 at 18:13
  • I suppose it is a separate question but I don't see why the said code would create that type of results....? – Chrisetiquette Jun 30 '16 at 22:13
  • The related question in my previous comment indicates that this can happen when there are commas in your exported data. I suspect that's the way the data in your `Address` variable looks. Since you didn't indicate where `Address` comes from, what data type it is, or show any samples of it, I assumed that it was a plain string that was already in the right format. Anything further is outside the scope of the question that was asked. If this has been helpful, please mark it as the answer. Thanks. – GuitarPicker Jul 05 '16 at 20:23
0

These websites might help. The example code in the website shows how to call Word to spell check. You should be able to modify it, to use with your code.

http://www.vb-helper.com/howto_net_spellcheck.html

http://www.vbforums.com/showthread.php?307151-SPELL-CHECK-and-WORD

Cal-cium
  • 654
  • 12
  • 22