Consulting

Results 1 to 5 of 5

Thread: Solved: Using Document.Words Object in VBA

  1. #1

    Solved: Using Document.Words Object in VBA

    I have written the below code to write all words from MainDocument.doc to ChangeLog.txt. It should bypass the punctuation marks. But it isn't working. I don't know why. Any idea?

    [VBA]
    Sub DictionaryCompare()
    Dim docCurrent As Document
    Dim LookupWord As Object

    sMainDoc = "C:\Dictionary\MainDocument.doc"
    Set docCurrent = Documents.Open(sMainDoc)
    docCurrent.Activate

    For Each LookupWord In docCurrent.Words
    If LookupWord <> vbCr And LookupWord <> "." And LookupWord <> "," And LookupWord <> ";" And LookupWord <> "?" And LookupWord <> "'" Then
    Open "C:\Dictionary\ChangeLog.txt" For Append As #2
    Write #2, Trim(LookupWord) & "," & FontColor & "," & PartOfSpeech
    Close #2
    End If
    Next
    End Sub
    [/VBA]

  2. #2
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Select case statement within your for loop would be much easier to read, and probably easier to troubleshoot instead of a complex If statement... for example:
    [vba]
    Select Case LookupWord
    Case vbCr, ".", ",", ";", "?", "'"
    'do nothing
    Case else
    'write to the file
    Open "C:\Dictionary\ChangeLog.txt" For Append As #2
    Write #2, Trim(LookupWord) & "," & FontColor & "," & PartOfSpeech
    End Select
    [/vba]

  3. #3
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Frosty,

    You probably want to include Close #2 after the Write and before your End Select.

    Cheers,
    Ron
    Windermere, FL

  4. #4
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Ron is absolutely right-- sorry, wrote that example a little quickly.

  5. #5
    Thanks all, especially Frosty. The case thing worked :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •