Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-08-2021, 08:14 AM
scienceguy scienceguy is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2016
Advanced Beginner
How to determine if a Section number has a field code
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default How to determine if a Section number has a field code

Hello,



I'm trying to write a simple macro that determines if a Section number within the document's text has a field code. In other words, did the author use Microsoft Word's "cross-reference" feature. For example, "Please see Section 1 for more details." Does the "1" have a field code? If it does then turn the font blue. If it doesn't then turn the font red and add a comment.

I tried to mimic this application based off of a similar question, where I was looking for "Table n". However, it seems there is something unique about "Section n" vs "Table n". That solution is located here.

I believe the format is the word "Section" + non-breaking space + number. Therefore, my search string I have tried was:

aRng.Find.Text = "Section?[0-9.]{1,}"

However, this isn't working. It finds the "Section n" if Word's cross-reference isn't used, but it misses the properly formatted one that uses Word's cross reference feature. My full code is below. I am attaching a simple Word file to illustrate the issue. The first paragraph has "Section 2" and was properly formatted with Word's cross-reference feature. Therefore, the font for "2" should be turned blue. The third paragraph has "Section 1.1", which was not properly formatted. Therefore, its font should be turned red and a comment added.

Thank for you in advance for any assistance!

Roy


Code:
Sub myMacro()

    Dim aRng As Range
    
    Set aRng = ActiveDocument.Range
    With aRng.Find
        .ClearFormatting
        .Text = "Section^s[0-9.]{1,}"
        .MatchWildcards = True
        Do While .Execute  ' Loop until Word can no longer find the search string
            aRng.MoveStart Unit:=1, Count:=8                 'wdCharacter = 1
            aRng.MoveEnd Unit:=1, Count:=1
            If aRng.Fields.Count > 0 Then 'there is a field
                
                If InStr(1, aRng.Fields(1).Code.Text, "_Ref") > 0 And aRng.Font.Color <> vbBlue Then
                    aRng.Font.Color = vbBlue
                End If
                    
            Else
                aRng.Font.Color = vbRed
                ActiveDocument.Comments.Add Range:=aRng, Text:="Missing cross-reference field code"
            End If ''
            aRng.Collapse Direction:=0      'wdCollapseEnd
        Loop
        
    End With

End Sub
Attached Files
File Type: docm example code.docm (43.3 KB, 10 views)
Reply With Quote
  #2  
Old 11-08-2021, 02:06 PM
Guessed's Avatar
Guessed Guessed is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try this version. It is more correct to search for a Ref field type rather than whether the field text includes "_Ref" somewhere in it.
Code:
Sub myMacro()
  Dim aRng As Range, rngNumber As Range
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .ClearFormatting
    .Text = "Section?[0-9.]{1,}"
    .MatchWildcards = True
    Do While .Execute  ' Loop until Word can no longer find the search string
      Set rngNumber = aRng.Duplicate
      rngNumber.Start = aRng.Words(2).Start
      If rngNumber.Fields.Count > 0 Then 'there is a field
        If rngNumber.Fields(1).Type = 3 Then rngNumber.Font.ColorIndex = vbBlue   'wdFieldRef=3
      Else
        rngNumber.Font.Color = vbRed
        ActiveDocument.Comments.Add Range:=rngNumber, Text:="Missing cross-reference field code"
      End If
      aRng.Collapse Direction:=0      'wdCollapseEnd
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 11-08-2021, 02:33 PM
scienceguy scienceguy is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2016
Advanced Beginner
How to determine if a Section number has a field code
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default

Thanks, Guessed! Unfortunately, it still doesn't catch the "Section n", where the Section n was entered as a true cross-reference.

Roy
Reply With Quote
  #4  
Old 11-08-2021, 08:29 PM
Guessed's Avatar
Guessed Guessed is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You are right. The Find is not hitting those ones so the If statement is pointless. I am surprised by this as I thought it would find those as numbers.

It appears you need to toggle the field codes and then you can search for those instances with a
.Text = "Section?^d"
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 11-09-2021, 04:53 AM
scienceguy scienceguy is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2016
Advanced Beginner
How to determine if a Section number has a field code
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default

