Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-04-2014, 12:33 AM
mkhuebner mkhuebner is offline How to find and select text in a document? Windows 7 64bit How to find and select text in a document? Office 2010 64bit
Novice
How to find and select text in a document?
 
Join Date: Feb 2014
Posts: 7
mkhuebner is on a distinguished road
Default How to find and select text in a document?


I'm trying to create a Word 2010 macro that finds, selects, and reformats a single word in my document starting from the current cursor position. During recording of the macro, I've tried to use Find in the menu (Ctrl-F) but nothing is recorded in my macro for the find operation even though the cursor moves and highlights the found word. I've tried using the code below but that is also not moving the current cursor position to this word. Can somebody tell me how to record a Find text operation in a Word macro or else tell me what the correct VBA code is to do this? Thanks in advance!

Selection.Find.Text = "Synposis"
Selection.Find.Execute
Selection.Style = ActiveDocument.Styles("Body Text")
Selection.Font.Bold = wdToggle
Reply With Quote
  #2  
Old 02-04-2014, 06:53 PM
macropod's Avatar
macropod macropod is offline How to find and select text in a document? Windows 7 32bit How to find and select text in a document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Your code suggests you want to toggle the bold attribute for 'Synopsis' in the 'Body Text' Style, not simply make it all bold or all not bold. In that case, you could use a macro like:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With Selection
  Set Rng = .Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Synopsis"
    .Replacement.Text = "^&"
    .Style = "Body Text"
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .Font.Bold = wdToggle
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Rng.Select
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
If, however, you wish to make all such text bold, the macro could be made much more efficient and simplified to:
Code:
Sub Demo()
Application.ScreenUpdating = False
With Selection
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Synopsis"
    .Replacement.Text = "^&"
    .Style = "Body Text"
    .Replacement.Font.Bold = True
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
If you want to make the font not bold instead, change:
.Replacement.Font.Bold = True
to:
.Replacement.Font.Bold = False
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-04-2014, 07:02 PM
mkhuebner mkhuebner is offline How to find and select text in a document? Windows 7 64bit How to find and select text in a document? Office 2010 64bit
Novice
How to find and select text in a document?
 
Join Date: Feb 2014
Posts: 7
mkhuebner is on a distinguished road
Default

Thanks. Does the code have to be this complicated? All I want to do is find the next word "Synopsis" after the current cursor position and change it's style to Body Text and make it bold face.
Reply With Quote
  #4  
Old 02-04-2014, 07:26 PM
macropod's Avatar
macropod macropod is offline How to find and select text in a document? Windows 7 32bit How to find and select text in a document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

No, it doesn't 'have' to be so complicated, but leaving out some parameters might lead to unexpected results ...

Re:
Quote:
change it's style to Body Text and make it bold face
That's not what the code I posted does. Based on what you had posted, the code I posted looks for 'Synopsis' that is already in the 'Body Text' Style. Furthermore, you can't re-format only a single word of many in a paragraph with the 'Body Text' Style, as that Style is a paragraph Style. Of course, that doesn't matter if 'Synopsis' is the only word in the paragraphs concerned.

As for changing the font to bold, if 'Synopsis' is the only word in the paragraphs concerned, you'd do better to change the 'Body Text' Style's font to bold than to override the Style format. Similarly, if 'Synopsis' is only one word of many in a paragraph, you'd do better to apply the 'Strong' character Style to 'Synopsis' than to override the Style format. For example:
Code:
Sub Demo()
Application.ScreenUpdating = False
With Selection
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Synopsis"
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Replacement.Style = "Body Text"
    .Execute Replace:=wdReplaceAll
    .Replacement.Style = "Strong"
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 02-04-2014, 07:29 PM
mkhuebner mkhuebner is offline How to find and select text in a document? Windows 7 64bit How to find and select text in a document? Office 2010 64bit
Novice
How to find and select text in a document?
 
Join Date: Feb 2014
Posts: 7
mkhuebner is on a distinguished road
Default

Is there any way to generate this code using Word's macro recording mode? When I tried to do a Find "Synopsis" operation, no VBA code was generated.
Reply With Quote
  #6  
Old 02-04-2014, 07:34 PM
macropod's Avatar
macropod macropod is offline How to find and select text in a document? Windows 7 32bit How to find and select text in a document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Basic code produced using the macro recorder:
Code:
Sub Macro1()
'
' Macro1 Macro
'
'
  Selection.Find.ClearFormatting
  Selection.Find.Replacement.ClearFormatting
  Selection.Find.Replacement.Style = ActiveDocument.Styles("Body Text")
  With Selection.Find
    .Text = "Synopsis"
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = True
    .MatchWholeWord = True
    .MatchByte = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Selection.Find.Execute Replace:=wdReplaceAll
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 02-04-2014, 07:47 PM
mkhuebner mkhuebner is offline How to find and select text in a document? Windows 7 64bit How to find and select text in a document? Office 2010 64bit
Novice
How to find and select text in a document?
 
Join Date: Feb 2014
Posts: 7
mkhuebner is on a distinguished road
Default

How did you get it to record a Find text string operation?
Reply With Quote
  #8  
Old 02-04-2014, 07:50 PM
macropod's Avatar
macropod macropod is offline How to find and select text in a document? Windows 7 32bit How to find and select text in a document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

By inputting the parameters you see recorded into the Find/Replace dialogue...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 02-04-2014, 08:04 PM
mkhuebner mkhuebner is offline How to find and select text in a document? Windows 7 64bit How to find and select text in a document? Office 2010 64bit
Novice
How to find and select text in a document?
 
Join Date: Feb 2014
Posts: 7
mkhuebner is on a distinguished road
Default

It's recording ok now. I had to use keyboard commands to select the Find operation instead of the mouse.

Thanks for all your help!
Reply With Quote
Reply

Tags
macro find text

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find and select text in a document? Select a area in a Word Document Peter Stahl Word VBA 2 08-09-2013 03:18 AM
How to find and select text in a document? Unable to Select Multiple folders in Outlook 2007 Advance Find gregory Outlook 2 04-28-2012 10:53 PM
How to find and select text in a document? Select statement to a new document ishaw Word 1 10-17-2011 02:23 AM
Unable to select anything in an MS Word Document PaulT Word 0 08-08-2011 07:45 PM
How to find and select text in a document? Select printer to document JosL Office 3 03-07-2009 12:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:53 PM.


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