Hi JT
Dougs posted a neat version that uses a table in a Word document. But as an
option here's a version that uses a comma delimited text file:
Private Sub btnOK_Click()
Dim ilngCol As Long
Dim lngColumns As Long
Dim strAddress As String
With cboAddress
' The user must have selected something in the ComboBox
If .ListIndex = -1 Then Exit Sub
' Build address string
lngColumns = CLng(.Tag) - 1
For ilngCol = 0 To lngColumns
strAddress = strAddress & .List(.ListIndex, ilngCol)
If ilngCol < lngColumns Then strAddress = strAddress & vbCr
Next
End With
' Do something with strAddress
MsgBox strAddress
End Sub
Private Sub UserForm_Initialize()
LoadAddressFile
End Sub
Public Sub LoadAddressFile()
Const cFileToProcessList As String = "F:\My Templates\Addresses.txt"
Dim hFile As Long
Dim strInput As String
' Allocate next available file number
hFile = FreeFile
' Read the file conataining the Path/File names of the
' Word documents you need to process
Open cFileToProcessList For Input Access Read As hFile
Do Until EOF(hFile)
' Read the path/name of the next Word document to process
Line Input #hFile, strInput
' You've read in the path and file name of
AddToListBox strInput
Loop
Close #hFile
End Sub
Private Sub AddToListBox(ByVal strInfo As String)
Const cDelimiter As String = ","
Dim lngCol As Long
Dim strAddressBit As String
' Parse out the string, each token is separated by a comma
With cboAddress
Do Until LenB(strInfo) = 0
strAddressBit = Snip(strInfo, cDelimiter)
If lngCol = 0 Then
.AddItem strAddressBit
Else
.List(.ListCount - 1, lngCol) = strAddressBit
End If
lngCol = lngCol + 1
Loop
' Set the number of columns so that we can build the
' correct output string when the OK Button is selected
.Tag = lngCol
End With
End Sub
Private Function Snip(ByRef strIn As String, _
ByVal strDelimiter) As String
Dim lngPos As Long
lngPos = InStr(strIn, strDelimiter)
If lngPos = 0 Then
Snip = strIn
strIn = vbNullString
Else
Snip = Left$(strIn, lngPos - 1)
strIn = Mid$(strIn, lngPos + 1)
End If
End Function
Like the Word table it's easy to maintain, but it's also very small. The
above example uses a ComboBox control called "cboAddress". The text file it
uses is called "F:\My Templates\Addresses.txt", just change this to
whatever you want.
The data looks something like this:
Branch A,Address line A,Suburb A,State A Postcode
Branch B,Address line B,Suburb B,State B Postcode
Branch C,Address line C,Suburb C,State C Postcode
Branch D,Address line D,Suburb D,State D Postcode
Branch E,Address line E,Suburb E,State E Postcode
Branch F,Address line F,Suburb F,State F Postcode
HTH + Cheers - Peter