Using bookmarks/fields with preprinted forms

T

TC

Hi. Any advice for the following is greatly appreciated

On my word document, there are several bookmarks that I am attempting to populate from vba
This document originally was protected (forms) and when I used the following statement, I would receive an error that I was not allowed to add text to a protected area of the document

ActiveDocument.Bookmarks("bookmark1").Range.Selec
Selection.Text = "test

So I unprotected the document, and all works fine
HOWEVER ..
this document is setup to print data only from forms -- to be used with a preprinted form. If you manually enter text into the bookmark area, the data (only) will print. If I unprotect the document, the bookmark seems to disappear (if manually entering text, or populating from a macro), and the text will not print

What approach should I be taking here
Can I somehow unprotect the doc in vba, populate the fields and then protect again
Or should I be using something different than bookmarks

Thanks
Tro
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < TC > écrivait :
In this message, < TC > wrote:

|| Hi. Any advice for the following is greatly appreciated.
||
|| On my word document, there are several bookmarks that I am attempting to
populate from vba.
|| This document originally was protected (forms) and when I used the
following statement, I would
|| receive an error that I was not allowed to add text to a protected area
of the document:
||
|| ActiveDocument.Bookmarks("bookmark1").Range.Select
|| Selection.Text = "test"
||
|| So I unprotected the document, and all works fine.
|| HOWEVER ...
|| this document is setup to print data only from forms -- to be used with a
preprinted form. If
|| you manually enter text into the bookmark area, the data (only) will
print. If I unprotect the
|| document, the bookmark seems to disappear (if manually entering text, or
populating from a
|| macro), and the text will not print.

It is not clear to me what you are trying to achieve...
Are those bookmarks you are trying to edit via VBA regular bookmarks, or are
they form fields name as well... ? In other words, are you trying to change
the value of bookmarked text outside the form fields (in the protected
area), or the value of a bookmarked form field (the unprotected area that
users can edit)?

Also, these bookmarks you are trying to edit, should they print along with
the form field data? Or are you interested only in modifying some bookmarked
text in the protected area of the document, and then let users fill in the
form fields and print only the field data?

As for disappearing bookmarks... that is an annoying side effect of changing
their content with VBA. There is a way around that though:
Paste the following code in your code module

'_______________________________________
Sub ChangeBookmarkText()

ModifyBookmark "Bkm_Test", "This is new text"

End Sub
'_______________________________________

'_______________________________________
Function ModifyBookmark _
(BkmName As String, BkmNewText As String)

Dim BkmText As Range

Set BkmText = ActiveDocument.Bookmarks(BkmName).Range

BkmText.Text = BkmNewText
ActiveDocument.Bookmarks.Add BkmName, BkmText

End Function
'_______________________________________

In the "ChangeBookmarkText" Sub, change the name of the bookmark and the
text you want. The sub will call the function.
If you only need it once in your code, then use the code inside the
function, replacing the variables (BkmName and BkmNewText ) with the your
string values.

||
|| What approach should I be taking here?
|| Can I somehow unprotect the doc in vba, populate the fields and then
protect again?
|| Or should I be using something different than bookmarks?
||
|| Thanks,
|| Troy

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

Alex Ivanov

ActiveDocument.FormFields("text1").Result = "Something"
ActiveDocument.FormFields("text2").Result = "Something else"
Field names are the bookmarks associated with formfields.

HTH,
Alex.

TC said:
Thanks Jean-Guy for your help, see my comments inline (*)...




----- Jean-Guy Marcil wrote: -----

Bonjour,

Dans son message, < TC > écrivait :
In this message, < TC > wrote:

|| Hi. Any advice for the following is greatly appreciated.
||
|| On my word document, there are several bookmarks that I am attempting to
populate from vba.
|| This document originally was protected (forms) and when I used the
following statement, I would
|| receive an error that I was not allowed to add text to a protected area
of the document:
||
|| ActiveDocument.Bookmarks("bookmark1").Range.Select
|| Selection.Text = "test"
||
|| So I unprotected the document, and all works fine.
|| HOWEVER ...
|| this document is setup to print data only from forms -- to be used with a
preprinted form. If
|| you manually enter text into the bookmark area, the data (only) will
print. If I unprotect the
|| document, the bookmark seems to disappear (if manually entering text, or
populating from a
|| macro), and the text will not print.

It is not clear to me what you are trying to achieve...
Are those bookmarks you are trying to edit via VBA regular bookmarks, or are
they form fields name as well... ? In other words, are you trying to change
the value of bookmarked text outside the form fields (in the protected
area), or the value of a bookmarked form field (the unprotected area that
users can edit)?

*---- How do I address the form fields in vba? How do I know what these are named on the Word doc.
*---- If I highlight the bookmarked area, and choose properties, I can see
the name of the Bookmark, however I do not see the 'field' name ... what
have I missed here??
*---- I am trying to edit the value of the bookmark form field (the
unprotected area that users can edit)
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < TC > écrivait :
In this message, < TC > wrote:

|| Thanks Jean-Guy for your help, see my comments inline (*)...
||
||
||
||
|| ----- Jean-Guy Marcil wrote: -----
||
|| Bonjour,
||
|| Dans son message, < TC > écrivait :
|| In this message, < TC > wrote:
||
|| || Hi. Any advice for the following is greatly appreciated.
|| ||
|| || On my word document, there are several bookmarks that I am
attempting to
|| populate from vba.
|| || This document originally was protected (forms) and when I used
the
|| following statement, I would
|| || receive an error that I was not allowed to add text to a
protected area
|| of the document:
|| ||
|| || ActiveDocument.Bookmarks("bookmark1").Range.Select
|| || Selection.Text = "test"
|| ||
|| || So I unprotected the document, and all works fine.
|| || HOWEVER ...
|| || this document is setup to print data only from forms -- to be
used with a
|| preprinted form. If
|| || you manually enter text into the bookmark area, the data (only)
will
|| print. If I unprotect the
|| || document, the bookmark seems to disappear (if manually entering
text, or
|| populating from a
|| || macro), and the text will not print.
||
|| It is not clear to me what you are trying to achieve...
|| Are those bookmarks you are trying to edit via VBA regular
bookmarks, or are
|| they form fields name as well... ? In other words, are you trying to
change
|| the value of bookmarked text outside the form fields (in the
protected
|| area), or the value of a bookmarked form field (the unprotected area
that
|| users can edit)?
||
|| *---- How do I address the form fields in vba? How do I know what these
are named on the Word
|| doc.
|| *---- If I highlight the bookmarked area, and choose properties, I can
see the name of the
|| Bookmark, however I do not see the 'field' name ... what have I missed
here??
||
|| *---- I am trying to edit the value of the bookmark form field (the
unprotected area that users
|| can edit)

Alex has already addressed these 3 points in his reply to your second post.

||
|| Also, these bookmarks you are trying to edit, should they print
along with
|| the form field data? Or are you interested only in modifying some
bookmarked
|| text in the protected area of the document, and then let users fill
in the
|| form fields and print only the field data?
||
|| *---- What is typed in the bookmarked area is what I want to print out.

If the bookmark refers to a formfield name, then it should print as long as
the form field is preserved when you change its value through VBA.
The problem you had before was that you tried to change the bookmark as if
it was text, in this case the bookmark is also a field name in a protected
form. The code is different. See the code Ales posted, it should do the
trick. Forget the code I posted as I thought you were trying to change text
bookmarks. With the code like the one Alex posted you should not need to
protect/unprotect...


--
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