Using Bookmarks to store User Form data, how to hide?

S

src

Hi there,

I'm automating our company's standard documents. The idea is to
collect info about the document and author, and automatically fill this
in at spots in the header and footer. This is to standardize how it's
input so users don't have to worry about getting the right format.

Previously, I used a stack of ASK fields to collect the info into
Bookmarks, then put cross references in the header and footer. I put
the ASK fields on the front title page, which doesn't get used much and
so were out of the way of stray keystrokes. This worked well since the
ASK fields were "invisible" unless you toggled field codes. But all
those dialog boxes in a row were a bit tedious.

So now I've created a User Form that collects all the info in one spot
and inserts the data into Bookmarks.

I inserted the Bookmarks on the title page like I had the ASK fields,
but now I realize that the full text of the Bookmarks is showing up on
my page!

My question is: How can I make these Bookmarks invisible like the ASK
fields?

I have already moved the obvious Bookmarks off the title page and into
the header and footer (Doc No., Doc Title, etc), but some of the
Bookmarks I will only use as cross references later in the body of the
document (which doesn't exist yet) such as Customer Name.

I've tried highlighting the Bookmarks on the title page and selecting
Font > Hidden, but when I insert a cross reference, the formatting
comes along and the reference is also hidden!

Is there any kind of switch or place I can stash Bookmarks that will
make them invisible to the user, yet be easy to cross reference to?

THANKS,
Matt
 
J

Jay Freedman

You can store any kind of information in custom document properties,
which you can create and edit in File > Properties > Custom. Then you
can display the values in the document with DocProperty fields.

The userform can create/modify the custom document properties --
instead of using, for example,

ActiveDocument.Bookmarks("Department").Range.Text = Dept.Text

substitute the statement

ActiveDocument.CustomDocumentProperties.Add _
Name:="Department", LinkToContent:=False, _
Value:=Dept.Text, Type:=msoPropertyTypeString

Then insert the field {DocProperty Department} wherever you want
this value to be displayed.

You can also use document variables in the same way, except that these
aren't editable through any dialog; you would have to provide VBA
support for making any changes.

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

src

Ah, now there's something I hadn't thought of. I remembered doc
properties, but forgot there were custom ones.

I set up all my custom doc properties, and have been able to make a
nice little button to insert them like cross references wherever the
cursor is.

But I'm having trouble updating the properties with my user form.
Here's what I've got (all of this is in the Userform code):

Private Sub cmdOK_Click()
Call UpdateDocProperties("PreparedBy", txtPreparedBy)
End Sub

Sub UpdateDocProperties(dpname, dptext)
ActiveDocument.CustomDocumentProperties.Add _
Name:=dpname, LinkToContent:=False, _
Value:=dptext, Type:=msoPropertyTypeString
End Sub

I'm getting:
Run-time Error '-2147467259 (80004005)':
Automation Error
Unspecified Error

Seems that if the dpname already exists, it crashes trying to add it
again. I hope you'll excuse my limited knowledge of VBA, but is there
a slightly different command that will allow it to update instead of
add?

Thanks for the tip!
Matt
 
J

Jezebel

If the property already exists, you can set it instead; although that runs a
further risk (albeit minor) that the property exists but with the wrong data
type. Most robust coding is to delete the property first, ignoring any
error --

on error resume next
ActiveDocument.CustomDocumentProperties(dpname).delete
on error goto 0
ActiveDocument.CustomDocumentProperties.Add ...
 
S

src

Fantastic! Exactly what I was looking for. Thanks for all the help.
Using custom Document Properties is a marked improvement over trying to
manipulate and update Bookmarks.

Thanks,
Matt
 

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