Writing an Auto Fill Macro

Microsoft Word 2003 win32(full prod)
February 25, 2010 at 14:36:13
Specs: Windows XP
I am creating a form in Microsoft Word 2003. There is an area that requests a physical company information, including;
Address:
City, State, Zip:
Phone:
Fax:
Primary Contact:
Email:

I would like to make it so that when a box labeled 'same as physical address' is checked that the information that has been entered will autofill to corresponding fields labeled Billing Information, with the following fields:
Address:
City, State, Zip:
Phone:
Fax:
Primary Contact:
Email:

Can anyone help me with this?


See More: Writing an Auto Fill Macro

Report •

#1
February 26, 2010 at 04:08:55
Hi,

When you fill out the initial company information and address, what identifies where each piece of information is. Is it identified by it's location in a table in Word, or is the information in named fields.

What is the 'same as physical address' box - what type of object is it?

Regards

Regards


Report •

#2
February 26, 2010 at 09:18:19
Thank you for your quick response.

We can name the fields using their ‘bookmarks’. They can be named exactly as their titled (address, citystatezip, fax, etc.) As far as what type of fields they are, they are ‘text form fields’


Report •

#3
February 26, 2010 at 11:24:38
Hi,

My approach would be to name the form fields in the Billing section all starting with Bx
e.g. BxAddress1, BxZip
The form fields in the shipping section have the same names except they start with Sx
SxAddress1, SxZip etc.
(FormFields are named using the Bookmark box during the design phase).

Attach the following code to a button or other element on your form. When clicked it runs the following macro:

Sub AddrCpy()
Dim objField As FormField
Dim strDest As String
'loop through all form fields in the active document
For Each objField In ActiveDocument.FormFields()
    'look for form fields starting with Bx
    If Left(objField.Name, 2) = "Bx" Then
        'create the destination form field address
        strDest = "Sx" & Right(objField.Name, Len(objField.Name) - 2)
        'copy the text
        ActiveDocument.FormFields(strDest).Result = objField.Result
    End If
Next objField
End Sub

This will find each form field with a name starting "Bx" and will copy the text to the equivalent field that starts with "Sx"

You can add more code to ensure that mandatory fields have a minimum of text in them or that State has a recognized name or abbreviation, if not don't copy - just pop-up a message box.

The reason for naming fields starting say "Bx" is that it has to be something that no other form field starts with. For example "Ch" wouldn't work because you might have a "CheckBox1" form field in your document.

Hope this gives you something to work with.

Regards


Report •
Related Solutions


Ask Question