Results 1 to 26 of 26

Thread: Counter Problem....

  1. #1

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Wink 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:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5. Dim count As Integer
    6. Dim wdText As String
    7.  
    8. count = 0
    9. wdText = Text1.Text
    10.  
    11. Set objWdDoc = Application.ActiveDocument
    12. Set objWdRange = objWdDoc.Content
    13.  
    14. With objWdRange.Find
    15.      
    16.  Do While .Execute(FindText:=wdText, Format:=False, _
    17.  MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
    18.     count = count + 1
    19.  
    20.  Loop
    21.  
    22. End With
    23. MsgBox "There are " & count & " words found in this document", vbInformation
    24. Set objWdRange = Nothing
    25. Set objWdDoc = Nothing
    26. Exit Sub
    27. ErrTrap:
    28. If Err.Number = 429 Then
    29.    Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
    30. Else
    31.    Msgbox "An error of " & Err.Number & " " has occured.  This means " & Err.Description
    32. End If
    33. 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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: please, i need your assistance to complete my project

    Whats the error?
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    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...

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: please, i need your assistance to complete my project

    Where is your with block?
    VB Code:
    1. .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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: please, i need your assistance to complete my project

    here is my modified code:

    VB Code:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5. Dim count As Integer
    6. Dim wdText As String
    7.  
    8. count = 0
    9. wdText = Text1.Text
    10.  
    11. Set objWdDoc = Application.ActiveDocument
    12. Set objWdRange = objWdDoc.Content
    13.  
    14. With objWdRange.Find
    15.      
    16.  Do While .Execute(FindText:=wdText, Format:=False, _
    17.  MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
    18.     count = count + 1
    19.  
    20.  Loop
    21.  
    22. ' this is where i put my condition, is this correct?
    23. If optHighlight.Value=True Then
    24.  .Replacement.Highlight=wdYellow
    25. Else
    26.  .Replacement.Highlight=wdBlack
    27. End if
    28.  
    29. End With
    30. MsgBox "There are " & count & " words found in this document", vbInformation
    31. Set objWdRange = Nothing
    32. Set objWdDoc = Nothing
    33. Exit Sub
    34. ErrTrap:
    35. If Err.Number = 429 Then
    36.    Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
    37. Else
    38.    Msgbox "An error of " & Err.Number & " " has occured.  This means " & Err.Description
    39. End If
    40. End Sub


    thanks for the help....

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5. Dim count As Integer
    6. Dim wdText As String
    7.  
    8. count = 0
    9. wdText = Text1.Text
    10.  
    11. Set objWdDoc = Application.ActiveDocument
    12. Set objWdRange = objWdDoc.Content
    13.  
    14. With objWdRange.Find
    15.      
    16.  Do While .Execute(FindText:=wdText, Format:=False, _
    17.  MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
    18.     count = count + 1
    19.  
    20.     ' this is where i put my condition, is this correct?
    21.     If optHighlight.Value=True Then
    22.         .Replacement.Highlight=wdYellow
    23.     Else
    24.         .Replacement.Highlight=wdBlack
    25.     End if
    26.  
    27.  Loop
    28.  
    29.  
    30.  
    31. End With
    32. MsgBox "There are " & count & " words found in this document", vbInformation
    33. Set objWdRange = Nothing
    34. Set objWdDoc = Nothing
    35. Exit Sub
    36. ErrTrap:
    37. If Err.Number = 429 Then
    38.    Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
    39. Else
    40.    Msgbox "An error of " & Err.Number & " " has occured.  This means " & Err.Description
    41. End If
    42. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: please, i need your assistance to complete my project

    okay, i will try your suggestions... hope it will work...

  8. #8

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    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?

  9. #9

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    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?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5. Dim count As Integer
    6. Dim wdText As String
    7.  
    8. count = 0
    9. wdText = Text1.Text
    10.  
    11. Set objWdDoc = Application.ActiveDocument
    12. Set objWdRange = objWdDoc.Content
    13.  
    14. With objWdRange.Find
    15.      
    16.  Do While .Execute(FindText:=wdText, Format:=False, _
    17.  MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
    18.     count = count + 1
    19.  
    20.  Loop
    21.  
    22. ' this is where i put my condition, is this correct?
    23. If optHighlight.Value=True Then
    24.  .Highlight=wdYellow
    25. Else
    26.  .Highlight=wdBlack
    27. End if
    28.  
    29. End With
    30. MsgBox "There are " & count & " words found in this document", vbInformation
    31. Set objWdRange = Nothing
    32. Set objWdDoc = Nothing
    33. Exit Sub
    34. ErrTrap:
    35. If Err.Number = 429 Then
    36.    Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
    37. Else
    38.    Msgbox "An error of " & Err.Number & " " has occured.  This means " & Err.Description
    39. End If
    40. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  11. #11

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    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...

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. .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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  14. #14

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: please, i need your assistance to complete my project

    ok, i will try...

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  16. #16

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: please, i need your assistance to complete my project

    okay... i hope you can help me Rob....

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  18. #18

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: please, i need your assistance to complete my project

    i try:

    VB Code:
    1. .Font.Bold = True

    but it does not give the result of the word that inputted in Text1.Text to be bold... why?

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  20. #20

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: please, i need your assistance to complete my project

    No, after the .Execute. Inside the Loop....

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  22. #22

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: please, i need your assistance to complete my project

    the same result, no bold after all...

  23. #23

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: please, i need your assistance to complete my project

    VB Code:
    1. 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...

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  25. #25

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    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:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5. Dim count As Integer
    6. Dim wdText As String
    7.  
    8. count = 0
    9. wdText = Text1.Text
    10.  
    11. Set objWdDoc = Application.ActiveDocument
    12. Set objWdRange = objWdDoc.Content
    13.  
    14. ' set the color
    15.  
    16. objWdDoc.Range.HighlightColorIndex = wdYellow
    17.  
    18. With objWdRange.Find
    19.  
    20. 'condition to set optionbutton to true
    21.  
    22. If optHighlight.Value = True Then
    23.   .Text = Text1.Text
    24.   .Replacement.Text = ""
    25.   .Execute Replace:= wdReplaceAll
    26. End If
    27.      
    28.  Do While .Execute(FindText:=wdText, Format:=False, _
    29.  MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
    30.     count = count + 1
    31.  
    32.  Loop
    33.  
    34. End With
    35. MsgBox "There are " & count & " words found in this document", vbInformation
    36. Set objWdRange = Nothing
    37. Set objWdDoc = Nothing
    38. Exit Sub
    39. ErrTrap:
    40. If Err.Number = 429 Then
    41.    Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
    42. Else
    43.    Msgbox "An error of " & Err.Number & " " has occured.  This means " & Err.Description
    44. End If
    45. End Sub

    please help me....
    Last edited by neo_phyte; Nov 21st, 2005 at 08:40 AM.

  26. #26

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Arrow 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:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. Dim objWdRange As Word.Range
    4. Dim objWdDoc As Word.Document
    5. Dim count As Integer
    6. Dim wdText As String
    7.  
    8. count = 0
    9. wdText = Text1.Text
    10.  
    11. Set objWdDoc = Application.ActiveDocument
    12. Set objWdRange = objWdDoc.Content
    13.  
    14. With objWdRange.Find
    15.  
    16.  Do While .Execute(FindText:=wdText, Format:=False, _
    17.  MatchCase:=True, MatchWholeWord:=False, MatchAllWordForms:=False) = True
    18.     count = count + 1
    19.  
    20. If Checkbox1.Value = True Then
    21.     objWdDoc.Range.HighlightColorIndex = wdYellow
    22.     .ClearFormatting
    23.     .Replacement.ClearFormatting
    24.     .Replacement.Highlight = True
    25.     .Execute Replace:=wdReplaceAll
    26. End If
    27. Exit Do
    28. Loop
    29.  
    30. End With
    31. MsgBox "There are " & count & " words found in this document", vbInformation
    32. Set objWdRange = Nothing
    33. Set objWdDoc = Nothing
    34. Exit Sub
    35. ErrTrap:
    36. If Err.Number = 429 Then
    37.    Msgbox "You cannot start the counting operation, please open a document", vbOKOnly + vbExclamation, "No document opened"
    38. Else
    39.    Msgbox "An error of " & Err.Number & " " has occured.  This means " & Err.Description
    40. End If
    41. 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
  •  



Click Here to Expand Forum to Full Width