Solved

Macro: remove all reference fields in a word 2010 document

Posted on 2013-01-12
5
949 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
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…
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.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

763 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

11 Experts available now in Live!

Get 1:1 Help Now