bookmark size

G

Gonzo

Error stating that I am trying to paste too much into a bookmark.
Basically an array is created from a list and some user inputs and the
contents of the array are used to populate bookmarks in a new template
(bookmarks inserted after loading the template and then text is
pasted).Works flawlessly for a short sentence but gives the error if it is
longer.What is interesting is that if the error dialog is closed the paste
operation obviously worked and the text is intact! Is there a limit to the
size a bookmark can take?

open the template:
add the bookmarks:
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="MED" & intC
Selection.MoveDown unit:=wdLine, Count:=1
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="AMT" & intC
Selection.MoveDown unit:=wdLine, Count:=10
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="REF" & intC

Move to the bookmark and add the appropriate text from the array:
Selection.GoTo what:=wdGoToBookmark, Name:="MED" & i
Selection.TypeText astrMed(i, 0)
Selection.GoTo what:=wdGoToBookmark, Name:="AMT" & i
Selection.TypeText astrMed(i, 1)
Selection.GoTo what:=wdGoToBookmark, Name:="REF" & i
Selection.TypeText astrMed(i, 2)
Print the prescription and close the template.

Seems to choke if the text added wraps to the next line

Thanks for any enlightenment.
 
J

Jay Freedman

Hi, Gonzo,

I'm not sure why you're getting that error -- there is no restriction on the
size of text in a bookmark. However, I strongly recommend against using
Selection.GoTo and Selection.TypeText in an application like this, and I
think you'll find that the alternative also avoids the error.

After the bookmarks have been created, use code like this to populate them
without having to select them:

ActiveDocument.Bookmarks("MED" & i).Range.Text = astrMed(i, 0)
ActiveDocument.Bookmarks("AMT" & i).Range.Text = astrMed(i, 1)
ActiveDocument.Bookmarks("REF" & i).Range.Text = astrMed(i, 2)

As a further comment, you shouldn't be opening a template and creating
bookmarks in it. Instead, the unpopulated bookmarks should exist permanently
in the template, and your macro should be creating a new document *based on*
the template -- then the bookmarks will already be in place. You do this
with a line like

Documents.Add Template:="MyTemplate.dot"
 
G

Gonzo

Jay - thanks for the code and suggestions. The bookmarks are created
dynamically because the number of prescription blanks is variable, hence the
integer tag on the bookmark. To explain further, the prescription template
has a prescription blank in the R upper corner. This is copied at runtime
and pasted in columns to get the correct number of prescriptions as defined
by the calling proceedure. I can't print empty prescriptions and give them
to the patient (legal no no), I don't want to cut the pages and I need to be
able to have 1 or 2 or 3 or 4 prescriptions per page depending on my needs
at the time. I am making sense? Is there a better way as you have shown with
the range object?
 
J

Jay Freedman

Hi, Gonzo,

Here's how I would set this up:

The intended way of using templates is that you create the template, and
after that it's never opened for editing unless you need to change the
wording or functions. The user does a File > New and selects the template as
the basis for a new document. As the new document opens, the AutoNew macro
shows a userform to collect the information for this particular document,
and then fills in the necessary parts.

The template should contain all four possible prescription blanks. Each
prescription blank should contain its complete set of bookmarks (so blank 1
contains MED1, AMT1, REF1, etc. and blank 2 contains MED2,... ). Also, each
prescription blank should be enclosed by a bookmark (say, BLANK1, BLANK2,
....). [Yes, you can have one or more bookmarks inside another bookmark. The
only requirement is that each bookmark's name is unique.]

When the user starts a new document and the userform appears, one of the
data items should be the number of prescription blanks needed for this
document (use a list box or a match-required combo box to limit the choices
to 1, 2, 3, 4) -- or else you can just have the userform's code count how
many sets of information are filled in when the user clicks OK.

In the OK button's Click() procedure, the data from the userform is
transferred to the appropriate bookmarks in the document, and then any
unused prescription blanks are removed by statements like

For i = NumberOfBlanks + 1 to 4
ActiveDocument.Bookmarks("BLANK" & i).Range.Delete
Next i
 

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