PDA

View Full Version : How do I trigger a macro to run when the user clicks in a bookmarked area?



kcguy
07-24-2009, 05:29 PM
I have a document that must have a few lines of identical information (Project date, project location, project number) at the top of the first page and the bottom of the last page of a document. I created a form that asks for all of the info. When the user clicks OK, the info gets dumped into the document by using bookmarks.

Selection.GoTo What:=wdGoToBookmark, Name:="HeaderInfo"

Selection.TypeText _
Format(Now, "mmmm d, yyyy") & Chr$(13) & Chr$(13) & _
txtCName.Text & Chr$(13) & _
txtCTitle.Text & Chr$(13) & _
txtCFirm.Text & Chr$(13) & _
txtCAddress.Text & Chr$(13) & _
...and so on...

I do the same type of thing for "FooterInfo".

Now the catch is if someone were to change the information in the headerinfo bookmarked area, I need the text in the footerinfo area to change as well. In Excel, I'd just dump some copy commands into worksheet.selectionchange, but of course word doesn't have that option.

I can figure out how to select what I want to copy from the header area if something is changed. I just can't find a way to make the macro that does the copying to trigger if something in that area is changed. Any ideas?

macropod
07-25-2009, 08:28 PM
Hi kcguy,

Your code doesn't actually insert anything into the bookmarks - it replaces them with the output string. Consequently, there's nothing to cross-reference between the first & last pages. In any event, Word doen't have a something like a bookmark_change event you can hookinto.

If you use a 'different first page' layout, you can put the info for the first page into the header without worrying about bookmarks. You also don't need to use 'Selection' - 'Range' objects are all you need. For example:
Sub Test()
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text = "Hello"
End Sub

If the paragraph for the range to insert the first page's text has a unique Style name applied to it, you also don't need to explicitly write anything to the last page - all you need is a StyleRef field pointing to the Style you've used for the info on the first page.

If you're actually using Word's footer on the last page, you could use a field code to supress the output on the previous pages. A suitable field code would be:
{IF{PAGE}= {NUMPAGES} {STYLEREF HeaderInfo}}
where 'HeaderInfo' is the name of the Style you've applied to the para used to hold the info on the first page.

fumei
07-27-2009, 08:36 AM
Well phrased, and a ringing endorsement of why it is a good idea to use styles.

"Now the catch is if someone were to change the information in the headerinfo bookmarked area, I need the text in the footerinfo area to change as well. In Excel, I'd just dump some copy commands into worksheet.selectionchange, but of course word doesn't have that option."

Correct, Word does not have that "option". That is because Word is a word-processor, Excel is a spreadsheet. They are structurally different.

As macropod mentions, your code does not, in fact, retain the bookmark. Yes, the text you use with Selection is inserted at what WAS the bookmark "HeaderInfo", but that bookmark will no longer exist.

Even if it did - and there is simple code that will change the text within a bookmark, but retain the bookmark - if someone actually puts the cursor (the selection) into text within a bookmark and changes something, there is NO automated procedure that can detect that.

Word is a word-processor. Changing text is what it does. So the fact text is changed does not, in itself, cause an event to fire.

Could you write a procedure that checks these conditions (If "HeaderInfo" <> X (previously stored data) Then do something)?

Yes. But you would have to actually determine how and when you would fire such a procedure.

Paul_Hossler
07-29-2009, 07:39 AM
You could try

1. Inserting a REF field in the footer that pulls the HeaderInfo bookmark
2. Using the code from MVPS to update the bookmark text, instead of replacing the bookmark with the text

Paul


'http:word.mvps.org/faqs/MacrosVBA/WorkWithBookmarks.htm
'(1) Placeholder Bookmarks
'If you click somewhere in the document and insert a bookmark
' it will look like a beam I
' – this is a “placeholder” bookmark.
'(2) Enclosing Bookmarks
'Now, if you select some text and insert a bookmark it will look
'like the selected text is enclosed
' in square brackets ie: [selected text] – this is an “enclosing” bookmark.
'If the bookmark is an enclosing bookmark, it will be deleted, and the
' inserted text will appear in it's place.
'If the bookmark is a placeholder bookmark, the text will be inserted
'after the bookmark
'With both these methods, if the bookmark is a placeholder bookmark,
' the text will be inserted after the bookmark:
' ActiveDocument.Bookmarks("myBookmark").Range.InsertBefore "Inserted Text"
' ActiveDocument.Bookmarks("myBookmark").Range.InsertAfter "Inserted Text"
'In order to retrieve the text in a bookmark, the bookmark needs to be
' an enclosing bookmark. Then you can
'use the following to retrieve the text from the bookmark:
' strBookmark = ActiveDocument.Bookmarks("myBookmark").Range.Text
'The best way to insert text at a bookmark without losing the bookmark –
' which works reliably whether
' or not the bookmark currently encloses any text - is to set a
' range variable to the bookmark's range, as follows:
' Dim BMRange As Range
' 'Identify current Bookmark range and insert text
' Set BMRange = ActiveDocument.Bookmarks("MyBookmark").Range
' BMRange.Text = "Hello world"
' 'Re-insert the bookmark
' ActiveDocument.Bookmarks.Add "MyBookmark", BMRange

macropod
07-29-2009, 05:33 PM
Hi Paul,

A good reason for NOT using bookmarks in this case is the fact that users are able to edit whatever's in the bookmark. The risk that comes with such editing is that it's all too easy for the user to delete the bookmark whilst editing. If that happens, the cross-reference will fail. OTOH, if you use a paragraph Style, the underlying paragraph is much harder for the the clumsy user to delete - especially if that Style is formatted to repeat(rather than creating a paragraph in, say, the 'Normal' Style) when the Enter key is pressed.

Paul_Hossler
07-29-2009, 06:08 PM
All true. I've deleted BM's many, many, ... times, when all I wanted to do was update the text.

Paul