Insert Fields to Bookmark

G

Geoff332

Hi,

I have a document where I want to enter particular fields and text,
depending on a condition. When completed, the bookmark should refer to the
inserted text. The fields and bookmarks are already defined in the document.
It's important that the fields be inserted in the bookmark, rather than just
the value.

The code should look something like:

<delete existing bookmark text>
If (condition) Then
<add field1 to bookmark>
<add text to bookmark>
<add field2 to bookmark>
Else
<add field3 to bookmark>
End If

The cleanest way to do it would be to build the appropriate insert string
then replace the bookmark. I can do this easily with text, but not with
fields.

Any suggestions would be much appreciated.

Thanks,
Geoff.
 
D

Doug Robbins - Word MVP

What type of fields are you talking about. It might really be best to
describe the purpose of the document/template rather than, or at least in
addition to the way in which you are trying to accomplish it. There may be
a better way.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Geoff332

The documents in question all relate to a family of products - with custom
fields telling me about the product. If document refers to a specific product
in the family, then I want to display the product family (prdFamily), product
name (prdName) and product version (prdVersion). If a document is either
generic or refers to more than one specific product, then I want none of the
information displayed.

I had originally handled this by leaving the fields there and populating
prdName and prdVersion with a space. The problem is prdFamily will always be
populated and used elsewhere in the document - but in one instance, I don't
want it displayed. To further complicate it, I do want these as dynamic
fields and want the document to handle being changed from a specific product
to a generic document and back without breaking.

The point I had reached was to put the fields inside a bookmark. I can use
VBA to delete the fields; I can insert the fields and text at the bookmark,
but they are now outside of the bookmark. So if I try and reverse the process
again, the inserted fields aren't deleted.

Thanks,
Geoff.
 
D

Doug Robbins - Word MVP

Use DocVariable fields in the document (no bookmark required) and use VBA to
set the value of the corresponding variables and update the fields in the
document so that the DocVariable field display the values

With ActiveDocument
.Variables("varprdFamily").Value = "Some Family"
.Variables("varprdName").Value = "Some Name"
.Variables("varprdVersion").Value = "Some Version"
.Range.Fields.Update
End With

For the above code, you would use the following fields

{ DOCVARIABLE varprdFamily }
{ DOCVARIABLE varprdName }
{ DOCVARIABLE varprdVersion }

The beauty about document variables is that you just change the value
assigned to the variable and update the fields and it works.

However, if you are set on using bookmarks, see the article "Working with
Bookmarks in VBA" at:

http://www.word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
F

fumei via OfficeKB.com

Could you clarify:

"The fields and bookmarks are already defined in the document.
It's important that the fields be inserted in the bookmark, rather than just
the value."

The fields are already in the document...but you are placing them AGAIN in
the bookmark?


Use DocVariable fields in the document (no bookmark required) and use VBA to
set the value of the corresponding variables and update the fields in the
document so that the DocVariable field display the values

With ActiveDocument
.Variables("varprdFamily").Value = "Some Family"
.Variables("varprdName").Value = "Some Name"
.Variables("varprdVersion").Value = "Some Version"
.Range.Fields.Update
End With

For the above code, you would use the following fields

{ DOCVARIABLE varprdFamily }
{ DOCVARIABLE varprdName }
{ DOCVARIABLE varprdVersion }

The beauty about document variables is that you just change the value
assigned to the variable and update the fields and it works.

However, if you are set on using bookmarks, see the article "Working with
Bookmarks in VBA" at:

http://www.word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm
The documents in question all relate to a family of products - with custom
fields telling me about the product. If document refers to a specific
[quoted text clipped - 63 lines]
 
F

fumei via OfficeKB.com

In any case, if you are really trying to add fields into the bookmark (that
is, actually inside the bookmark), you wil have to use the bookmark range,
and the Collapse method. Especially if you are doing multiples like:


<add field1 to bookmark>
<add text to bookmark>
<add field2 to bookmark>

where you have a field, then text, and then another field.

Could you clarify:

"The fields and bookmarks are already defined in the document.
It's important that the fields be inserted in the bookmark, rather than just
the value."

The fields are already in the document...but you are placing them AGAIN in
the bookmark?
Use DocVariable fields in the document (no bookmark required) and use VBA to
set the value of the corresponding variables and update the fields in the
[quoted text clipped - 26 lines]
 
G

Geoff332

Unfortunately, I can't blank out all of the fields: they are used elsewhere
in the document. In all cases, at least one of them will be populated; I just
want to stop one instance of the field from being displayed. One alternative
is to put the same data in two fields. That'll work, but isn't very elegant.

Users, being users, will change their mind and do strange things. So I need
the solution to work from two possible starting scenarios:
1. The appropriate fields are already in the document (this is how the
document already works).
2. The fields are not already in the document and need to be inserted (this
would happen if someone had previously deleted them).

My idea was to enclose the fields in a bookmark so I know where they are,
then I can remove them (delete the content of the bookmark) or add them
(enclose them in the bookmark) as required. A bookmark seemed like the right
tool to achieve this, but if there's a better approach, then I'll try it out.

I'll look into the links and advice from yourself and fumei and see if I can
figure it out.

Thanks,
Geoff.
 

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