?
Solved

Macro: remove all reference fields in a word 2010 document

Posted on 2013-01-12
5
Medium Priority
?
1,012 Views
Last Modified: 2013-02-01
Hello, I have several Word 2010 documents that contain reference fields (EndNote citations, Table/figure references and some bookmark fields).  This can lie in most places in the document including headers/footers and within tables.  I would like some help in constructing a macro to cascade through the entire word document to remove these elements.  Any help would be greatly appreciated.

Thanks in advance,
Beverly
0
Comment
Question by:Bevos
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38771185
You could use a modification of the updating code from here:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_27989824.html
Sub CallAllStories()
    AllStories ActiveDocument
End Sub
Sub AllStories(doc As Word.Document)
    Dim rng As Word.Range
    Dim hdr As HeaderFooter
    Dim r As Integer
    Dim sh As Shape
    Dim ilsh As InlineShape
    
    For Each rng In doc.StoryRanges
        r = 0
         Do
            r = r + 1
            ProcessRange rng
            Select Case rng.StoryType
                Case wdEvenPagesHeaderStory To wdFirstPageFooterStory 'any sort of header or footer
                    'look in 'floating' shapes
                    For Each sh In rng.ShapeRange
                        If sh.TextFrame.HasText Then
                            ProcessRange sh.TextFrame.TextRange
                        End If
                    Next sh
                    'look in inline shapes
                    For Each ilsh In rng.InlineShapes
                        ProcessRange ilsh.Range
                    Next ilsh
            End Select
            Set rng = rng.NextStoryRange
        Loop Until rng Is Nothing
    Next rng
End Sub

Sub ProcessRange(rng As Range)
    Dim fld As Field
    Dim f As Integer
    
    For f = 1 To rng.Fields.Count
        Set fld = rng.Fields(f)
        If fld.Type = wdFieldRef Then
            fld.Delete
        End If
    Next f
End Sub

Open in new window

0
 

Author Comment

by:Bevos
ID: 38773020
Hi Graham, thanks for the help again!  I'm getting an error when executing the macro on the line 'Set fld = range.fields(f)' with the error message reading, "Run-time error '5941': The requested member of the collection does not exist.' Do you know what could be causing the problem?

Thanks,
Bev
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1000 total points
ID: 38773071
Deleting a field decreases the field count, but it doesn't seem to be evaluated in time to stop an extra iteration of the loop when the indexing variable (f) exceeds the current field count.
This code restarts at the beginning of the range after a deletion.
Sub ProcessRange(rng As Range)
    Dim fld As Field
    Dim f As Integer
    Dim bFound As Boolean
   
    Do
        bFound = False
        For Each fld In rng.Fields
            If fld.Type = wdFieldRef Then
                fld.Delete
                bFound = True
                Exit For
            End If
            f = f + 1
        Next fld
    Loop While bFound
End Sub
0
 

Author Comment

by:Bevos
ID: 38774610
Hi Graham, this one works just fine with no errors during the execution but some fields aren't being removed.  This is in the same template type of file that you helped me with earlier that has a header area with a table.  These reference fields are formatted somewhat differently (when using SHIFT+F9).  For example in the header the references are formatted as { REF Title \* Mergeformat } whereas in the body of the report they would appear { REF Title }.  Also, is there a way to get this to include table and figure captions? These seem to appear as { SEQ Table \* ARABIC }.

Thanks for any feedback,
Bev
0
 

Author Closing Comment

by:Bevos
ID: 38844996
Doesn't actually work as given but was a good effort. Needed a little extra help but I guess this is fine.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question