' Look up a name and address and insert it into the current document at the
' current insertion point
'
Public Sub InsertNameFromList()
Const STYLE_A as string = "Normal NoLead" 'Lines other than
last: 0 before, 0 after
Const STYLE_B as string = "Normal" 'Last line:
0 before, 6 after
Dim pText As String
Dim pLayout As String
Dim pData() As String
Dim pIndex As Long
'Define the required format
pLayout = "<PR_GIVEN_NAME> <PR_SURNAME>" & vbCr & _
"<PR_COMPANY_NAME>" & vbCr & _
"<PR_STREET_ADDRESS>" & vbCr & _
"<PR_LOCALITY>" & " " & "<PR_STATE_OR_PROVINCE>" & " " &
"<PR_POSTAL_CODE>"
'Fetch the address in the required format
pText = Application.GetAddress(AddressProperties:=pLayout)
'Insert into document unless not found or cancelled
If Len(pText) > 0 Then
'Split into lines
pData = Split(pText, vbCr)
'Insert the name: style A
Selection.Style = ActiveDocument.Styles(STYLE_A)
Selection.TypeText Text:=pData(0)
Selection.TypeParagraph
'Loop through the remaining lines
For pIndex = 1 To UBound(pData)
'Last line: Style_B, no trailing paragraph
If pIndex = UBound(pData) Then
Selection.Style = ActiveDocument.Styles(STYLE_B)
Selection.TypeText Text:=pData(pIndex)
'Not last line: Style_A inherited from previous line, add
trailing paragraph
Else
Selection.TypeText Text:=pData(pIndex)
Selection.TypeParagraph
End If
Next
End If
End Sub