Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro is making form empty

Status
Not open for further replies.

xenofoob

Programmer
Oct 4, 2004
28
NL
Hey,

I've recorded a macro with the use of a form. It is working correctly, with adding one extra new row with textfields when hitting tab at the last field. Over and over again :)

BUT.... the information I've entered in the fields is being cleared!

Is there somekind of commando that doesn't clear my text fields?
 
This is my code:
Code:
Sub autoadd()
'
' autoadd Macro
' Macro opgenomen op 23-11-2004 door E. Holman
'
    ActiveDocument.Unprotect
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    With Selection.FormFields(1)
        .EntryMacro = ""
        .ExitMacro = ""
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdNumberText, Default:="", Format:=""
            .Width = 0
        End With
    End With
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="€ "
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    With Selection.FormFields(1)
        .EntryMacro = ""
        .ExitMacro = "autoadd"
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdNumberText, Default:="", Format:="0,00"
            .Width = 0
        End With
    End With
    ActiveDocument.Protect wdAllowOnlyFormFields
End Sub
 
Hai Xeno, :D

Set NoReset to True (in protect method)
Last line is:
ActiveDocument.Protect wdAllowOnlyFormFields, True

Have Fun!

Groetjes,
Joost Verdaasdonk
 
Dude :) Thanks a lot mate :) it's working.

How do you know this? Is there somekind of tutorial that I can read to gather more information about this? Or did you just read a book?

Another question. Relative simple I think. How do I select the first field after leaving the last field?

tnx
 
Hi Xeno, :D

You're welcome!

Yes a lot off books and a lot more exercise ;-)
A Good book is Steve Romans: Word Macro's . (I think that was his name)

BTW; The best tutorial is F1 (not dissing you) and use Intelissense in the VBA and read wat arguments a method has.

If you need an answer select the Word and hit F1! (Tada...)

If presume Text1 is the first formfield (bookmark in options)

Make a macro:
Sub Test()
ActiveDocument.FormFields("Text1").Select
End Sub

Go to the options and in the outgoing macro select test. Protect the form and lets Go...

Enjoy!

Groetjes,
Joost Verdaasdonk
 
Another thing.... Is there a way to count the values of the third fields of every line? And put this into another field?

Just curious.
 
Well.... my field has no name... Is that no problem? Thanks again ;)
 
Hi Xeno, :D

A bit more information please...

Third fields??...every line?? (what lines?)

Are we talking tables here?

Wel lets see tommorow...have to sleep now! ;-)

Groetjes,
Joost Verdaasdonk
 
Arrr.... can't edit posts.

It doesn't work. When I don't fill in anything the macro crashes. I can fill in a number (tried that). Is there a way to count all the fields and decrease that with 3?
 
One last thing

You'll be a lot better of if you give you're formfields bookmarks names!

This will give easy access and make you're programme thougher!

I've done programticaly (Field add) You could use a counter to increment the field bookmark ore something..but you should use bookmarks..

And now I'm gone....

Groetjes,
Joost Verdaasdonk
 
Allright :) here 's my code now
Code:
Sub autoadd()
'
' autoadd Macro
' Macro opgenomen op 23-11-2004 door E. Holman
'
    
'If BuildKeyCode(wdKeyControl, _
    'wdKeyShift, wdKeyTab) = 777 Then
    'MsgBox "Shift tab ingedrukt", vbCritical
'End If
    
    ActiveDocument.Unprotect
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    With Selection.FormFields(1)
        .EntryMacro = ""
        .ExitMacro = ""
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdNumberText, Default:="", Format:=""
            .Width = 0
        End With
    End With
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="€ "
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    With Selection.FormFields(1)
        .EntryMacro = ""
        .ExitMacro = "autoadd"
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdNumberText, Default:="", Format:="0,00"
            .Width = 0
        End With
    End With
    
    'Count all the input fields
    '
    For Each aField In ActiveDocument.FormFields
        If aField.Type = wdFieldFormTextInput Then Count = Count + 1
    Next aField
    
    'Select the first field of the row
    '
    ActiveDocument.FormFields(Count - 1).Select
    
    ActiveDocument.Protect wdAllowOnlyFormFields, True
End Sub

What it does:
* Adding one table row with three columns containing input fields.
* Selecting the first field of the row

At this moment my macro almost what I wanted but just a vew things:
* Shift tab should not add fields.... only the tab or enter key... I've tried that on the top but doesn't work as I thought :p
* Count the third fields of every row and store the total value into another differend input field

| Cola | 2.00 | €2,50 |
| 7-UP | 6.00 | €3,75 |
| ...........ADD............|

| TOTAL | €6,25 |

Greets,
Xeno
This is what i want to reach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top