Clear field with macro

G

Guest

I have a form field (an invoice) with a CommandButton to run a macro wich inserts the incoice number from c:\settings.txt (and saves the new numer)

It´s great, but if I click it several times (to increase the invoice number) it adds the new number before the old, instead of clearing the bookmark and inserting the new number in an empty field.

Ie: If a press the button once, my bookmark says 033. But if I press it twice, is says 034033 instead of 034.

So I´d want a code for clearing the bookmarked field.

Here´s my code (the unprotect and protect is there because I run a form field)

Private Sub CommandButton1_Click()

ActiveDocument.Unprotect

Order = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Order")

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")

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

End Sub

Thanks for your answer!
 
G

Guest

No need to unprotect/re-protect the document. Just use something like:

ActiveDocument..FormFields("Order").Result = Format(Order, "00#") _
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")

This will replace the contents of the form field instead of inserting additional text.

If you are not using a form field because you do not want the data to be modified by the user, you can still use a form field but turn off "fill-in enabled" in the form field properties.
--
Please post all follow-ups to the newsgroup. Email requests for assistance can not be acknowledged.

~~~~~~~
Beth Melton

Word FAQ: http://mvps.org/word
TechTrax: http://mousetrax.com/techtrax/
 
G

Guest

Perfect Beth!

This was the function I was looking for to begin with!

Thank you very much!

P.S. If someone else reads this, the row:
ActiveDocument..FormFields("Order").Result = Format(Order, "00#") _
should not have two dots in a row, it should be:
ActiveDocument.FormFields("Order").Result = Format(Order, "00#") _



"Beth Melton" skrev:
 

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