Solved

Macro: remove all reference fields in a word 2010 document

Posted on 2013-01-12
5
953 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

912 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now