Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Macro: remove all reference fields in a word 2010 document

Posted on 2013-01-12
5
Medium Priority
?
1,002 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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses

636 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