Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 21st, 2003, 04:48 AM
Defens0rFidei's Avatar
Defens0rFidei Defens0rFidei is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 16 Defens0rFidei User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
VBA Newbie - Help Please

Hi-

I would like to write a Word Macro that searches a document for a set of pre-defined words and highlights them.

Can someone help me out with this problem?

I am completely new to VBA, but I can understand the code if someone can show me an example of a solution.

Much thanks!

Reply With Quote
  #2  
Old November 21st, 2003, 07:20 AM
Defens0rFidei's Avatar
Defens0rFidei Defens0rFidei is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 16 Defens0rFidei User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have this code working which highlights the whole line, but I just want to highlight the word.

How do I modify it to just select the dirty word?

Code:
Sub Highlighter()
    
    '
    ' Highlighter Macro
    ' Macro created 11/21/2003
    '
    Dim objDoc As Word.Document
    Dim lNumberOfLines As Long
    Dim lCount As Long
    Dim sDirtyWords As Variant
    Dim sWord As Variant
    Dim HighlighColor As Variant
    
    ' Get the ActiveDocument
    Set objDoc = ActiveDocument
    
    ' Set the highlight color - could be wdOrange, wdBlue...
    HighlighColor = wdYellow
    
    ' Load words into array
    sDirtyWords = Array("and", "for", "the", "which")
    
    ' Get number of lines in document
    lNumberOfLines = objDoc.ComputeStatistics(wdStatisticLines)
    
    ' Remove all highlighting in document
    Selection.WholeStory
    Selection.Range.HighlightColorIndex = wdNoHighlight
    
    ' Loop through all the lines in the document
    For lCount = 1 To lNumberOfLines
    
        ' Go to line
        SelectLine lCount
        
        ' Loop through all words
        For Each sWord In sDirtyWords
            If InStr(1, Selection.Text, sWord) > 0 Then
                Selection.Range.HighlightColorIndex = wdYellow
            End If
        Next
    Next
    
    ' Remove handle to document
    Set objDoc = Nothing
End Sub

Sub SelectLine(lLine As Long)
    Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=lLine, Name:=""
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    
    ' Select line
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
End Sub


Reply With Quote
  #3  
Old November 23rd, 2003, 01:47 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > VBA Newbie - Help Please


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT