PDA

View Full Version : Solved: Getting user input at run time from own created userform



talhamoin
02-24-2011, 01:06 PM
I have written this code to change the font color of a word on the basis of selection the user makes.

This should only ask for the user selection when the "If varWordCount > 1 And LookupWord <> Chr(182) Then" condition is true. I want it to prompt the user with a dropdown to select a category. So the dropdown will have values Category 1, Category 2, Category 3 and Category 4.

Moreover, "LookupWord <> Chr(182)" doesnt seem to work too. I used this because I don't want the paragraph mark to be counted in the paragraph.

For Each LookupWord In docCurrent.Words
Set DictionRangeForCount = DictionRef.Content
With DictionRangeForCount.Find
varWordCount = 0
Do While .Execute(FindText:=LookupWord, Forward:=True, Format:=True, MatchWholeWord:=True) = True
varWordCount = varWordCount + 1
Loop
End With

If varWordCount > 1 And LookupWord <> Chr(182) Then
UserSelection = MsgBox(LookupWord + " has more than 1 occurrence", vbQuestion)
End If
Next

fumei
02-24-2011, 01:29 PM
"Moreover, "LookupWord <> Chr(182)" doesnt seem to work too. I used this because I don't want the paragraph mark to be counted in the paragraph. "

And what does Chr(182) have to do with a paragraph??????

1. Please use the VBA code tags when posting code.

2. please declare variables.

3. "I want it to prompt the user with a dropdown to " What dropdown?

4. please state what IS happening.

talhamoin
02-24-2011, 11:48 PM
Mr. Fumei, thanks for the response. I am sorry, I missed out so many things.

Here's the complete VBA code that I did so far.
Sub DictionaryCompare()
Dim sDictionDoc As String
Dim DictionRef As Document
Dim docCurrent As Document
Dim refWord As Object
Dim LookupWord As Object
Dim DictionRange As Object

sDictionDoc = "c:\Dictionary.doc"

Set docCurrent = Selection.Document
Set DictionRef = Documents.Open(sDictionDoc)
docCurrent.Activate

For Each LookupWord In docCurrent.Words
Set DictionRangeForCount = DictionRef.Content
With DictionRangeForCount.Find
varWordCount = 0
Do While .Execute(FindText:=LookupWord, Forward:=True, Format:=True, MatchWholeWord:=True) = True
varWordCount = varWordCount + 1
Loop
End With

If varWordCount > 1 And LookupWord <> Chr(182) Then
UserSelection = MsgBox(LookupWord + " has more than 1 occurrence", vbQuestion)
End If

Set DictionRange = DictionRef.Content
With DictionRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = LookupWord
.MatchWholeWord = False
.MatchCase = False
.Forward = True
End With

If DictionRange.Find.Execute Then
MsgBox (LookupWord)
End If
Next
DictionRef.Close
docCurrent.Activate
End Sub

3. As mentioned, that I want the user to be prompted with a my own user form. This form will have the word being looked up and a dropdown list box. That form needs to have a dropdown list. That list will have options "Category 1", "Category 2", "Category 3" and "Category 4". Suppose the user selects Category 1 and clicks OK, I will change the font color of that word.

4. Actually, I am not sure how to get values from that dropdown box.

fumei
02-25-2011, 10:10 AM
Basically I can repeat my last post.

Declare your variables. Are you using Option Explicit? If not, start to do so. DictionRangeForCount is not declared. UserSelection is not declared.

Again...WHAT dropdown? There is nothing here that indicates any dropdown.

"I want the user to be prompted with a my own user form. "

Then make one. That would be a good place to start.

Again, what has Chr(182) to do with anything?

And describe - clearly - what it is you want to happen.

"Suppose the user selects Category 1 and clicks OK, I will change the font color of that word."

WHAT word? It sounds like you want:

A userform that changes, putting each word from "c:\Dictionary.doc" into a Label (or something), one after the other, on the userform. Clicking OK does a search for that word in the document, changing the font (???) based on a selection of a dropdown.

What is: UserSelection = MsgBox(LookupWord + " has more than 1 occurrence", vbQuestion)

supposed to do? It does not seem to do anything. Do you realize that the value of UserSelection is a number??? It will always = 1.

talhamoin
02-27-2011, 09:06 AM
I used a workaround. Instead of creating a userform, I used InputBox. Here's the new shape of code:



