Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help building string from property names array

Status
Not open for further replies.

BugZap13

Programmer
Dec 2, 2013
29
0
1
US
I am trying to build a routing that will process an aray of property names and obtain the property values from an object. If you examine the following code, the routine "BuildString_One" creates a string based on the propery values. I am trying to build the same string in the second routine, "BuildString_Two", using an array of property names instead. How do I evaluate the property names in the array to the underlying property values?

Code:
Private aFields(5) As String

Sub BuildList()
  aFields(0) = “.FullName”
  aFields(1) = “.BusinessAddress”
  aFields(2) = “.BusinessAddressStreet”
  aFields(3) = “.BusinessAddressCity”
  aFields(4) = “.BusinessAddressState”
  aFields(5) = “.BusinessAddressPostalCode”

  Dim oOutlook As Outlook.NameSpace
  Dim oFolder As Outlook.Folder
  Dim oContactItem As Object
  Dim oContact As ContactItem
  
  Set oOutlook = Application.Session
  Set oFolder = oOutlook.GetDefaultFolder(olFolderContacts)
  
  For Each oContactItem In oFolder.Items
    If (oContactItem.Class = olContact) Then
      Set oContact = oContactItem
      BuildString_One oContact
      BuildString_Two oContact
    End If
  Next
End Sub

Sub BuildString_One(oContact As ContactItem)
  Dim cString As String
  With oContact
  cString = .FullName & Chr(9) _
    & .BusinessAddress & Chr(9) _
    & .BusinessAddressStreet & Chr(9) _
    & .BusinessAddressCity & Chr(9) _
    & .BusinessAddressState & Chr(9) _
    & .BusinessAddressPostalCode
  End With
End Sub

Sub BuildString_Two(oContact As ContactItem)
  Dim iIndex As Integer
  Dim cString As String
  With oContact
  For iIndex = 0 To UBound(aFields)
     ' The following is the line needing the help................
    [COLOR=#EF2929]cString = Cstring + (.Afields(iIndex)) & Chr(9)[/color]
  Next
  End With
End Sub
 
It looks like you need CallByName function. See vba help file for details.

combo
 
I think you need to start with:

Private aFields(5) As StringObject
 
Thanx combo. I did not know about the CallByName function. Following is the code that worked for those following along.

Code:
cFieldName = aFields(iIndex) 
cPropertyValue = CallByName(oContact, cFieldName, VbGet)
cString = cString & cPropertyValue & chr(9)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top