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
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