Re: Form refresh code
- From: "Graham Mayor" <gmayor@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 25 Sep 2008 12:58:44 +0300
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
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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
.
- Follow-Ups:
- Re: Form refresh code
- From: Steve
- Re: Form refresh code
- References:
- Form refresh code
- From: Steve
- Re: Form refresh code
- From: Suzanne S. Barnhill
- Re: Form refresh code
- From: Steve
- Re: Form refresh code
- From: Graham Mayor
- Re: Form refresh code
- From: Steve
- Re: Form refresh code
- From: Graham Mayor
- Re: Form refresh code
- From: Steve
- Re: Form refresh code
- From: Graham Mayor
- Re: Form refresh code
- From: Steve
- Form refresh code
- Prev by Date: visual basic help!! fill out form word PLEASE HELP!!!
- Next by Date: header and footer
- Previous by thread: Re: Form refresh code
- Next by thread: Re: Form refresh code
- Index(es):