Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 04-15-2011, 08:51 AM
SDondeti SDondeti is offline Windows XP Office 2007
Novice
 
Join Date: Apr 2011
Posts: 7
SDondeti is on a distinguished road
Default Word VBA - Find

Hi,

Word VBA code was written in finding the word in a document and trying to get next word or characters after search word.

Example: In a sample.dotm, I am searching a word "Heading" in a forward=false which means up. I am able to find it and i want to know the next word or next characters after "Heading" using word vba.

Can some one help me out.

Here is the code i am trying to use:

Code:
With Selection.Find
  .text = "Heading"
  .Replacement.text = ""
  .Forward = True
  .Wrap = wdFindAsk
  .Format = False
  .MatchCase = True
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
End With
Selection.Find.Execute
how to find the reference where the search word is found?

Thanks in advance.

SDondeti.
Reply With Quote
  #2  
Old 04-15-2011, 03:38 PM
macropod macropod is online now Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 9,363
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
Default

Hi Sdoneti,

Try something like the following, which shows how you can get the next word, or even the whole paragraph:
Code:
Sub Demo()
With Selection.Range
  With .Find
    .ClearFormatting
    .Text = "Heading"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    ' get the next word
    MsgBox .Words.Last.Next.Words.First
    ' get the whole paragraph
    MsgBox .Paragraphs.Last.Range.Text
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 04-19-2011, 08:36 AM
SDondeti SDondeti is offline Windows XP Office 2007
Novice
 
Join Date: Apr 2011
Posts: 7
SDondeti is on a distinguished road
Default

Hi Macropad,

Thank you. I used InsertCation to solve my problem.

CaptionLabels.Add Name:="Picture"
Selection.InsertCaption Label:="Picture", _
Title:=": ", _
Position:=wdCaptionPositionBelow, _
ExcludeLabel:=0

Thanks,
Sudhakar
Reply With Quote
  #4  
Old 08-15-2012, 10:58 AM
telkomek telkomek is offline Windows XP Office 2003
Novice
 
Join Date: Aug 2012
Posts: 1
telkomek is on a distinguished road
Default when i'm test is looping forever

Quote:
Originally Posted by macropod View Post
Hi Sdoneti,

Try something like the following, which shows how you can get the next word, or even the whole paragraph:
Code:
Sub Demo()
With Selection.Range
  With .Find
    .ClearFormatting
    .Text = "Heading"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    ' get the next word
    MsgBox .Words.Last.Next.Words.First
    ' get the whole paragraph
    MsgBox .Paragraphs.Last.Range.Text
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
End Sub
when i'm test is looping forever.
are only happen to me?
Reply With Quote
  #5  
Old 08-15-2012, 05:03 PM
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 9,363
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
Default

Works fine for me. The code makes one pass through the selected range, then stops.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lost Word Document and cant find it!!! I saved it but it ISNT there!! APAV Word 8 08-30-2012 09:05 AM
How to find out the version of the word files? mariaprabudass Word 1 06-21-2010 08:44 AM
can Find/Replace NOT find something? Bobosmite Word 6 05-27-2010 08:09 PM
How to find the version of word file? mariaprabudass Word 7 04-29-2010 03:44 AM
FInd recurring words in Word 2003 NJ007 Word 4 01-25-2010 02:11 PM


All times are GMT -7. The time now is 06:07 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft