Bookmarks and Headers

D

David_Loughry

Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel I have a bookmark in the header of a word document. I am trying to write some applescript to update the contents of the bookmark (which automatically deletes the bookmark) and then re-create the bookmark so that I am able to run the script multiple times on the one document.

My problem is that I have been unable to find a way of creating a bookmark in the header.

I have found the following code which is meant to replace the content of a bookmark and then re-create it. The code works fine within the main body of the document but I am unable to get it to work in the header.

Any suggestions?

=====================

tell application "Microsoft Word"

set bmRange to text object of bookmark "myBookmark" of active document

set bmStart to bmRange's start of content

set content of bmRange to "Inserted Text"

set bmEnd to bmStart + (count "Inserted Text")

make new bookmark at active document with properties {name:"myBookmark", start of bookmark:bmStart, end of bookmark:bmEnd}

end tell
 
J

John McGhie

Hi David:

That's because a Bookmark CANNOT occur in a running header :)

A bookmark must be on a "page". A running header is on multiple pages, so
the bookmark field cannot function.

Tell us what you are trying to do and we'll suggest a better method. I
suspect you are looking for a StyleREF field or a DocumentProperty field.

Cheers

Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel
I have a bookmark in the header of a word document. I am trying to write some
applescript to update the contents of the bookmark (which automatically
deletes the bookmark) and then re-create the bookmark so that I am able to run
the script multiple times on the one document.

My problem is that I have been unable to find a way of creating a bookmark in
the header.

I have found the following code which is meant to replace the content of a
bookmark and then re-create it. The code works fine within the main body of
the document but I am unable to get it to work in the header.

Any suggestions?

=====================

tell application "Microsoft Word"

set bmRange to text object of bookmark "myBookmark" of active document

set bmStart to bmRange's start of content

set content of bmRange to "Inserted Text"

set bmEnd to bmStart + (count "Inserted Text")

make new bookmark at active document with properties {name:"myBookmark",
start of bookmark:bmStart, end of bookmark:bmEnd}

end tell


--

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410
+61 4 1209 1410, mailto:[email protected]
 
P

Peter Jamieson

1. You could have told us that you had lifted your code from

http://www.mactech.com/vba-transition-guide/index-041.html

or perhaps some intermediary. Or perhaps /they/ lifted their code from
elsewhere?

2. That said, from the Word 2004/VBA point of view, there's actually no
problem doing what you want to do as far as the product or the object
model is concerned - for example, in VBA you can do

Sub replacemybm1()
Dim rng As Word.Range
For Each rng In ActiveDocument.StoryRanges
If rng.Bookmarks.Count > 0 Then
Dim lngStart As Long
lngStart = rng.Bookmarks(1).Start
rng.Bookmarks(1).Range.Text = "replacement text"
Dim lngEnd As Long
lngEnd = lngStart + Len("replacement text")
rng.SetRange lngStart, lngEnd
rng.Bookmarks.Add "myBookmark", rng
End If
Next
End Sub

or

Sub replacemybm2()
Dim rng As Word.Range
Set rng = ActiveDocument.StoryRanges(7)
Dim lngStart As Long
lngStart = rng.Bookmarks(1).Start
rng.Bookmarks(1).Range.Text = "replacement text"
Dim lngEnd As Long
lngEnd = lngStart + Len("replacement text")
rng.SetRange lngStart, lngEnd
rng.Bookmarks.Add "myBookmark", rng
Set rng = Nothing
End Sub

The question is whether Applescript+the Word Applescript dictionary lets
you do what you want. Not being an Applescript person, the best I can do
is have a further look.

Peter Jamieson

http://tips.pjmsn.me.uk
 
D

David_Loughry

John, thanks for your response. The problem that I am trying to solve is the following:

(1) We are using a document management system to create and store all of our Word documents in
(2) When users create a new document within the document management system, the document is automatically downloaded to the users Documents folder and opened for editing
(3) The document filename contains the document id, for example, 3637383_SampleDocument.docx
(4) There is a requirement to create a script that automatically populates the Document ID into the Footer of the document
(5) This script needs to be run multiple times. This is because a document can be created based on an existing document. The script will have already run on the existing document and we need to run it again so that the Document ID is updated to the new document.
(6) The Document ID is not the only thing we store in the footer of the document. We also store the Company Name, Date and Page Details.

As an aside, we also need to automatically populate data within the body of the document. We use the Document ID to call a Doucment Management System URL that returns us, in XML form, some properties of the document (ie client name, matter (job) name, author etc). We then find relevant bookmarks within the document body and replace them with the relevant data from the URL. We re-create the bookmarks so that the script can be re-run.

Peter, thankyou for taking the time to look at this post.
 
P

Peter Jamieson

John's suggestion to use document properties (and DOCPROPERTY fields to
display them) seems likely to fit the bill, as long as the cost of
dealing with this legacy is not too high:
(5) This script needs to be run multiple times. This is because a
document can be created based on an existing document.

Peter Jamieson

http://tips.pjmsn.me.uk
 
J

John McGhie

Hi David:

Sounds like you're using something like ProjectWise.

There should be a module available in the document management system that
automatically populates the Document Properties (the metadata stored in the
document) with the information you want. There certainly is in ProjectWise,
and in Documentum.

Then you merely have to insert the document properties as fields in the text
and the information will populate automatically each time the document is
retrieved.

That's how we did it in RioTinto (ProjectWise, Documentum) and in
Commonwealth Bank (Documentum).

Most of the document properties you will need already exist in the Built-In
Document Properties (although some are not instantiated until you populate
them). But you can create as many more as you wish.

They key point I am making is that you can then avoid using bookmarks: you
simply insert "fields" and Word will automatically populate them for you.

Don't forget to "Update Fields" before saving the document :)

I do wish these document management system vendors would take a look at how
Word actually operates before creating their vapourware that adds massive
costs to the implementation because they missed the point :)

Hope this helps


John, thanks for your response. The problem that I am trying to solve is the
following:

(1) We are using a document management system to create and store all of our
Word documents in
(2) When users create a new document within the document management system,
the document is automatically downloaded to the users Documents folder and
opened for editing
(3) The document filename contains the document id, for example,
3637383_SampleDocument.docx
(4) There is a requirement to create a script that automatically populates the
Document ID into the Footer of the document
(5) This script needs to be run multiple times. This is because a document can
be created based on an existing document. The script will have already run on
the existing document and we need to run it again so that the Document ID is
updated to the new document.
(6) The Document ID is not the only thing we store in the footer of the
document. We also store the Company Name, Date and Page Details.

As an aside, we also need to automatically populate data within the body of
the document. We use the Document ID to call a Doucment Management System URL
that returns us, in XML form, some properties of the document (ie client name,
matter (job) name, author etc). We then find relevant bookmarks within the
document body and replace them with the relevant data from the URL. We
re-create the bookmarks so that the script can be re-run.

Peter, thankyou for taking the time to look at this post.

--

The email below is my business email -- Please do not email me about forum
matters unless I ask you to; or unless you intend to pay!

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410
+61 4 1209 1410, mailto:[email protected]
 

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