Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Document.Words Object in VBA Word

Status
Not open for further replies.

talhamoin

IS-IT--Management
Mar 3, 2011
14
AE
Hello

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?

Code:
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
 


Hi,

Where does FontColor & PartOfSpeech get defined and assigned?


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 


This seems to 'work' (without the file logging)
Code:
    For Each LookupWord In docCurrent.Words
        Select Case Trim(LookupWord)
            Case vbCr, ".", ",", ";", "?", "'", Chr(34), Chr(148)
            Case Else
                Debug.Print Trim(LookupWord)
'                Write #2, Trim(LookupWord) & "," & FontColor & "," & PartOfSpeech
        End Select
    Next

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi,

at first it seemed to me that you want to make some sort of dictionary. But the output file is called "ChangeLog". Your code and the output file's folder name again let me assume that you want to make a dictionary.

IMO two kinds of what Word regards as a word do not go into a dictionary: numbers and punctuation marks. Furthermore I assume that only the words of the main text will go into a dictionary, as headers and footers are most likely already checked.

With these assumptions I would change the loop like this:
Code:
Dim x As Object
For Each x In ActiveDocument.StoryRanges(wdMainTextStory).Words
  If Len(Trim(x)) > 1 Then
    If Not IsNumeric(x) Then
      ...
    End If [COLOR=gray]'IsNumeric[/color]
  End If [COLOR=gray]'Len>1[/color]
Next x
I'm just guessing that your code didn't work because an IF does not "like" so many ANDs.

HTH anyway.

Markus
 
Hi thank you all especially SkipVought. Using Case statement really helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top