Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-12-2013, 02:23 AM
norgro norgro is offline Start of Document Code Problem Windows XP Start of Document Code Problem Office 2007
Novice
Start of Document Code Problem
 
Join Date: Feb 2013
Location: Perh Australia
Posts: 12
norgro is on a distinguished road
Default Start of Document Code Problem


I am writing code to highlight "ance" in a Word document. In Braille, there is a contraction for "ance" (and other combinations of letters). My macro seems to work provinding the document does not start with "ance".

It would be appreciated if someone could tell me how to include the "if the document does not start with "ance" condition.

I am attaching the code.
Code:
Sub HighlightAnceIfNotStartOfWord() 
    Set oRng = ActiveDocument.Range 
     ' Search document for specified text and highlight text.
    With oRng.Find 
        .Text = "ance" 
         '
         ' .Highlight - colour - yellow if previous character if alphabetic character - otherwise turquoise.
        .Wrap = wdFindStop 'stops at the end of the document
        While .Execute 
             ' MsgBox (oRng.Characters.First.Previous)
            If (Asc(oRng.Characters.First.Previous) >= 65 And Asc(oRng.Characters.First.Previous) <= 90) _ 
            Or (Asc(oRng.Characters.First.Previous) >= 97 And Asc(oRng.Characters.First.Previous) <= 122) _ 
            Then 
                oRng.HighlightColorIndex = wdYellow 
            Else: oRng.HighlightColorIndex = wdTurquoise 
            End If 
        Wend 
    End With 
End Sub

Last edited by macropod; 02-12-2013 at 03:39 AM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 02-12-2013, 03:49 AM
macropod's Avatar
macropod macropod is offline Start of Document Code Problem Windows 7 64bit Start of Document Code Problem Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following:
Code:
Sub HighlightAnceIfNotStartOfWord()
' Search document for specified text and highlight text.
With ActiveDocument.Range
    With .Find
        .Text = "ance"
         ' .Highlight - colour - yellow if previous character if alphabetic character - otherwise turquoise.
        .Wrap = wdFindStop 'stops at the end of the document
        .Execute
    End With
    Do While .Find.Found
        If .Characters.First.Previous Like "[A-Za-z]" Then
            .HighlightColorIndex = wdYellow
        Else
            .HighlightColorIndex = wdTurquoise
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
    Loop
End With
End Sub
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-12-2013, 04:03 AM
norgro norgro is offline Start of Document Code Problem Windows XP Start of Document Code Problem Office 2007
Novice
Start of Document Code Problem
 
Join Date: Feb 2013
Location: Perh Australia
Posts: 12
norgro is on a distinguished road
Default

Sincere thanks Paul. I really appreciate your help.

What a great forum this is!

Norman
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Start of Document Code Problem How do you view the source code in a Word 10 web document? provlima Word 2 05-25-2012 01:12 PM
Start of Document Code Problem issues with old documents resuming when i start a new document hughug Word 2 12-12-2011 02:40 PM
Start of Document Code Problem .OnAction only works in document with the code donbexcel Word VBA 1 11-02-2011 05:25 AM
Start of Document Code Problem Remove text in start up document Sheila Word 1 09-30-2010 03:33 PM
how can I add a peace of code to MS document amlife Word VBA 0 03-03-2010 03:35 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:19 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft