Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-22-2024, 03:07 AM
Shelley Lou Shelley Lou is offline VBA Unlink Cross Reference Fields for Specific Text Windows 10 VBA Unlink Cross Reference Fields for Specific Text Office 2016
Expert
VBA Unlink Cross Reference Fields for Specific Text
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Unlink Cross Reference Fields for Specific Text

I am currently updating the styles of a lot of 200 page documents and need to unlink any cross references within the document that begin with specific text 'Part' or 'Schedule' but leave all other cross references linked. How would I go about creating a macro to only unlink if the cross ref field begins with 'Part' or 'Schedule'. Thanks.


Image of cross refs Part and Schedule before unlinked
Unlink Specific Cross Refs.JPG

Image of cross refs Part and Schedule after unlinked
Image of Unlinked Cross Refs.JPG

Unlink cross ref for Part and Schedule.DOCX
Reply With Quote
  #2  
Old 10-22-2024, 09:47 AM
gmayor's Avatar
gmayor gmayor is offline VBA Unlink Cross Reference Fields for Specific Text Windows 10 VBA Unlink Cross Reference Fields for Specific Text 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

That should be fairly straightforward:


Code:
Sub Macro1()
Dim oFld As Field
Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
            If oFld.Type = wdFieldRef Then
                If oFld.Result Like "Part*" Or _
                    oFld.Result Like "Schedule*" Then
                    oFld.Unlink
                End If
            End If
        Next oFld
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                For Each oFld In oStory.Fields
                    If oFld.Type = wdFieldRef Then
                        If oFld.Result Like "Part*" Or _
                            oFld.Result Like "Schedule*" Then
                            oFld.Unlink
                        End If
                    End If
                Next oFld
            Wend
        End If
    Next oStory
lbl_Exit:
    Set oStory = Nothing
    Set oFld = Nothing
    Exit Sub
End Sub
Check before and after running the macro with

Code:
Sub Macro2()
Dim oFld As Field
Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        For Each oFld In oStory.Fields
            If oFld.Type = wdFieldRef Then
                Debug.Print oFld.Result
            End If
        Next oFld
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                For Each oFld In oStory.Fields
                    If oFld.Type = wdFieldRef Then
                        Debug.Print oFld.Result
                    End If
                Next oFld
            Wend
        End If
    Next oStory
lbl_Exit:
    Set oStory = Nothing
    Set oFld = Nothing
    Exit Sub
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
  #3  
Old 10-22-2024, 11:34 AM
Shelley Lou Shelley Lou is offline VBA Unlink Cross Reference Fields for Specific Text Windows 10 VBA Unlink Cross Reference Fields for Specific Text Office 2016
Expert
VBA Unlink Cross Reference Fields for Specific Text
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Unlink Cross Reference Fields for Specific Text

Wow Graham, thank you so much, Macro1 works perfectly. What does Macro2 do, should I add it as a Call to Macro1 at the start and end perhaps - is it checking the document to ascertain if those types of cross reference exist in the document? If so, I could add a message box to say either found or not found maybe?
Reply With Quote
  #4  
Old 10-22-2024, 02:24 PM
Guessed's Avatar
Guessed Guessed is offline VBA Unlink Cross Reference Fields for Specific Text Windows 10 VBA Unlink Cross Reference Fields for Specific Text Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
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

Graham's Macro2 sends its output to the Immediate Window and essentially gives you a list of the remaining Ref fields values. I'm not sure on the value of running it but it does allow you to quickly review the document's ref fields to spot if there are any broken ones.

Something to be aware of when looking at your before and after screen grabs. You need to be careful of converting ref fields that start with Part* because a field starting with Parties or Party x would also get caught in that code. You could add a space "Part *" to eliminate that possibility but may also need a wildcard if there are non-breaking spaces in some cases.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 11-05-2024, 09:58 AM
Shelley Lou Shelley Lou is offline VBA Unlink Cross Reference Fields for Specific Text Windows 10 VBA Unlink Cross Reference Fields for Specific Text Office 2016
Expert
VBA Unlink Cross Reference Fields for Specific Text
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Unlink Cross Reference Fields for Specific Text

Hi Andrew, thank you for the heads up, I have insert a space after Part and all seems to be working quite well - many thanks for the update
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Unlink Cross Reference Fields for Specific Text Determining if an in-text table reference was created with Word's native cross reference feature scienceguy Excel Programming 2 10-25-2021 04:44 AM
Macro for removing cross-reference fields with specific contents Ulodesk Word VBA 5 07-13-2017 08:27 AM
VBA Unlink Cross Reference Fields for Specific Text Cross Reference REF fields Unlink mktate Word VBA 7 06-24-2016 05:55 AM
VBA Unlink Cross Reference Fields for Specific Text Totaling specific configuration selections that cross reference multiple fields (tricky) lonniepoet Excel Programming 1 06-09-2016 09:54 AM
manipulating cross-reference fields _wim_ Word 0 12-10-2010 05:52 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:46 AM.


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