MS Office Forum / Word / Programming / March 2006
"Run Macro on Exit" on formfield doesn't fire
|
|
Thread rating:  |
Nat81 - 09 Mar 2006 09:32 GMT 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
Charles Kenyon - 09 Mar 2006 13:08 GMT Is your template protected for forms?
 Signature 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
See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome! --------- --------- --------- --------- --------- --------- 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 everyone > [quoted text clipped - 16 lines] > > Nat Nat81 - 09 Mar 2006 13:16 GMT 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
> 0 Then strField = Selection.Bookmarks(Selection.Bookmarks.Count).Name End If End Sub
Hope this helps clear things up a bit ;-)
Charles Kenyon - 09 Mar 2006 21:47 GMT 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.
 Signature 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
See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome! --------- --------- --------- --------- --------- --------- 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
> 0 Then strField = Selection.Bookmarks(Selection.Bookmarks.Count).Name End If End Sub
Hope this helps clear things up a bit ;-)
Nat81 - 10 Mar 2006 06:13 GMT 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
Cindy M -WordMVP- - 10 Mar 2006 06:55 GMT 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
:-) Nat81 - 10 Mar 2006 07:15 GMT 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
Jean-Guy Marcil - 10 Mar 2006 13:58 GMT Nat81 was telling us: Nat81 nous racontait que :
> Hi Cindy, > [quoted text clipped - 3 lines] > 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?
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Nat81 - 13 Mar 2006 07:12 GMT 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
|
|
|