View Single Post
  #12   Report Post  
Posted to microsoft.public.word.docmanagement
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Form refresh code

You are not bugging me

I hadn't appreciated that you required two macros to clear the form in
different ways; however you can so so with one, and for that purpose alone
the form does not require unprotecting:-

Sub ResetForm()
Dim oFld As FormFields
Dim i As Long
Dim sQuery As String
Dim sAdd As String

sQuery = MsgBox("Click Yes to clear the whole form" & vbCr & _
"Click No to leave the address", _
vbYesNoCancel, "Clear Form")
If sQuery = vbCancel Then Exit Sub
Set oFld = ActiveDocument.FormFields
sAdd = oFld("Address").Result
For i = 1 To oFld.Count
oFld(i).Select
If oFld(i).Type wdFieldFormDropDown Then
oFld(i).Result = ""
End If
Next
If sQuery = vbNo Then
oFld("Address").Result = sAdd
End If
End Sub

--

Graham Mayor - Word MVP

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



Steve wrote:
I'm confused with the ResetForm macro and ResetFormFields macro.

Do these run together.

To clear my objective up, i require one button to reset the whole
form and one button to clear the whole form except the address field.
This enables the user to produce multiple quotes without have to
insert the address again.

Sorry if i'm bugging you Graham.

"Graham Mayor" wrote:

The problem appears to be that you are unprotecting the document to
insert the address from Outlook directly into the document, much as
you did earlier with the number, coupled with some anomalies
relating to the ResetForm macro.

Your version of the InserrtAddress macro also unlocked the field
with the password 123 then locked it again without the password. It
is better to define the password as a string as in the earlier
example and insert the string variable name in the protect/unprotect
commands.

It would be better if you inserted a form field - let's call it
'Address' so it matches the Address field that the reset macro
cannot find . You can then insert the address from the macro without
unlocking the form by using the same method as before i.e.

Public Sub InsertAddressFromOutlook()
Dim strCode As String, strAddress As String
Dim iDoubleCR As Integer
strCode = "PR_GIVEN_NAME PR_SURNAME" & vbCr & _
"PR_COMPANY_NAME" & vbCr & _
"PR_POSTAL_ADDRESS" & vbCr
strAddress = Application.GetAddress(AddressProperties:=strCode, _
UseAutoText:=False, DisplaySelectDialog:=1, _
RecentAddressesChoice:=True, UpdateRecentAddresses:=True)
If strAddress = "" Then Exit Sub
iDoubleCR = InStr(strAddress, vbCr & vbCr)
Do While iDoubleCR 0
strAddress = Left(strAddress, iDoubleCR - 1) & _
Mid(strAddress, iDoubleCR + 1)
iDoubleCR = InStr(strAddress, vbCr & vbCr)
Loop
strAddress = Left(strAddress, Len(strAddress) - 1)
ActiveDocument.FormFields("Address").Result = strAddress
End Sub

The ResetForm macro that I posted should replace your ResetForm not
be used in addition to it - and it appears from your listing that it
now has no name?

There remains some confusion over *your* ResetForm macro which
appears to refer to a field that doesn't exist. If it does and has
nothing to do with the InsertAddress function then you would need to
rename the field at the end of the above macro to reflect the form
field name that you wish the address to be inserted into.

I think that should have it covered
--

Graham Mayor - Word MVP

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



Steve wrote:
I get a visual basic warning box with no message.

I can only guess maybe conflict with another macros.

Please see below the macros i'm running:


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("order").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

-------------------------------

Public Sub InsertAddressFromOutlook()
Dim strCode As String, strAddress As String
Dim iDoubleCR As Integer

'Set up the formatting codes in strCode
strCode = "PR_GIVEN_NAME PR_SURNAME" & vbCr & _
"PR_COMPANY_NAME" & vbCr & _
"PR_POSTAL_ADDRESS" & vbCr

'Display the 'Select Name' dialog, which lets the user choose
'a name from their Outlook address book
strAddress = Application.GetAddress(AddressProperties:=strCode, _
UseAutoText:=False, DisplaySelectDialog:=1, _
RecentAddressesChoice:=True, UpdateRecentAddresses:=True)
'If user cancelled out of 'Select Name' dialog, quit
If strAddress = "" Then Exit Sub

'Eliminate blank paragraphs by looking for two carriage returns
in a row iDoubleCR = InStr(strAddress, vbCr & vbCr)
Do While iDoubleCR 0
strAddress = Left(strAddress, iDoubleCR - 1) & _
Mid(strAddress, iDoubleCR + 1)
iDoubleCR = InStr(strAddress, vbCr & vbCr)
Loop

'Strip off final paragraph mark
strAddress = Left(strAddress, Len(strAddress) - 1)
'Insert the modified address at the current insertion point
ActiveDocument.Unprotect Password:="123"
Selection.Range.Text = strAddress
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub

-----------------------------------------

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

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


End Sub


"Graham Mayor" wrote:

In what way does it fail? The macro unlocks the form using your
password 123 resets all the fields except the one called Address,
but ignores any dropdown fields, then relocks the form using your
password? Ther address field content is copied to a variable then
written back to the field.

--

Graham Mayor - Word MVP

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



Steve wrote:
Hi Graham,

The code you gave fails.

The document is protected upon opening.

The code runs when the document is unprotected but resets all
fields.

Regards

Steve

"Graham Mayor" wrote:

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