Insertion of a field code at the end of each paragraph

A

andreas

Dear Experts:

I need to achieve the following with a macro.

- go to the end of every paragraph of the current document
- insert the following field code immediately before the paragraph
mark: { NOTEREF fn_1 \h } .

The "fn_1" part of the field code is a bookmark whose numbered part is
to be INCREMENTED for every insertion. That is: The first insertion of
this field at the end of the first paragraph should look like this:
{ NOTEREF fn_1 \h }, the second insertion should look like this
{ NOTEREF fn_2 \h }, the third insertion must be { NOTEREF fn_3 \h }
and so forth.

Is this possible?

Your professional help is much appreciated. Thank you very much in
advance. Regards, Andreas
 
J

Jay Freedman

andreas said:
Dear Experts:

I need to achieve the following with a macro.

- go to the end of every paragraph of the current document
- insert the following field code immediately before the paragraph
mark: { NOTEREF fn_1 \h } .

The "fn_1" part of the field code is a bookmark whose numbered part is
to be INCREMENTED for every insertion. That is: The first insertion of
this field at the end of the first paragraph should look like this:
{ NOTEREF fn_1 \h }, the second insertion should look like this
{ NOTEREF fn_2 \h }, the third insertion must be { NOTEREF fn_3 \h }
and so forth.

Is this possible?

Your professional help is much appreciated. Thank you very much in
advance. Regards, Andreas

Yes, it's possible, although there are some pitfalls.

One possible problem occurs if the document contains "empty" paragraph
marks. This method of making space between paragraphs is to be
discouraged -- use the paragraph style's Space After setting to do this --
but it's still common. Presumably you don't want fields added to such
paragraphs.

Another occurs if some of the footnote references haven't been marked with
bookmarks. If you add a NoteRef field pointing to a nonexistent bookmark
name, you'll get an "Error! Bookmark not defined" result from the field. You
might want this to alert you to the missing bookmark, but the macro below
will simply skip that number and go on to the next one. Even that might not
work properly, depending on exactly how the document's markup is different
from the expected sequence. Still, it's worth a try and tinkering to make it
work with your particular document.

Sub InsertNoteRef()
Dim nPara As Long, nBk As Long
Dim oRg As Range
Dim BkOK As Boolean

nBk = 1

For nPara = 1 To ActiveDocument.Paragraphs.Count
Set oRg = ActiveDocument.Paragraphs(nPara).Range

BkOK = BookmarkOK(nBk)
While (Not BkOK) And _
(nBk <= ActiveDocument.Bookmarks.Count)
nBk = nBk + 1
BkOK = BookmarkOK(nBk)
Wend

If (Len(oRg.Text) > 1) And BkOK Then

With oRg
.Collapse wdCollapseEnd
.Move wdCharacter, -1
ActiveDocument.Fields.Add _
Range:=oRg, Type:=wdFieldNoteRef, _
Text:="fn_" & CStr(nBk) & " \h", _
PreserveFormatting:=False
End With

nBk = nBk + 1
End If
Next
End Sub

Private Function BookmarkOK(nBookmark As Long) As Boolean
BookmarkOK = ActiveDocument.Bookmarks.Exists( _
"fn_" & CStr(nBookmark))
End Function

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
C

Cindy M.

Hi Andreas,
I need to achieve the following with a macro.

- go to the end of every paragraph of the current document
- insert the following field code immediately before the paragraph
mark: { NOTEREF fn_1 \h } .

Something like this should get you started

Sub InsertFldEndOfAllParas()
Dim doc As Word.Document
Dim rngPara As Word.Range
Dim paraCounter As Long
Dim fldCodeStart As String
Dim fldCodeEnd As String

fldCodeStart = "NoteRef fn_"
fldCodeEnd = " \h"
Set doc = ActiveDocument
For paraCounter = 1 To doc.Paragraphs.Count
Set rngPara = doc.Paragraphs(paraCounter).Range
rngPara.Collapse wdCollapseEnd
rngPara.MoveEnd wdCharacter, -1
doc.Fields.Add Range:=rngPara, Text:=fldCodeStart & _
CStr(paraCounter) & fldCodeEnd, PreserveFormatting:=False
Next
End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
A

andreas

Hi Andreas,



Something like this should get you started

Sub InsertFldEndOfAllParas()
    Dim doc As Word.Document
    Dim rngPara As Word.Range
    Dim paraCounter As Long
    Dim fldCodeStart As String
    Dim fldCodeEnd As String

    fldCodeStart = "NoteRef fn_"
    fldCodeEnd = " \h"
    Set doc = ActiveDocument
    For paraCounter = 1 To doc.Paragraphs.Count
        Set rngPara = doc.Paragraphs(paraCounter).Range
        rngPara.Collapse wdCollapseEnd
        rngPara.MoveEnd wdCharacter, -1
        doc.Fields.Add Range:=rngPara, Text:=fldCodeStart & _
          CStr(paraCounter) & fldCodeEnd, PreserveFormatting:=False
    Next
End Sub

Cindy Meister
INTER-Solutions, Switzerlandhttp://homepage.swissonline.ch/cindymeister(last update Jun 17
2005)http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)

Hi Cindy,

thank you very much for your terrific help. Exactly what I wanted.
Thank you. Regards, Andreas
 
A

andreas

Yes, it's possible, although there are some pitfalls.

One possible problem occurs if the document contains "empty" paragraph
marks. This method of making space between paragraphs is to be
discouraged -- use the paragraph style's Space After setting to do this --  
but it's still common. Presumably you don't want fields added to such
paragraphs.

Another occurs if some of the footnote references haven't been marked with
bookmarks. If you add a NoteRef field pointing to a nonexistent bookmark
name, you'll get an "Error! Bookmark not defined" result from the field. You
might want this to alert you to the missing bookmark, but the macro below
will simply skip that number and go on to the next one. Even that might not
work properly, depending on exactly how the document's markup is different
from the expected sequence. Still, it's worth a try and tinkering to makeit
work with your particular document.

Sub InsertNoteRef()
    Dim nPara As Long, nBk As Long
    Dim oRg As Range
    Dim BkOK As Boolean

    nBk = 1

    For nPara = 1 To ActiveDocument.Paragraphs.Count
        Set oRg = ActiveDocument.Paragraphs(nPara).Range

        BkOK = BookmarkOK(nBk)
        While (Not BkOK) And _
            (nBk <= ActiveDocument.Bookmarks.Count)
                nBk = nBk + 1
                BkOK = BookmarkOK(nBk)
        Wend

        If (Len(oRg.Text) > 1) And BkOK Then

            With oRg
                .Collapse wdCollapseEnd
                .Move wdCharacter, -1
                ActiveDocument.Fields.Add _
                    Range:=oRg, Type:=wdFieldNoteRef, _
                    Text:="fn_" & CStr(nBk) & " \h", _
                    PreserveFormatting:=False
            End With

            nBk = nBk + 1
        End If
    Next
End Sub

Private Function BookmarkOK(nBookmark As Long) As Boolean
    BookmarkOK = ActiveDocument.Bookmarks.Exists( _
            "fn_" & CStr(nBookmark))
End Function

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso
all may benefit.- Hide quoted text -

- Show quoted text -

Hi Jay,

thank you very much for your quick and detailed help. I am aware of
this "empty" paragraph problem. I tried out your code but regrettably
no field codes are added. If I comment out all the lines that refer to
the bookmark the macro is running fine, but then there is no
incrementing of the bookmark number.

Again, thank you very much for your professinal help. Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top