Sign in to follow this  
Followers 0
PoorLuzer

Operating on Highlighted Text

Was this useful  

  1. 1. Was this snippet useful?

    • Hey! It helped me.
      0
    • This is crap!
      0
    • I used some code from this snippet
      0
    • I was not looking for this but it helped me in doing something related.
      0

Please sign in or register to vote in this poll.

1 post in this topic

People,

I wanted to operate on highlighted text in a WORD (Office 2003) document.

The operation needs to be conditional based on the type of highlighting present.

For example, I want something like:

1. If highlighting in yellow is present, delete text with that highlighting

2. If highlighting in red is present, move such text to separate file OR remove formatting for now OR make font bold etc

Overview of the logic will look like following:

If Activedocument.Font.Shading.BackgroundPatternColor == wdColorYellow

Then

' This is commented text - delete this portion

...

Elseif Activedocument.Font.Shading.BackgroundPatternColor == wdColorRed

Then

' this needs to attract attention - move such text to seprate file OR remove formatting for now OR make font bold

Activedocument.Font.Shading.BackgroundPatternColor = wdColorAutomatic

....

Note that the same document might have multiple highlighting in multiple colors.

This solution is completely due to Ed Weber where he gave the complete code:

http://www.productivitytalk.com/forums/ind...st&p=652416

I have just modified his code in AutoIt v3 because that's what I am going to use.. adding and maintaining macros is too much of a chore for me.

Run this code and bring your WORD doc into focus after dismissing the message box.

#include <Word.au3>

#cs
    Copyright 2008 PoorLuzer@gmail.com
#ce

const $wdFindStop = 0

const $wdAuto = 0

const $wdPink = 5
const $wdRed = 6
const $wdYellow = 7
const $wdGreen = 11
const $wdDarkRed = 13
const $wdDarkYellow = 14
const $wdGray50 = 15
const $wdGray25 = 16 

    MsgBox(0, "ALERT!", "After clicking OK on this message box, immediately bring the WORD window into focus!")

    Sleep(5000)

    $hWndWORD = WinGetHandle("[ACTIVE]", "")

    If 0 == $hWndWORD Then
    
        MsgBox(0, $msgBoxTitle, "WORD Failed!", 5)
        MsgBox(0, $msgBoxTitle, @ERROR)
        Exit
    EndIf
    
    $oWordApp = _WordAttach($hWndWORD, "HWND")
    
    If Not @error Then
    
        $oDoc = _WordDocGetCollection ($oWordApp, 0)
        
        If Not @error Then
        
          MsgBox(64, "Document FileName", $oDoc.FullName)
        Else
            MsgBox(64, "Failed", $oDoc.FullName)
            Exit
        EndIf
    EndIf

    $Rng = $oDoc.Range.Duplicate
    
    With $Rng.Find
    
        .ClearFormatting()
        .Format = True
        .Highlight = True
        .Wrap = $wdFindStop
        .Text = ""
        
        While $Rng.Find.Execute
        
            If $Rng.Find.Found Then
            
                        If $wdRed == $Rng.HighlightColorIndex Then
                        
                    $Rng.HighlightColorIndex = $wdAuto
                    $Rng.Bold = True
                EndIf
            EndIf
            
            $Rng.Start = $Rng.End + 1
            $Rng.End = $oDoc.Range.End
        WEnd
    EndWith

    MsgBox(64, "Done", $oDoc.FullName)

Highlight.au3

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0