Sub DictionaryCompare()
Dim sDictionDoc As String
Dim DictionRef As Document
Dim docCurrent As Document
Dim refWord As Object
Dim LookupWord As Object
Dim DictionRange As Object

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

For Each LookupWord In docCurrent.Words
sDictionDoc = "c:\Dictionary.doc"
Set DictionRef = Documents.Open(sDictionDoc)
Set DictionRangeForCount = DictionRef.Content

With DictionRangeForCount.Find
varWordCount = 0
Do While .Execute(FindText:=LookupWord, Forward:=True, Format:=False, MatchWholeWord:=True) = True
varWordCount = varWordCount + 1
Loop
End With

If varWordCount > 1 Then
UserSelection = InputBox("Please input the article of speech for " + LookupWord)
DictionRef.Close
Else
DictionRef.Activate
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = LookupWord
.MatchWholeWord = False
.MatchCase = False
.Forward = True
End With
If Selection.Find.Execute Then
Dim Complete As Integer
DictionaryRefPara = Selection.Paragraphs(1)
Complete = Len(DictionaryRefPara)
SpacePlace = InStr(DictionaryRefPara, " ")
PartOfSpeech = Right(DictionaryRefPara, Complete - SpacePlace)
DictionRef.Close
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Color = wdColorAqua
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchWholeWord = False
.MatchCase = False
.MatchWildcards = False
.Wrap = wdFindContinue
.Text = LookupWord
.Execute Replace:=wdReplaceAll
End With
Else
MsgBox (LookupWord + " is not defined in custom dictionary")
End If
End If
Next
docCurrent.Activate
End Sub


The contents of MainDocument.doc are: "A good quick brown". So, the "For Each LookupWord In docCurrent.Words" loop should execute for 5 times.

When LookupWord is 'A' it works fine and the control goes to InputBox. I am not concerned, at this stage, with this part of code:

If varWordCount > 1 Then
UserSelection = InputBox("Please input the article of speech for " + LookupWord)
DictionRef.Close
Else

I will manage it later because it's an easy part.

But when this loop picks the second word from MainDocument.doc i.e. 'good', it goes on executing and I have to break its execution. I don't know where's the problem.

For troubleshooting, I removed the below code snippet:

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Color = wdColorAqua
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchWholeWord = False
.MatchCase = False
.MatchWildcards = False
.Wrap = wdFindContinue
.Text = LookupWord
.Execute Replace:=wdReplaceAll
End With

It works fine without this block of code i.e. it picks each word from MainDocument.doc and execute as I desire. But without this block I can't change the font color of the word that's in the loop from MainDocument.doc.

Please help.

fumei
03-07-2011, 10:24 AM
1. you still are not declaring all your variables (smainDoc is not declared, varWordCount is not declared, UserSelection is not declared, DictionRangeForCount is not declared). Why not? Are you using Option Explicit? If not, start doing so.

2. Why are you declaring things as Object? E.g. Dim DictionRange As Object

3. Why are you declaring objects you do not use? E.g. Dim DictionRange As Object

OK. MainDocument is: "A good quick brown."
The contents of MainDocument.doc are: "A good quick brown". So, the "For Each LookupWord In docCurrent.Words" loop should execute for 5 times. Yes, because the period is a word.

But what is in Dictionary.doc?

Your original post was about "to change the font color of a word on the basis of selection the user makes. "

Is this still what you want to do?

Why are you using a word count at all???

You do NOT need to Activate:
Set docCurrent = Documents.Open(sMainDoc)
docCurrent.Activate
The Set instruction opens the document and it IS Active.

Do you realize you Set the object and open the file EVERY TIME in this loop!

For Each LookupWord In docCurrent.Words
sDictionDoc = "c:\Dictionary.doc"
Set DictionRef = Documents.Open(sDictionDoc)


Please restate EXACTLY what you want to do. Please state what is in Dictionary.doc. And please...answer the questions.

I am sure this can be done fairly easily.

fumei
03-07-2011, 10:26 AM
By the way, I am also sure (as I have done it already) that you can do what i think you want to do with half the number of variables you are using. that is, you do NOT need half of them.

If you were usiong Option Explicit you would find out that:

DictionaryRefPara = Selection.Paragraphs(1)
is invalid. beside the fact that DictionaryRefPara is not declared.