Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Nov 2008
    Location
    Sydney, New South Wales, Australia
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    vba New Objects 2007/2010

    Does anyone know why this only runs in ThisDocument class and not a module?

    Has anyone used these new objects? Many thanks.


    Set fnd = content.Find

    Option Explicit
    ' Word 2010
    ' Demonstrate some of the features new to the Find object starting in Word 2007:
    ' IgnorePunct
    ' IgnoreSpace
    ' MatchPrefix
    ' MatchSuffix
    ' ClearHitHighlight
    ' HitHighlight
    ' In a new document, type the following text and then press Enter:
    '
    ' =rand(5, 5)
    '
    ' This action inserts 5 paragraphs with 5 sentences each into the
    ' current document. Then, in the VBA editor, in the ThisDocument class,
    ' copy in this code, and place the cursor within
    ' this procedure and press F8 to single step through the code. Arrange
    ' the VBA and Word windows side by side on screen so you can view the
    ' behavior as you step through the code.
    Sub DemoFind()
    ' Set up a search, in the random text, for
    ' "tab Most"
    Dim fnd As Find


    Set fnd = content.Find

    fnd.text = "tab Most"
    ' Ignore punctuation and white space. In the document,
    ' the text appears as "tab. Most". This will still find
    ' a match.
    fnd.IgnorePunct = True
    fnd.IgnoreSpace = True

    ' Highlight the found text.
    fnd.HitHighlight fnd.text, vbYellow, vbRed

    ' Now clear the highlighting. This is only meaningful
    ' if you are single-stepping through the code.
    fnd.ClearHitHighlight

    ' Match the text "th" only when it appears at the beginning
    ' of a word:
    fnd.MatchPrefix = True
    fnd.text = "th"
    fnd.HitHighlight fnd.text, vbYellow, vbRed
    ' Now clear the highlighting. This is only meaningful
    ' if you are single-stepping through the code.
    fnd.ClearHitHighlight
    ' Match the text "th" only when it appears at the end
    ' of a word:
    fnd.MatchPrefix = False
    fnd.MatchSuffix = True
    fnd.text = "th"
    fnd.HitHighlight fnd.text, vbYellow, vbRed
    ' Now clear the highlighting. This is only meaningful
    ' if you are single-stepping through the code.
    fnd.ClearHitHighlight
    End Sub
    Attached Images Attached Images
    Last edited by jec1; 2012-04-24 at 17:07.

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,514
    Thanks
    3
    Thanked 143 Times in 136 Posts
    content doesn't have a context in a module. Try the following

    Set fnd = ActiveDocument.Content.Find
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    Lounger
    Join Date
    Nov 2008
    Location
    Sydney, New South Wales, Australia
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks Andrew

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •