Re: Form refresh code

Tech-Archive recommends: Speed Up your PC by fixing your registry



The following will reset all the form fields except for a field with the
bookmark name 'Address'

Sub ResetFormFields()
Dim bProtected As Boolean
Dim oFld As FormFields
Dim i As Long
Dim sAdd As String
Dim sPassword as String
sPassword = "123"
Set oFld = ActiveDocument.FormFields
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=sPassword
End If
'Format all fields as not hidden
sAdd = oFld("Address").Result
For i = 1 To oFld.Count
oFld(i).Select
Selection.Font.Hidden = False
If oFld(i).Type <> wdFieldFormDropDown Then
oFld(i).Result = ""
End If
Next
oFld("Address").Result = sAdd
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=sPassword
End If
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Steve wrote:
Hi Graham,

Thanks for that, it works a treat.

One other thing. I'd like to reset the form but not all of it.
Example the address field remains un-reset.

Sub ResetForm()
'
' ResetForm Macro
'
'
ActiveDocument.Unprotect Password:="123"
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, Password:="123"
ActiveDocument.FormFields("Address").Select


End Sub

"Graham Mayor" wrote:

If you re-run the macro within the open document, as written it will
simply append the new number. To avoid this (and the need to unlock
the form) replace the bookmark with a form field (here shown as
Text1) with the fillin property disabled. The following version of
the macro will then fill that field with the current number every
time the macro is run. Thus you can run it from a toolbar button to
update the number:

Sub AutoNew()
Dim SettingsFile As String
Dim Order As String
SettingsFile = "D:\Profiles\Steven
Perry\Documents\Business\Ainsworth Print &
Design\Templates\Quotation Number.Txt"
Order = System.PrivateProfileString(SettingsFile, "MacroSettings",
"Order") If Order = "" Then
Order = 1
Else
Order = Order + 1
End If
System.PrivateProfileString(SettingsFile, "MacroSettings", "Order")
= Order With ActiveDocument
.FormFields("Text1").Result = format(Order, "200#")
.SaveAs FileName:="D:\Profiles\Steven
Perry\Documents\Business\Master File\Quotation File\Quotation ID_" &
format(Order, "200#")
End With
End Sub

If you prefer your current approach then you are going to have to
recode to replace the content of the bookmark rather than add to it
. See the example at http://www.gmayor.com/word_vba_examples.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Steve wrote:
Hi Suzanne,

Thanks for your reply.

Here is my AutoNew macro. Is there something i need to add to make
this work?

Sub AutoNew()

Order = System.PrivateProfileString("D:\Profiles\Steven
Perry\Documents\Business\Ainsworth Print &
Design\Templates\Quotation Number.Txt", _
"MacroSettings", "Order")

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

System.PrivateProfileString("D:\Profiles\Steven
Perry\Documents\Business\Ainsworth Print &
Design\Templates\Quotation Number.Txt", "MacroSettings", _
"Order") = Order
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order,
"200#") ActiveDocument.SaveAs FileName:="D:\Profiles\Steven
Perry\Documents\Business\Master File\Quotation File\Quotation ID_" &
Format(Order, "200#")
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub

"Suzanne S. Barnhill" wrote:

If the form is created as a template, and if your UserForms (or
whatever they are) are launched by an AutoNew macro, then every
time a new document is created based on the template, your macros
will run. There is no need to close and restart Word; just create
a new document.

Alternatively, you can choose to either hide a UserForm or close
it; in the first instance, it will retain previous entries.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

"Steve" <Steve@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:4AE17389-CF7D-4FBF-A03C-DE703D237EA0@xxxxxxxxxxxxxxxx
Hi there,

I've created a form running various macro codes. One example of
code would be a quotation reference number which updates when the
macro template is fired.

As there are bulk quotations to produce, i'd like the user to
avoid closing
word and restarting just in order to refresh.

I'd like code for a button that would act as if the document were
just fired.

Also a button that may leave a certain field un-refreshed. For
example, several quotes to be produced for the same company where
the company name and
address field would remane un-refreshed.

Many thanks

Steve


.


Quantcast