Deleting Empty Cross References in code

D

ddowell

I have created a test Word document with a bookmark and cross
reference to it. In the case that when the bookmark is empty, I want
to delete the cross reference and bookmark so that there is no gap in
my text where those items appear.

I have played around with the following code but can't seem how to
find the cross references that relate to the empty bookmark.

Response.Write(doc.Bookmarks.Count) ' will print 1
Response.Write(doc.fields.Count) ' will print 2 one for the
bookmark, and one for the cross reference field (I assume)

if (doc.Bookmark(i).Result = "") then
'get cross referenced fields and delete
'delete bookmark

end if

Bottom line, I don't want gaps in my document where there is no data
being supplied to these fields. If there is a different/better way to
do this, please advise.

Thanks
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

HI D Dowell,

This should do what you want

Dim i As Integer, bmname As String, aref As Field
For i = ActiveDocument.Bookmarks.Count To 1 Step -1
If Len(ActiveDocument.Bookmarks(i).Range) < 2 Then
bmname = ActiveDocument.Bookmarks(i).Name
For Each aref In ActiveDocument.Fields
If aref.Type = wdFieldRef Then
If InStr(aref.Code, bmname) > 0 Then
aref.Delete
ActiveDocument.Bookmarks(i).Delete
End If
End If
Next aref
End If
Next i

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

ddowell

Doug:

Thanks for the reply.

The documents I am populating with data are legal documents and
therefore, are secured by protecting the document. It seems when I
use the code below to access the range of each field, I get an error
stating that the property is not available because the object refers
to a protected area of the document. When I unprotect the doc, I can
access the property, but then my fields are not updated with the data.

I've tried starting with the doc protected, setting the form data,
then unprotecting and searching fields for empty data, then
protecting, but the data is then gone from my doc.

Here is a simplified look at my code. It's in javascript because it
has to run client side so that the instance of word isn't running on
the server, but the object model is the same.

var doc = word.documents.open(//some document);
for (var k = 1; k <= doc.Bookmarks.Count; k++) {
var bookmark = doc.Bookmarks(k);
var field = doc.formfields(bookmark.Name);
field.result = ""; //some value

if (field.result == "") {
for (var i=1 i <= doc.Fields.Count; i++) {
if (doc.fields(i).Type == 3) {
if (doc.fields(i).Code.Text.indexOf(bookmark.Name)) {
doc.Fields(i).Delete();
}
}
}
}
}

doc.Fields.Update();
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

HI D Dowell,

Use code to unprotect and reprotect the document. You can then set NoReset
so that the fields are not emptied when the document is reprotected

ActiveDocument.Protect wdAllowOnlyFormFields, NoReset

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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