Checking if enclosing Bookmark contains text

R

Roger Marrs

I'd like to check a bookmark to see if it contains text ("a--z"). If it
does, then at a different bookmark I want to insert text, if the bookmark
does not contain the matching text, then I want to insert different text at
a third bookmark.

I've tried the following code, which doesn't produce any result.

Dim AttorneyWaiver As String
Dim AttorneyAppointed As String
Dim vAttorney As String

AttorneyWaiver = "Text version one."
AttorneyAppointed = "Text version two, " & vAttorney & ", goes here."
vAttorney = ActiveDocument.Bookmarks("DefenseAttorney").Range.Text

With ActiveDocument.Bookmarks("DefenseAttorney2").Range.Find
.Text = "a--z"
.Wrap = wdFindContinue
If .Execute Then
ActiveDocument.Bookmarks("AttorneyWaiver").Range.InserstAfter
AttorneyWaiver
Else:
ActiveDocument.Bookmarks("AttorneyAppointed").Range.InsertAfter
AttorneyAppointed
End If
End With
 
J

Jean-Guy Marcil

Roger Marrs was telling us:
Roger Marrs nous racontait que :
I'd like to check a bookmark to see if it contains text ("a--z"). If
it does, then at a different bookmark I want to insert text, if the
bookmark does not contain the matching text, then I want to insert
different text at a third bookmark.

I've tried the following code, which doesn't produce any result.

Dim AttorneyWaiver As String
Dim AttorneyAppointed As String
Dim vAttorney As String

AttorneyWaiver = "Text version one."
AttorneyAppointed = "Text version two, " & vAttorney & ", goes
here." vAttorney =
ActiveDocument.Bookmarks("DefenseAttorney").Range.Text

With ActiveDocument.Bookmarks("DefenseAttorney2").Range.Find
.Text = "a--z"
.Wrap = wdFindContinue
If .Execute Then

ActiveDocument.Bookmarks("AttorneyWaiver").Range.InserstAfter
AttorneyWaiver Else:
ActiveDocument.Bookmarks("AttorneyAppointed").Range.InsertAfter
AttorneyAppointed
End If
End With

Have you tried:

With ActiveDocument
If InStr(1, .Bookmarks("DefenseAttorney2").Range.Text, "a--z") > 0 Then
.Bookmarks("AttorneyWaiver").Range.InserstAfter AttorneyWaiver
Else
.Bookmarks("AttorneyAppointed").Range.InsertAfter AttorneyAppointed
End If
End With

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Roger Marrs

Hmmm, I tried the code suggested by Jean-Guy Marcil, but that didn't yield
a result either. I'm not getting any error messages, the macro appears to
execute, but it doesn't perform any text insertion whatsoever. I've double
checked the spelling of the bookmarks in my ActiveDocument with those in the
macro code and everything appears to be correct.

Basically, I have an enclosing bookmark in a form document that contains a
merge code. [a-DefenseAttorneyName-z] I've called that bookmark
DefenseAttorney2. The ActiveDocument is the document that is created as a
result of the merge. If there is a defense attorney whose name merges into
the document, then I get for example, [a-John Dewey-z]. If there is not an
attorney on the case, then I get [a--z] as the content of the bookmark. I
thought I could search on "a--z", and if that's found, then there is no
attorney, and the no attorney language could be inserted into the document.
If a--z is not found, then that means there is an attorney and the proper
text could be inserted in the document to account for that situation.

Maybe my whole approach is inappropriate and I need to go about this some
other way. I'm open to suggestions.

Roger
 
J

Jean-Guy Marcil

Roger Marrs was telling us:
Roger Marrs nous racontait que :
Hmmm, I tried the code suggested by Jean-Guy Marcil, but that didn't
yield a result either. I'm not getting any error messages, the macro
appears to execute, but it doesn't perform any text insertion
whatsoever. I've double checked the spelling of the bookmarks in my
ActiveDocument with those in the macro code and everything appears to
be correct.

The code I posted works as advertised, except for one typo:
The first InsertAfter has a typo in it, which I ma sure you caught.

If you fixed that and it still not works, then:

Are you sure you are assigning values to
AttorneyWaiver
AttorneyAppointed
??

In your last post, you mentioned a "form", which you did not in your
original post.

If it is a form, are the bookmarks you referred to in the original post
linked to formfields? Is the document locked for forms? Will the text to
insert be inserted *in* a formfield or outside a formfield (Next to one)?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Roger Marrs

It's still not working. My use of the term "form" was probably
inappropriate. It's not a form in the since of a Word form document. It's
a form that a third party app uses to merge Oracle DB data into an .rtf
document. I open the merged document in Word to perform edits.

I think I have assigned values. For simplicity sake I've abbreviated the
text that gets inserted in the actual document, but here's my Dim statements
and associated values:

Dim AttorneyWaiver As String
Dim AttorneyAppointed As String
Dim vAttorney As String

AttorneyWaiver = "I have been advised that ...''
AttorneyAppointed = "I am represented by counsel, " & vAttorney & ", and
...."
vAttorney = ActiveDocument.Bookmarks("DefenseAttorney").Range.Text

I think that should work, but maybe I'm missing something.
 
J

Jean-Guy Marcil

Roger Marrs was telling us:
Roger Marrs nous racontait que :
It's still not working. My use of the term "form" was probably
inappropriate. It's not a form in the since of a Word form document.
It's a form that a third party app uses to merge Oracle DB data into
an .rtf document. I open the merged document in Word to perform
edits.

I think I have assigned values. For simplicity sake I've abbreviated
the text that gets inserted in the actual document, but here's my Dim
statements and associated values:

Dim AttorneyWaiver As String
Dim AttorneyAppointed As String
Dim vAttorney As String

AttorneyWaiver = "I have been advised that ...''
AttorneyAppointed = "I am represented by counsel, " & vAttorney &
", and ..."
vAttorney = ActiveDocument.Bookmarks("DefenseAttorney").Range.Text

I think that should work, but maybe I'm missing something.

Sorry, I have tested the code I posted and it works. So I am not sure what
to say at this point...

Here it is again:

Sub test()

Dim AttorneyWaiver As String
Dim AttorneyAppointed As String
Dim vAttorney As String

vAttorney = ActiveDocument.Bookmarks("DefenseAttorney").Range.Text

AttorneyWaiver = "I have been advised that ...''"
AttorneyAppointed = "I am represented by counsel, " & vAttorney & ", and..."

With ActiveDocument
If InStr(1, .Bookmarks("DefenseAttorney2").Range.Text, "a--z") > 0 Then
.Bookmarks("AttorneyWaiver").Range.InsertAfter AttorneyWaiver
Else
.Bookmarks("AttorneyAppointed").Range.InsertAfter AttorneyAppointed
End If
End With


End Sub

Of course, you have to define "vAttorney " before you can use it in
"AttorneyAppointed", but that would not prevent the code from running.

When you say "It's still not working", what happens exactly? That statement
does not help us help you.
Where is your bookmarked text?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Roger Marrs

You were absolutely correct. The code you supplied worked just fine. I
feel rather foolish to admit that I was working on one copy of the macro
code, but running a different copy, thus my experience of the macro not
doing anything at all. Thank you for the assistance.

Roger
 
J

Jean-Guy Marcil

Roger Marrs was telling us:
Roger Marrs nous racontait que :
You were absolutely correct. The code you supplied worked just fine.
I feel rather foolish to admit that I was working on one copy of the
macro code, but running a different copy, thus my experience of the
macro not doing anything at all. Thank you for the assistance.

LOL

It happens to the best of us...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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