Thank you for your continued help! That will certainly catch the properly formatted ones, but unfortunately, will miss the others. The only solution I have come up with is to do two rounds on searching for “Section [0-9.]{1,}” and one searching for “Section?^d”. Not very elegant but it works.
Reply With Quote
  #6  
Old 11-10-2021, 05:58 AM
gmayor's Avatar
gmayor gmayor is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Based on your example, the following modification to Andrew's code should work
Code:
Sub Macro2()
Dim aRng As Range
    Set aRng = ActiveDocument.Range
    With aRng.Find
        .ClearFormatting
        .Text = "Section"
        .MatchWildcards = True
        Do While .Execute  ' Loop until Word can no longer find the search string
            aRng.MoveEnd wdWord, 2
            aRng.MoveEndWhile "0123456789."
            aRng.Start = aRng.Words(2).Start
            If aRng.Fields.Count > 0 Then    'there is a field
                If aRng.Fields(1).Type = 3 Then aRng.Font.ColorIndex = wdBlue   'wdFieldRef=3
            Else
                aRng.Font.ColorIndex = wdRed
                ActiveDocument.Comments.Add Range:=aRng, Text:="Missing cross-reference field code"
            End If
            aRng.Collapse 0      'wdCollapseEnd
        Loop
    End With
    Set aRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 11-10-2021, 06:47 AM
scienceguy scienceguy is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2016
Advanced Beginner
How to determine if a Section number has a field code
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default

Hi Graham,

Thanks for the help! We're almost there! Unfortunately, it is picking up more than "Section n". I ran the code on a new file and attached it (test_file_gmayor.docm). For example, we got a hit on "Section in-". If you have a moment, could you take a look at page 4, please? There, you'll see where we're getting more than Section n. Any ideas?

Sorry for the trouble, but I really do appreciate the assistance!

Thanks,
Roy
Attached Files
File Type: docm test_file_gmayor.docm (61.4 KB, 9 views)
Reply With Quote
  #8  
Old 11-10-2021, 09:51 PM
gmayor's Avatar
gmayor gmayor is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

OK the following should address that issue
Code:
Sub Macro2()
Dim aRng As Range
    Set aRng = ActiveDocument.Range
    With aRng.Find
        .ClearFormatting
        .Text = "Section"
        .MatchWildcards = True
        Do While .Execute
            aRng.End = aRng.End + 1
            aRng.Collapse 0
            aRng.MoveEndWhile "0123456789."
            If aRng.Fields.Count > 0 Then
                If aRng.Fields(1).Type = 3 Then aRng.Font.ColorIndex = wdBlue
            Else
                If Len(aRng) > 0 Then
                    aRng.Font.ColorIndex = wdRed
                    ActiveDocument.Comments.Add _
                            Range:=aRng, _
                            Text:="Missing cross-reference field code"
                End If
            End If
            aRng.Collapse 0
        Loop
    End With
    MsgBox "Done!", vbInformation
    Set aRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 11-11-2021, 05:19 AM
scienceguy scienceguy is offline How to determine if a Section number has a field code Windows 10 How to determine if a Section number has a field code Office 2016
Advanced Beginner
How to determine if a Section number has a field code
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default

Many thanks, Graham and Andrew! You guys are ROCK STARS!!!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Field Code: Page number for current StyleRef Cosmo Word 4 11-15-2020 03:54 PM
Word 2016: How do I change a REF field code to display a reference to a Roman number as Arabic Peggy Sue Word 1 08-15-2017 09:50 PM
How to determine if a Section number has a field code VBA Code to search for field codes with certain text before the Field code and to change style welcometocandyland Word VBA 4 02-08-2017 06:53 PM
How to determine if a Section number has a field code Is there a way programmatically to determine the footnote number style, if other than arabic numbers gn4619 Word VBA 2 11-23-2015 11:00 PM
How to determine if a Section number has a field code Version number from document filename via field code palanski Word 3 10-15-2014 01:54 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:03 PM.


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