|
-
Nov 20th, 2005, 01:23 AM
#1
Thread Starter
Lively Member
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.
Last edited by neo_phyte; Nov 29th, 2005 at 05:39 AM.
-
Nov 20th, 2005, 11:52 PM
#2
Re: please, i need your assistance to complete my project
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 12:48 AM
#3
Thread Starter
Lively Member
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...
-
Nov 21st, 2005, 12:55 AM
#4
Re: please, i need your assistance to complete my project
Where is your with block?
VB Code:
.Replacement.Highlight=wdYellow
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 01:02 AM
#5
Thread Starter
Lively Member
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....
-
Nov 21st, 2005, 01:14 AM
#6
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 01:18 AM
#7
Thread Starter
Lively Member
Re: please, i need your assistance to complete my project
okay, i will try your suggestions... hope it will work...
-
Nov 21st, 2005, 01:24 AM
#8
Thread Starter
Lively Member
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?
-
Nov 21st, 2005, 01:26 AM
#9
Thread Starter
Lively Member
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?
-
Nov 21st, 2005, 01:29 AM
#10
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 01:37 AM
#11
Thread Starter
Lively Member
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...
-
Nov 21st, 2005, 01:40 AM
#12
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 01:43 AM
#13
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 01:44 AM
#14
Thread Starter
Lively Member
Re: please, i need your assistance to complete my project
-
Nov 21st, 2005, 01:46 AM
#15
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 01:54 AM
#16
Thread Starter
Lively Member
Re: please, i need your assistance to complete my project
okay... i hope you can help me Rob....
-
Nov 21st, 2005, 02:08 AM
#17
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 02:17 AM
#18
Thread Starter
Lively Member
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?
-
Nov 21st, 2005, 02:19 AM
#19
Re: please, i need your assistance to complete my project
And you placed it before the .Execute?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 02:23 AM
#20
Thread Starter
Lively Member
Re: please, i need your assistance to complete my project
No, after the .Execute. Inside the Loop....
-
Nov 21st, 2005, 02:34 AM
#21
Re: please, i need your assistance to complete my project
It needs to be placed before the execute.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 02:41 AM
#22
Thread Starter
Lively Member
Re: please, i need your assistance to complete my project
the same result, no bold after all...
-
Nov 21st, 2005, 03:13 AM
#23
Thread Starter
Lively Member
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...
-
Nov 21st, 2005, 03:18 AM
#24
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 21st, 2005, 03:23 AM
#25
Thread Starter
Lively Member
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....
Last edited by neo_phyte; Nov 21st, 2005 at 08:40 AM.
-
Nov 29th, 2005, 05:37 AM
#26
Thread Starter
Lively Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|