-
Counter Problem....
good day guys, i need your assistance on this matter. i am planning to add optionbutton or checkbox to my form. This optionbutton or checkbox is used when i am going to Highlight the text that i am inputted in Text1.Text. When optionbutton becomes true or checkbox becomes true, the text that i am inputted in Text1.text will automatically highlight in the microsoft word application.
here is my existing code, please modify my code to complete my project.
VB Code:
Private Sub Command1_Click()
On Error Goto ErrTrap
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String
count = 0
wdText = Text1.Text
Set objWdDoc = Application.ActiveDocument
Set objWdRange = objWdDoc.Content
With objWdRange.Find
Do While .Execute(FindText:=wdText, Format:=False, _
MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
count = count + 1
Loop
End With
MsgBox "There are " & count & " words found in this document", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
Exit Sub
ErrTrap:
If Err.Number = 429 Then
Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
Else
Msgbox "An error of " & Err.Number & " " has occured. This means " & Err.Description
End If
End Sub
i am a starter of any programming language... but i try my best to learn more... and this is my new start, to learn more visual basic... my current job is word processor, and i made my macro just to automate my work.... thanks for the help guys... hope you can help me for this.
-
Re: please, i need your assistance to complete my project
-
Re: please, i need your assistance to complete my project
"invalid property" that is the error..... could you modify my first posted code... hope you can help me.. thanks...
-
Re: please, i need your assistance to complete my project
Where is your with block?
VB Code:
.Replacement.Highlight=wdYellow
-
Re: please, i need your assistance to complete my project
here is my modified code:
VB Code:
Private Sub Command1_Click()
On Error Goto ErrTrap
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String
count = 0
wdText = Text1.Text
Set objWdDoc = Application.ActiveDocument
Set objWdRange = objWdDoc.Content
With objWdRange.Find
Do While .Execute(FindText:=wdText, Format:=False, _
MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
count = count + 1
Loop
' this is where i put my condition, is this correct?
If optHighlight.Value=True Then
.Replacement.Highlight=wdYellow
Else
.Replacement.Highlight=wdBlack
End if
End With
MsgBox "There are " & count & " words found in this document", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
Exit Sub
ErrTrap:
If Err.Number = 429 Then
Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
Else
Msgbox "An error of " & Err.Number & " " has occured. This means " & Err.Description
End If
End Sub
thanks for the help....
-
Re: please, i need your assistance to complete my project
Looks like it may fit in in your .Execute loop since that is the return object your working off of.
VB Code:
Private Sub Command1_Click()
On Error Goto ErrTrap
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String
count = 0
wdText = Text1.Text
Set objWdDoc = Application.ActiveDocument
Set objWdRange = objWdDoc.Content
With objWdRange.Find
Do While .Execute(FindText:=wdText, Format:=False, _
MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
count = count + 1
' this is where i put my condition, is this correct?
If optHighlight.Value=True Then
.Replacement.Highlight=wdYellow
Else
.Replacement.Highlight=wdBlack
End if
Loop
End With
MsgBox "There are " & count & " words found in this document", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
Exit Sub
ErrTrap:
If Err.Number = 429 Then
Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
Else
Msgbox "An error of " & Err.Number & " " has occured. This means " & Err.Description
End If
End Sub
-
Re: please, i need your assistance to complete my project
okay, i will try your suggestions... hope it will work...
-
Re: please, i need your assistance to complete my project
i have tried it now, but it does not highlight the word.... is there other way Rob?
-
Re: please, i need your assistance to complete my project
there is no error, but it does not highlight the word.... could you suggest more sir Rob?
-
Re: please, i need your assistance to complete my project
I think I see it now. your not Replacing any text so there is no Replacement object or property.
VB Code:
Private Sub Command1_Click()
On Error Goto ErrTrap
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String
count = 0
wdText = Text1.Text
Set objWdDoc = Application.ActiveDocument
Set objWdRange = objWdDoc.Content
With objWdRange.Find
Do While .Execute(FindText:=wdText, Format:=False, _
MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
count = count + 1
Loop
' this is where i put my condition, is this correct?
If optHighlight.Value=True Then
.Highlight=wdYellow
Else
.Highlight=wdBlack
End if
End With
MsgBox "There are " & count & " words found in this document", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
Exit Sub
ErrTrap:
If Err.Number = 429 Then
Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
Else
Msgbox "An error of " & Err.Number & " " has occured. This means " & Err.Description
End If
End Sub
-
Re: please, i need your assistance to complete my project
okay, i try the above code, but it does not highlight the word/s i am inputted in Text1.Text, in the microsoft word application... it seems that the code does not find word or words to be highlighted...
-
Re: please, i need your assistance to complete my project
Ok, it should go before the .Execute loop and the syntax is .Highlight = True It doesn not seem that you can change the color.
-
Re: please, i need your assistance to complete my project
I think I found how to set the color of the found highlighted word.
VB Code:
.Font.Color = wdColorYellow
Again before the .Highlight line of code.
-
Re: please, i need your assistance to complete my project
-
Re: please, i need your assistance to complete my project
Darn, its the forecolor and not the actual highlight color. Guess I should have tried first. :D
-
Re: please, i need your assistance to complete my project
okay... i hope you can help me Rob....
-
Re: please, i need your assistance to complete my project
looks like you can only turn the highlighting on or off, but you could format the .Font to bold or underlined or both or some other style.
-
Re: please, i need your assistance to complete my project
i try:
but it does not give the result of the word that inputted in Text1.Text to be bold... why?
-
Re: please, i need your assistance to complete my project
And you placed it before the .Execute?
-
Re: please, i need your assistance to complete my project
No, after the .Execute. Inside the Loop....
-
Re: please, i need your assistance to complete my project
It needs to be placed before the execute.
-
Re: please, i need your assistance to complete my project
the same result, no bold after all...
-
Re: please, i need your assistance to complete my project
VB Code:
Options.DefaultHighlightColorIndex = wdYellow
how about this code... i found it upon browsing of some posted problems here in VBA... can we used it... I think it can used for highlight coloring... I guess so.... please help Mr. Rob...
-
Re: please, i need your assistance to complete my project
Use this instead. It wil color the highlighting for just a defined Range instead of the entire word application.
VB Code:
objWdDoc.Content.Range.HighlightColorIndex = wdYellow
-
closer to be resolved....
Mr. Rob good day to you...
I made my code basing from your advise, but seems that whole document will be highlighted instead of selection based from the input.
VB Code:
Private Sub Command1_Click()
On Error Goto ErrTrap
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String
count = 0
wdText = Text1.Text
Set objWdDoc = Application.ActiveDocument
Set objWdRange = objWdDoc.Content
' set the color
objWdDoc.Range.HighlightColorIndex = wdYellow
With objWdRange.Find
'condition to set optionbutton to true
If optHighlight.Value = True Then
.Text = Text1.Text
.Replacement.Text = ""
.Execute Replace:= wdReplaceAll
End If
Do While .Execute(FindText:=wdText, Format:=False, _
MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
count = count + 1
Loop
End With
MsgBox "There are " & count & " words found in this document", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
Exit Sub
ErrTrap:
If Err.Number = 429 Then
Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
Else
Msgbox "An error of " & Err.Number & " " has occured. This means " & Err.Description
End If
End Sub
please help me....
-
Counter problem...
This will highlight the text that i am inputted in Text1.Text, but i got the problem now with the counter, since it counts one even if there are many texts found in the document based from the inputted text in Text1.Text. Please help...
VB Code:
Private Sub Command1_Click()
On Error Goto ErrTrap
Dim objWdRange As Word.Range
Dim objWdDoc As Word.Document
Dim count As Integer
Dim wdText As String
count = 0
wdText = Text1.Text
Set objWdDoc = Application.ActiveDocument
Set objWdRange = objWdDoc.Content
With objWdRange.Find
Do While .Execute(FindText:=wdText, Format:=False, _
MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
count = count + 1
If Checkbox1.Value = True Then
objWdDoc.Range.HighlightColorIndex = wdYellow
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
End If
Exit Do
Loop
End With
MsgBox "There are " & count & " words found in this document", vbInformation
Set objWdRange = Nothing
Set objWdDoc = Nothing
Exit Sub
ErrTrap:
If Err.Number = 429 Then
Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
Else
Msgbox "An error of " & Err.Number & " " has occured. This means " & Err.Description
End If
End Sub