Reference Field Error!

S

singeredel

I have the following sentence and reference fields in a template:

Thank you, { REF AttentionTitle\* CharFormat } {REF AttentionLineLast \*
Charformat }, for allowing me to take part in the care of your patient.

After running the program to create the document using the template,
sometimes the fields pertaining to these references are blank and therefore
it leaves the error message "Error! Reference Source not found. Error!
Reference Source not found" in the reference fields, as follows:

"Thank you, { Error! Reference source not found. } { Error! Reference source
note found }, for allowing me to take part..."

I would like to know how to write code to just delete these messages if
these errors are found so that the text reads:

"Thank you for allowing me..."

Thanks!
 
J

Jay Freedman

singeredel said:
I have the following sentence and reference fields in a template:

Thank you, { REF AttentionTitle\* CharFormat } {REF AttentionLineLast
\* Charformat }, for allowing me to take part in the care of your
patient.

After running the program to create the document using the template,
sometimes the fields pertaining to these references are blank and
therefore it leaves the error message "Error! Reference Source not
found. Error! Reference Source not found" in the reference fields, as
follows:

"Thank you, { Error! Reference source not found. } { Error! Reference
source note found }, for allowing me to take part..."

I would like to know how to write code to just delete these messages
if these errors are found so that the text reads:

"Thank you for allowing me..."

Thanks!

You don't need macro code. What you do need is a somewhat complex field code
that displays a result only when it isn't the error message. The first field
becomes

{ IF { REF AttentionTitle } <> "Error! Reference source not found." { QUOTE
", { REF AttentionTitle \* CharFormat }" } }

and the second field becomes

{ IF { REF AttentionLineLast } <> "Error! Reference source not found." {
QUOTE "{ REF AttentionLineLast \*
Charformat }, " } }

Notice the placement of the commas inside the QUOTE field rather than in the
text outside the field -- if the fields don't display anything, you don't
want the commas either. You may have to play with this a bit to get the
placement of spaces to be correct.

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

singeredel

Thanks. I appreciate your input!

I have played with those complex field codes in the past and it is just too
confusing and I could never get it to work because I do need to have the
punctuation deleted also. I just preferred to have it in the code to
hopefully make it simpler.
 
J

Jay Freedman

I wouldn't consider the necessary code to be simpler than the fields, but
very well...

Insert a bookmark starting at the left side of the first comma and ending at
the right side of the second comma. I named the bookmark "RefAttention", but
if you choose a different name you need only change the value assigned to
the constant BkmNm in the macro.

Sub DeleteErroredFields()
Const BkmNm = "RefAttention"
Dim bCodesVisible As Boolean

bCodesVisible = ActiveWindow.View.ShowFieldCodes
ActiveWindow.View.ShowFieldCodes = False

With ActiveDocument.Bookmarks
If .Exists(BkmNm) Then
.Item(BkmNm).Range.Fields.Update
If InStr(.Item(BkmNm).Range.Text, "Error!") Then
.Item(BkmNm).Range.Delete
End If
End If
End With

ActiveWindow.View.ShowFieldCodes = bCodesVisible
End Sub
 

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