MSWord and Spelling Errors

Started by JTaylor, October 30, 2017, 10:12:53 AM

Previous topic - Next topic

JTaylor

Not sure if this will be useful to anyone but thought I would share.   Have a situation where I have a large list of words and needed to know which were misspelled along with a related ID#.  It was too large for Word to handle it via a macro internally (probably could figure it out eventually) so thought I would try it in WinBatch (thanks to those who have previously posted Word scripts).  For what it is worth (open to suggestions for making it better)....

Jim

Code (winbatch) Select

DirChange(DirScript())

tname = "fix_title.txt"
gname = "good_words.txt"
bname = "bad_words.txt"

og    = FileOpen(gname,"WRITE")
ob    = FileOpen(bname,"WRITE")

Wordobj         = ObjectCreate ("Word.Application")
Wordobj.Visible = @TRUE
Docobj          = Wordobj.Documents
Docobj.Add
oSelection      = Wordobj.Selection
docSource       = Docobj.Application.ActiveDocument

txt             = StrReplace(FileGet(tname),@CRLF,@CR)
icnt            = ItemCount(txt,@CR)

For x = 1 to icnt

  line = ItemExtract(x,txt,@CR)
  id = ItemExtract(1,line,@TAB)
  word = StrTrim(ItemExtract(2,line,@TAB))
  If word == "" Then Continue
  nword = StrClean(word,"0123456789","",@TRUE,1)   ;Word seems to give a pass to any set of characters if they have numbers in them

  oRange = docSource.Range()
  cnt = oRange.Delete()
  oSelection.TypeText(nword)
  good = 1

  ForEach rng In docSource.SpellingErrors
     good = 0
     bad_word = rng.Text
     FileWrite(ob,id:@TAB:word)
  Next

  If good == 1 Then FileWrite(og,id:@TAB:word)

  If IsKeyDown(@CTRL&@SHIFT) Then x = 100000000

Next

:CANCEL
FileClose(og)
FileClose(ob)
Docobj.Close
Wordobj.Quit