"Run Macro on Exit" on formfield doesn't fire

N

Nat81

Hi everyone

I searched the groups for someone with the same issue - no success. So
I'm trying it with a new subject. Here's my problem:

I am working with word2000 and I have created a word template (.dot)
with a few formfields in it. The dot has a module containing two public
routines (pCalculateTotals and pGetField). The pGetfield is added in
the "Run Macro on Enter", the pCalculateTotals in the "Run Macro on
Exit"-Event of the formtextfield.

Now I've tried this in a simple doc-File. Everything works fine. But
when I create a doc out of the dot (by doubleclicking on the dot), the
events won't fire. Why is that?

Any help is much appreciated!

Thanks a lot for your answers in advance!!

Nat
 
C

Charles Kenyon

Is your template protected for forms?
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
N

Nat81

Hi Charles,

Yes it is. Here's what I do via Code:

'************* Code in ThisDocument **************
Private Sub Document_New()
'* protect document
ActiveDocument.Sections(1).ProtectedForForms = False
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = False
ActiveDocument.Protect Password:="", NoReset:=False,
Type:=wdAllowOnlyFormFields
'* set cursor
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzWegleitung"
End Sub

'******************** Code in Module

Public strField As String

Sub pCalculateTotals()
Dim varTotal As Variant
On Error Resume Next
Select Case strField
Case "txtAnzWegleitung"
ActiveDocument.FormFields.Item("txtCHFWegleitung").Result =
IIf(ActiveDocument.FormFields("txtAnzWegleitung").Result = "", 0,
ActiveDocument.FormFields("txtAnzWegleitung").Result)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzErgänzung"
Case "txtAnzErgänzung"
ActiveDocument.FormFields.Item("txtCHFErgänzung").Result =
IIf(ActiveDocument.FormFields("txtAnzErgänzung").Result = "", 0,
ActiveDocument.FormFields("txtAnzErgänzung").Result)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzLohnausw"
Case "txtAnzLohnausw"
ActiveDocument.FormFields.Item("txtCHFLohn").Result = 0
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzWertschr"
Case "txtAnzWertschr"
ActiveDocument.FormFields.Item("txtCHFWertschriften").Result =
IIf(ActiveDocument.FormFields("txtAnzWertschr").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzWertschr").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzSchuldenver"
Case "txtAnzSchuldenver"
ActiveDocument.FormFields.Item("txtCHFSchuldenverz").Result =
IIf(ActiveDocument.FormFields("txtAnzSchuldenver").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzSchuldenver").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzDA"
Case "txtAnzDA"
ActiveDocument.FormFields.Item("txtCHFDA").Result =
IIf(ActiveDocument.FormFields("txtAnzDA").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzDA").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAntrag"
Case "txtAntrag"
ActiveDocument.FormFields.Item("txtCHFAntrag").Result =
IIf(ActiveDocument.FormFields("txtAntrag").Result & "" = "", 0,
ActiveDocument.FormFields("txtAntrag").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzFragebogen"
Case "txtAnzFragebogen"
ActiveDocument.FormFields.Item("txtCHFFragebogen").Result =
IIf(ActiveDocument.FormFields("txtAnzFragebogen").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzFragebogen").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzSteuergesetz"
Case "txtAnzSteuergesetz"
ActiveDocument.FormFields.Item("txtCHFSteuergesetz").Result =
IIf(ActiveDocument.FormFields("txtAnzSteuergesetz").Result & "" = "",
0, ActiveDocument.FormFields("txtAnzSteuergesetz").Result * 25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzSteuertarif"
Case "txtAnzSteuertarif"
ActiveDocument.FormFields.Item("txtCHFSteuertarif").Result =
IIf(ActiveDocument.FormFields("txtAnzSteuertarif").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzSteuertarif").Result * 10)
Selection.GoTo What:=wdGoToBookmark, Name:="txtKursliste"
Case "txtKursliste"
ActiveDocument.FormFields.Item("txtCHFKursliste").Result =
IIf(ActiveDocument.FormFields("txtKursliste").Result & "" = "", 0,
ActiveDocument.FormFields("txtKursliste").Result * 14)
Selection.GoTo What:=wdGoToBookmark, Name:="txtKurslisteHB"
Case "txtKurslisteHB"
ActiveDocument.FormFields.Item("txtCHFKurslisteHB").Result =
IIf(ActiveDocument.FormFields("txtKurslisteHB").Result & "" = "", 0,
ActiveDocument.FormFields("txtKurslisteHB").Result * 14)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzTelefonverz"
Case "txtAnzTelefonverz"
ActiveDocument.FormFields.Item("txtCHFTelefonverz").Result =
IIf(ActiveDocument.FormFields("txtAnzTelefonverz").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzTelefonverz").Result * 6)
Selection.GoTo What:=wdGoToBookmark, Name:="txtSonstiges"
Case "txtCHFSonstiges"
Selection.GoTo What:=wdGoToBookmark, Name:="txtAufragErledigt"
End Select

ActiveDocument.FormFields.Item("txtCHFTotal").Result = 0
ActiveDocument.FormFields.Item("txtCHFTotal").Result = _
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFWegleitung").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFWegleitung").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFErgänzung").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFErgänzung").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFLohn").Result = "",
0, ActiveDocument.FormFields.Item("txtCHFLohn").Result)) + _

CDec(IIf(ActiveDocument.FormFields.Item("txtCHFWertschriften").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFWertschriften").Result)) +
_

CDec(IIf(ActiveDocument.FormFields.Item("txtCHFSchuldenverz").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFSchuldenverz").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFDA").Result = "", 0,
ActiveDocument.FormFields.Item("txtCHFDA").Result)) + _
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFAntrag").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFAntrag").Result)) + _
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFFragebogen").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFFragebogen").Result)) +
_

