Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-07-2017, 08:23 AM
welcometocandyland welcometocandyland is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Feb 2017
Posts: 2
welcometocandyland is on a distinguished road
Default VBA Code to search for field codes with certain text before the Field code and to change style

VBA Code to search for field codes with certain text before the Field code and to change style...



In text with field code I have " This Section { REF Sect_Stuff } applies to blah blah."

I wondered what code would be needed to find all the field codes in the doc with the word "Section" before it? "Section" not being part of the field code.

And how to change the style of a field code?

Thank You
Reply With Quote
  #2  
Old 02-07-2017, 11:19 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,552
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

You could toggle Word's field code display 'on' (e.g. via Alt-F9) then use:
Find = Section ^d
However, any formatting changes would be applied to the entire 'found' expression, not just to the field code. Furthermore, the correct way to reformat the field in such cases is via the addition of a Charformat switch and applying the formatting to the first letter in the field code, thus:
{ REF Sect_Stuff \* Charformat}
(to apply bold, red, formatting - Character Styles can also be used). If you have a great many of these to do, a macro might be called for.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 02-08-2017, 07:39 AM
welcometocandyland welcometocandyland is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Feb 2017
Posts: 2
welcometocandyland is on a distinguished road
Default

Thank you for your help. I do need a macro. I'm sorry I wasn't clear, that is the code I am looking for. I want to search the entire document for any occurrences of the word section followed by a field code. I then want to change the style of the field code only by incorporating the style change to the first character (R) and add the *\Charformat to the end of the field. Then proceed to the next occurrence. I don't want it to change the style of the word section or find the word section that isn't followed by a field code.


Can you please help?
Reply With Quote
  #4  
Old 02-08-2017, 04:29 PM
gmaxey gmaxey is offline Windows 7 32bit Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 746
gmaxey will become famous soon enough
Default

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFld As Word.Field
Dim oRng As Range
For Each oFld In ActiveDocument.Fields
  Set oRng = oFld.Code.Words.First.Previous.Previous
  oRng.MoveStart wdWord, -1
  If oRng.Text = "Section" Then
    oFld.Code.Text = oFld.Code.Text & " \* Charformat"
    oFld.Code.Characters(2).Font.Bold = True
    oFld.Update
  End If
Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 02-08-2017, 06:53 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,552
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Greg's posted one way, here's another:
Code:
Sub Demo()
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Section ^d"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    With .Fields(1)
      With .Code
        If InStr(.Text, "Mergeformat") > 0 Then
          .Text = Replace(.Text, "Mergeformat", "Charformat")
        ElseIf InStr(.Text, "Charformat") = 0 Then
          .InsertAfter " \* Charformat"
        End If
        .Words.First.Next.Style = "Emphasis"
      End With
      .Update
    End With
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
End Sub
My code's more robust...
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Code to Insert Legacy Text Field PSSMargaret Word VBA 17 06-15-2016 04:22 AM
Variable text field code based on occurrences on each page Cosmo Word 2 12-29-2015 11:54 AM
Macro to select an { includepicture } field code and format the picture behind text and 100% scale sanpedro Word VBA 3 03-30-2015 10:50 PM
need an arbitrary field code ajetrumpet Word 1 07-15-2013 04:58 PM
field code question bordercollie10 Word 3 10-15-2009 07:55 AM


All times are GMT -7. The time now is 06:27 PM.


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