Solved

Macro: remove all reference fields in a word 2010 document

Posted on 2013-01-12
5
985 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

724 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