CDec(IIf(ActiveDocument.FormFields.Item("txtCHFSteuergesetz").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFSteuergesetz").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFSteuertarif").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFSteuertarif").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFKursliste").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFKursliste").Result)) + _
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFKurslisteHB").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFKurslisteHB").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFTelefonverz").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFTelefonverz").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFSonstiges").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFSonstiges").Result))
End Sub

Sub pGetField()
If Selection.FormFields.Count = 1 Then
strField = Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count
strField = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If
End Sub

Hope this helps clear things up a bit ;-)
 
C

Charles Kenyon

Sorry, waaaaay to complex for me.

You have your first section unprotected, your second section protected.

The problem you are experiencing is that in a document based on your
template your on exit macro doesn't fire. How are you exiting the formfield?

For debugging purposes, try substituting some simple message boxes for your
detailed code. I am trying to find out what you expect to fire, and what
happens instead.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.


Hi Charles,

Yes it is. Here's what I do via Code:

'************* Code in ThisDocument **************
Private Sub Document_New()
'* protect document
ActiveDocument.Sections(1).ProtectedForForms = False
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = False
ActiveDocument.Protect Password:="", NoReset:=False,
Type:=wdAllowOnlyFormFields
'* set cursor
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzWegleitung"
End Sub

'******************** Code in Module

Public strField As String

