Have one Template but two address to put on heading/footing..

J

Jeffery B Paarsa

Hello,

I have a template but two office address. I would like to be able put
either of these addresses by simple selection/check mark and put one of these
addresses on the heading/footing of the document being created from this
template...

Any idea? sample code etc?
 
D

Doug Robbins - Word MVP

You might as well go the whole hog and create a userform in your template
into which you also insert the details of the person to whom the letter is
to be addressed as well as provide the facility for the user to select the
address to be used for the particular letter that they are creating.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

The selection of the address could be done by a number of methods. One
would be to have a couple of radio buttons - one for each address and then
based on the button that was selected, have code assign the required
information for that address to a document variable so that it would be
displayed in the document by the use of a { DOCVARIABLE } field inserted
into the template at the required location.

Another way would be to have a listbox that contained the alternative
addresses and the user would select the one that they want to use from that
list.

This routine loads a listbox with client details stored in a table in a
separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

Private Sub UserForm_Initialize()
Dim sourcedoc As Document, i As Integer, j As Integer, myitem As Range,
m As Long, n As Long
' Modify the path in the following line so that it matches where you
saved Clients.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="c:\Company.doc")
' Get the number or clients = number of rows in the table of client
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of client details
j = sourcedoc.Tables(1).Columns.Count
' Set the number of columns in the Listbox to match
' the number of columns in the table of client details
ListBox1.ColumnCount = j
' Define an array to be loaded with the client data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' Load data into ListBox1
ListBox1.List() = MyArray
' Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub



The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.

To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Gordon Bentley-Mix

Jeff,

The process of inserting one address or the other is simple enough. I would
just use AutoText entries and a bookmark in the appropriate location, and
then just insert the appropriate AutoText based on the user's input. The
exact code for doing this depends on _how_ you expect the user to indicate
their choice.

For example, if you have a UserForm that's displayed when a new document is
created from the template, you could simply add a "Use alternate address"
CheckBox on the UserForm and evaluate the .Value property of this CheckBox to
determine the correct course of action. Something like the following might
work.

Private Sub InsertOfficeAddress()
Dim myRange As Range
Dim OfficeAddressATEntryName As String
If chkUseAlternateAddress.Value = True Then
OfficeAddressATEntryName = "AlternateAddress"
Else
OfficeAddressATEntryName = "RegularAddress"
End If
With ActiveDocument
If .Bookmarks.Exists("OfficeAddress") Then
Dim myRange As Range
Set myRange = .Bookmarks("OfficeAddress").Range
On Error GoTo ATEntryError

..AttachedTemplate.AutoTextEntries(OfficeAddressATEntryName).Insert myRange,
True
End If
End With
Exit Sub

ATEntryError:
MsgBox "The required Office Address AutoText entry could not be found.",
vbCritical, "AutoText Error"
End Sub

This assumes the following:
* A CheckBox on the UserForm called chkUseAlternateAddress
* A bookmark in the document called OfficeAddress
* AutoText entries in template for the regular and alternate addresses
called (imaginatively enough) "RegularAddress" and "AlternateAddress"

Of course this only inserts the AutoText in on location, so you would need
to repeat the code with a different bookmark for any additional locations.
(Because I do this A LOT in some templates, I actually have a "generic"
procedure that accepts arguments for the bookmark and AutoText entry names
and a flag for restoring the bookmark after inserting the AutoText.)

If you don't have a UserForm for input, I'm not quite sure how you would
approach this. I suppose you could adapt the code to work as an OnExit macro
for a form field or put it behind a MacroButton or something. However, you
then have to deal with "cleaning up" the form field / MacroButton afterwards.
You could also look at putting this code behind a simple UserForm that's
displayed through a toolbar button or something.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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