Word 2003 crashes when I click between form fields with macro on e

C

czarnakota

I have a Word template with a few form fields on the first page. One of the
form fields has a macro on exit attached, which updates a bookmark to reflect
any changes in the form field. The bookmark is in a header in a different
section in the document. Everything works absolutely fine as long as I use
TAB to exit the field but when I CLICK on a different field, Word crashes.
Every single time. I have found a discussion about a similar problem here:
http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2007-07/msg00278.html
but in this situation the problem seemed to be that the macro was called
again before it finished running whereas I don't think this is the case in my
code (I might be wrong though, I have spent a really long time trying to
figure this out so maybe I can no longer see things). Also, in my case it
does not seem to be a single line of code that is causing the error - when I
insert a breakpoint and then execute the macro line by line, it executes
fine, but then sometimes Word will crash a few seconds after the macro
finished.

The problem does not seem to occur in the template itself - only in
documents, and only if they have been saved, closed and then reopened.

I thought this had something to do with Application.View.SeekView but a) I
tried to re-write my code so that the view is wdCurrentPageHeader and the
problem still occured, b) the code as it is at the moment works perfectly
fine if I tab through the fields, rather than click.

I would be really grateful for any help as to what the reason for this
problem might be as I have been trying to solve this for a good few weeks now
and am definitely running out of ideas.

The code in question is as follows:

Sub SyncBkmFldClient()
Dim str As String
Dim rng As Range
str = ActiveDocument.FormFields("Client1stPage").Result

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

If ActiveDocument.Bookmarks.Exists("ClientHeader") Then
Set rng = ActiveDocument.Bookmarks("ClientHeader").Range
rng.Text = str
rng.Bookmarks.Add ("ClientHeader")
End If

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub


Thanks
 
M

macropod

Hi czarnakota,

AFAIK, you don't need to unprotect/reprotect the document for the header update to work (it works for me). Try:
Sub SyncBkmFldClient()
Dim str As String, rng As Range, BkMrk As String
With ActiveDocument
BkMrk = "ClientHeader"
If .Bookmarks.Exists(BkMrk) Then
str = .FormFields("Client1stPage").Result
Set rng = .Bookmarks(BkMrk).Range
rng.Text = str
rng.Bookmarks.Add (BkMrk)
Set rng = Nothing
End If
End With
End Sub
 
C

czarnakota

Hi macropod

thanks a lot for your reply - it works! I've tried different versions of
your solution and it seems that the problem was that this line:

str = ActiveDocument.FormFields("Client1stPage").Result

has to be enclosed within the if .bookmarks.exists statement. Do you know why?

As for unprotecting the document - it doesn't work for me. I get the "you
are not allowed to edit this region because document protection is in effect"
message on this line

rng.text = str
 
C

czarnakota

Hello

for some reason the problem started again. It only happens when I click
between fields, not when I tab through them, which suggests that this is not
necessarily a problem with the code itself. I have no idea what else it could
be though and would be very grateful for any suggestions.

macropod said:
Hi czarnakota,

AFAIK, you don't need to unprotect/reprotect the document for the header update to work (it works for me). Try:
Sub SyncBkmFldClient()
Dim str As String, rng As Range, BkMrk As String
With ActiveDocument
BkMrk = "ClientHeader"
If .Bookmarks.Exists(BkMrk) Then
str = .FormFields("Client1stPage").Result
Set rng = .Bookmarks(BkMrk).Range
rng.Text = str
rng.Bookmarks.Add (BkMrk)
Set rng = Nothing
End If
End With
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]


czarnakota said:
I have a Word template with a few form fields on the first page. One of the
form fields has a macro on exit attached, which updates a bookmark to reflect
any changes in the form field. The bookmark is in a header in a different
section in the document. Everything works absolutely fine as long as I use
TAB to exit the field but when I CLICK on a different field, Word crashes.
Every single time. I have found a discussion about a similar problem here:
http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2007-07/msg00278.html
but in this situation the problem seemed to be that the macro was called
again before it finished running whereas I don't think this is the case in my
code (I might be wrong though, I have spent a really long time trying to
figure this out so maybe I can no longer see things). Also, in my case it
does not seem to be a single line of code that is causing the error - when I
insert a breakpoint and then execute the macro line by line, it executes
fine, but then sometimes Word will crash a few seconds after the macro
finished.

The problem does not seem to occur in the template itself - only in
documents, and only if they have been saved, closed and then reopened.

I thought this had something to do with Application.View.SeekView but a) I
tried to re-write my code so that the view is wdCurrentPageHeader and the
problem still occured, b) the code as it is at the moment works perfectly
fine if I tab through the fields, rather than click.

I would be really grateful for any help as to what the reason for this
problem might be as I have been trying to solve this for a good few weeks now
and am definitely running out of ideas.

The code in question is as follows:

Sub SyncBkmFldClient()
Dim str As String
Dim rng As Range
str = ActiveDocument.FormFields("Client1stPage").Result

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

If ActiveDocument.Bookmarks.Exists("ClientHeader") Then
Set rng = ActiveDocument.Bookmarks("ClientHeader").Range
rng.Text = str
rng.Bookmarks.Add ("ClientHeader")
End If

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub


Thanks
 

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