Sub pCalculateTotals()
Dim varTotal As Variant
On Error Resume Next
Select Case strField
Case "txtAnzWegleitung"
ActiveDocument.FormFields.Item("txtCHFWegleitung").Result =
IIf(ActiveDocument.FormFields("txtAnzWegleitung").Result = "", 0,
ActiveDocument.FormFields("txtAnzWegleitung").Result)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzErgänzung"
Case "txtAnzErgänzung"
ActiveDocument.FormFields.Item("txtCHFErgänzung").Result =
IIf(ActiveDocument.FormFields("txtAnzErgänzung").Result = "", 0,
ActiveDocument.FormFields("txtAnzErgänzung").Result)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzLohnausw"
Case "txtAnzLohnausw"
ActiveDocument.FormFields.Item("txtCHFLohn").Result = 0
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzWertschr"
Case "txtAnzWertschr"
ActiveDocument.FormFields.Item("txtCHFWertschriften").Result =
IIf(ActiveDocument.FormFields("txtAnzWertschr").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzWertschr").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzSchuldenver"
Case "txtAnzSchuldenver"
ActiveDocument.FormFields.Item("txtCHFSchuldenverz").Result =
IIf(ActiveDocument.FormFields("txtAnzSchuldenver").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzSchuldenver").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzDA"
Case "txtAnzDA"
ActiveDocument.FormFields.Item("txtCHFDA").Result =
IIf(ActiveDocument.FormFields("txtAnzDA").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzDA").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAntrag"
Case "txtAntrag"
ActiveDocument.FormFields.Item("txtCHFAntrag").Result =
IIf(ActiveDocument.FormFields("txtAntrag").Result & "" = "", 0,
ActiveDocument.FormFields("txtAntrag").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzFragebogen"
Case "txtAnzFragebogen"
ActiveDocument.FormFields.Item("txtCHFFragebogen").Result =
IIf(ActiveDocument.FormFields("txtAnzFragebogen").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzFragebogen").Result * 0.25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzSteuergesetz"
Case "txtAnzSteuergesetz"
ActiveDocument.FormFields.Item("txtCHFSteuergesetz").Result =
IIf(ActiveDocument.FormFields("txtAnzSteuergesetz").Result & "" = "",
0, ActiveDocument.FormFields("txtAnzSteuergesetz").Result * 25)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzSteuertarif"
Case "txtAnzSteuertarif"
ActiveDocument.FormFields.Item("txtCHFSteuertarif").Result =
IIf(ActiveDocument.FormFields("txtAnzSteuertarif").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzSteuertarif").Result * 10)
Selection.GoTo What:=wdGoToBookmark, Name:="txtKursliste"
Case "txtKursliste"
ActiveDocument.FormFields.Item("txtCHFKursliste").Result =
IIf(ActiveDocument.FormFields("txtKursliste").Result & "" = "", 0,
ActiveDocument.FormFields("txtKursliste").Result * 14)
Selection.GoTo What:=wdGoToBookmark, Name:="txtKurslisteHB"
Case "txtKurslisteHB"
ActiveDocument.FormFields.Item("txtCHFKurslisteHB").Result =
IIf(ActiveDocument.FormFields("txtKurslisteHB").Result & "" = "", 0,
ActiveDocument.FormFields("txtKurslisteHB").Result * 14)
Selection.GoTo What:=wdGoToBookmark, Name:="txtAnzTelefonverz"
Case "txtAnzTelefonverz"
ActiveDocument.FormFields.Item("txtCHFTelefonverz").Result =
IIf(ActiveDocument.FormFields("txtAnzTelefonverz").Result & "" = "", 0,
ActiveDocument.FormFields("txtAnzTelefonverz").Result * 6)
Selection.GoTo What:=wdGoToBookmark, Name:="txtSonstiges"
Case "txtCHFSonstiges"
Selection.GoTo What:=wdGoToBookmark, Name:="txtAufragErledigt"
End Select

ActiveDocument.FormFields.Item("txtCHFTotal").Result = 0
ActiveDocument.FormFields.Item("txtCHFTotal").Result = _
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFWegleitung").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFWegleitung").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFErgänzung").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFErgänzung").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFLohn").Result = "",
0, ActiveDocument.FormFields.Item("txtCHFLohn").Result)) + _

CDec(IIf(ActiveDocument.FormFields.Item("txtCHFWertschriften").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFWertschriften").Result)) +
_

CDec(IIf(ActiveDocument.FormFields.Item("txtCHFSchuldenverz").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFSchuldenverz").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFDA").Result = "", 0,
ActiveDocument.FormFields.Item("txtCHFDA").Result)) + _
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFAntrag").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFAntrag").Result)) + _
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFFragebogen").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFFragebogen").Result)) +
_

