How to set field name and get field value through app.config file?

06-09-2011 07:00 PM
New Contributor
I have code below to pull field value from a feature selection onto a word document template. Upon feature selection, the code below will loop through each field and I can pull the field value based of hard coding the field name.

Instead of hard coding the field names, I would like to go through config file, where I can set the field names in config file like adding a key = FID and retrieve the value. All I am doing is to read the field value from config file than hard coding.

How can this be done? Please help me.

Here is the code below by hard coding the field name:

Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)

Dim pMxdoc As IMxDocument
Dim pActiveView As IActiveView
Dim pEnumLayer As IEnumLayer
Dim pLayer As ILayer
Dim pFlayer As IFeatureLayer
Dim pFeatureSelection As IFeatureSelection
Dim pSelectionSet As ISelectionSet
Dim pEnumFeat As IEnumFeature
Dim pEnumFeatSet As IEnumFeatureSetup
Dim pFeat As IFeature
Dim A As Double
Dim B As String
Dim B1 As String

Dim pFeatSel As IFeatureSelection
Dim pMap As IMap
Dim pfeatureclass As IFeatureClass
Dim fields As IFields
Dim C As Long
Dim ii As Long
Dim Field As IField
Dim intFldIndex As Integer
Dim fieldname As String
Dim fieldvalue As Integer
Dim value As String
Dim namelist As String
Dim wdApp As Microsoft.Office.Interop.Word.Application
Dim mydoc As Microsoft.Office.Interop.Word.Document
Dim mywdRange As Microsoft.Office.Interop.Word.Range

pMxdoc = m_application.Document
pActiveView = pMxdoc.FocusMap
pMap = pMxdoc.FocusMap
pFlayer = pMxdoc.SelectedLayer
pfeatureclass = pFlayer.FeatureClass
fields = pfeatureclass.Fields
pFeatureSelection = pFlayer
pSelectionSet = pFeatureSelection.SelectionSet

' Verify that there is at least 1 layer in the TOC
If pMap.LayerCount < 1 Then
MsgBox("Must have at least 1 layer in your map.")
Exit Sub
End If

' verify the name of the selected layer
Dim count As Integer
count = pSelectionSet.Count
If pSelectionSet.Count <> 0 Then
MsgBox("You have have selected: " & pFlayer.Name)
End If

' get the field count
C = fields.FieldCount
MsgBox("There are: " & C & "fields in this layer")

pEnumFeat = pMxdoc.FocusMap.FeatureSelection
'pFeat = pEnumFeat.Next

pEnumFeatSet = pEnumFeat
pEnumFeatSet.AllFields = True
pFeat = pEnumFeat.Next

' loop through each field and add the field name to a list
For ii = 0 To fields.FieldCount - 1
Field = fields.Field(ii)
fieldname = Field.Name ' this gives the field name
value = pFeat.Value(ii).ToString ' this gives the field value
namelist = namelist & fieldname & value & Chr(13)

' diaplay the list of field names in a messagebox
MsgBox(namelist, , "field names and value")

' loop through to get value of FID     //@@@@@ I need help here  @@@@@@@
Do While (Not pFeat Is Nothing)
A = pFeat.Value(pFeat.Fields.FindField("UFO_ID")) // @@@@ want to set this in config file
B = pFeat.Value(pFeat.Fields.FindField("DATECREATE"))

pFeat = pEnumFeat.Next

' opening word doc with the field values inside the word bookmarks
wdApp = New Microsoft.Office.Interop.Word.Application
With wdApp
.Visible = True
.WindowState = WdWindowState.wdWindowStateMaximize
End With

mydoc = wdApp.Documents.Add(Template:="C:\Documents and Settings\ZQ3079\Mobile project\ExportWord(VB)\Audit Form Final.doc")
With mydoc.Bookmarks
.Item("A").Range.InsertAfter(A) ' "A" is bookmark name in word document
.Item("C").Range.InsertAfter(B) ' "C" is bookmark name in word document

End With
End Sub

Let me know.
0 Kudos
0 Replies