CDec(IIf(ActiveDocument.FormFields.Item("txtCHFSteuergesetz").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFSteuergesetz").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFSteuertarif").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFSteuertarif").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFKursliste").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFKursliste").Result)) + _
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFKurslisteHB").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFKurslisteHB").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFTelefonverz").Result
= "", 0, ActiveDocument.FormFields.Item("txtCHFTelefonverz").Result)) +
_
CDec(IIf(ActiveDocument.FormFields.Item("txtCHFSonstiges").Result =
"", 0, ActiveDocument.FormFields.Item("txtCHFSonstiges").Result))
End Sub

Sub pGetField()
If Selection.FormFields.Count = 1 Then
strField = Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count
strField = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If
End Sub

Hope this helps clear things up a bit ;-)
 
N

Nat81

Hi Charles
Sorry, I shouldn't have posted all the code, I think that's not
important. The important thing is, that it is not fired. Basically what
I am trying to do is the following:
I have a table with two columns of formfields. In the first column you
enter the number of items you would like to have. In the second column
the price should be calculated automatically on exiting the first
column. Example:

1. Column 2. Column
----------------------------------------
[field1] apples [field2] $
[field1] bananas [field2] $
------------------------------------------
Total [field3] $

So what I'm trying to do is, if I enter a value in the fields1 and exit
those fields the fields2 and field3 (which contains the whole total)
are calculated automatically.
I am exiting the formfield either by pressing tab or by clicking into
the next field. I've already tried all kinds of code (with
messageboxes, debugging prints etc.) It doesn't matter which kind of
code there is, it won't be fired. What I found out is, that normally in
the properties of the formfield there are those two dropdowns "macro on
exit" and "macro on enter". In there all the functions are listed.
Usually my functions should be listed as follows: "modulname.function".
But my functions are only listed as "function". May be it's got
something to do with that.

Thanks for your help again.

Nati
 
C

Cindy M -WordMVP-

<#[email protected]>
<[email protected]>
Newsgroups: microsoft.public.word.vba.general
NNTP-Posting-Host: 50.92.3.213.cust.bluewin.ch 213.3.92.50
Path: number1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!meganewsservers.com!feeder2.on.meganewsservers.com!nx02.iad01.newshosting.com!newshosting.com!newsfeed.news2me.com!newshub.sdsu.edu!msrtrans!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Lines: 1
Xref: number1.nntp.dca.giganews.com microsoft.public.word.vba.general:82374

Hi Nat81,
Yes it is. Here's what I do via Code:
Try taking On Error Resume Next out of the code and see
what any suppressed error messages are telling you...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
N

Nat81

Hi Cindy,

Thanks a lot for your suggestion, but its not working, either. The
macro is not even fired. I don't know what the problem ist. If I look
in the dropdown "macro on exit" the function is not listed anymore, so
how would the formfield know what to do, if the function doesn't exists
anymore ;-)

Thanks again
Nat
 
J

Jean-Guy Marcil

Nat81 was telling us:
Nat81 nous racontait que :
Hi Cindy,

Thanks a lot for your suggestion, but its not working, either. The
macro is not even fired. I don't know what the problem ist. If I look
in the dropdown "macro on exit" the function is not listed anymore, so
how would the formfield know what to do, if the function doesn't
exists anymore ;-)

Do you have a statement that makes the whole module Private?
(Something like: Option Private Module)

Also, I have not examined your code in details, but it seems that all it
does is calculate.
So, it seems that you do not even need a macro after all. Why not use a
Formula field instead?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
N

Nat81

Hi Jean-Guy

Thanks for your answer. No, there's no statement that makes the module
private. All I have in the module is a global variable and two public
procedures.

You're right, it only does calculate. Thanks for your suggestion with
the formula field. I've never used that (and didn't know that this
exists ;-), so I'll give it a shot. But I still would like to know why
my formfields are not working. So I'm still open to any suggestions. :D

Thanks a lot again.

Nat
 

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