Vous êtes sur la page 1sur 4

Sub AddPoints()

'This code written in VBA accepts an ecxel sheet having coordinates of points in
POI_X, and POI_Y columns.
'Converts the excel sheet to Text file and export to shapefile after converting
the text file to layer.
'So it can also be used to convert text file to shapefile.
' Tools-->References-->MicrosoftExcel 11.0 object Library has to be added so tha
t it can work with excel.
Dim Filename As String 'Contain file name with path
Dim wkb As Workbook
Excel.Application.DisplayAlerts = False
'Opens a dialog file to select
filex = Excel.Application.GetOpenFilename(FileFilter:="microsoft excel files (*.
xls), *.xls", Title:="Please select a file", MultiSelect:=False)
If filex <> False Then
Excel.Application.Workbooks.Open filex
Set wkb = ActiveWorkbook
Dim sPath As String
sPath = wkb.Path ' Getting the path to excel file which is workspace path.
'MsgBox sPath 'Debugging purpose
Filename = GetFileName(CStr(filex)) 'Gets only the file name without extension
wkb.SaveAs Filename:=Filename, FileFormat:=xlText, CreateBackup:=False 'Saves th
e file in Text Format
ActiveWorkbook.Close savechanges:=False

Excel.Application.DisplayAlerts = True
End If
Dim TxtFileName As String
TxtFileName = Filename + ".txt"

' Part 1: Define the text file.


Dim pWorkspaceName As IWorkspaceName
Dim pTableName As ITableName
Dim pDatasetName As IDatasetName
' Define the input table s workspace.
Set pWorkspaceName = New WorkspaceName
pWorkspaceName.PathName = sPath
pWorkspaceName.WorkspaceFactoryProgID = "esriCore.TextFileWorkspaceFactory.1"
' Define the dataset.
Set pTableName = New TableName
Set pDatasetName = pTableName

TxtFileName = Filename + ".txt"


pDatasetName.Name = TxtFileName
Set pDatasetName.WorkspaceName = pWorkspaceName

' Part 2: Define the XY events source name object.


Dim pXYEvent2FieldsProperties As IXYEvent2FieldsProperties
Dim pSpatialReferenceFactory As ISpatialReferenceFactory
Dim pProjectedCoordinateSystem As IProjectedCoordinateSystem2
Dim pXYEventSourceName As IXYEventSourceName
' Set the event to fields properties.
Set pXYEvent2FieldsProperties = New XYEvent2FieldsProperties
With pXYEvent2FieldsProperties
.XFieldName = "POI_X"
.YFieldName = "POI_Y"
.ZFieldName = ""
End With
' Set the spatial reference.
Set pSpatialReferenceFactory = New SpatialReferenceEnvironment
Set pProjectedCoordinateSystem = _
pSpatialReferenceFactory.CreateProjectedCoordinateSystem(esriSRProjCS_WGS1984UTM
_44N)
' Specify the event source and its properties.
Set pXYEventSourceName = New XYEventSourceName
With pXYEventSourceName
Set .EventProperties = pXYEvent2FieldsProperties
Set .SpatialReference = pProjectedCoordinateSystem
Set .EventTableName = pTableName
End With

' Part 3: Display XY events.


Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pName As IName
Dim pXYEventSource As IXYEventSource
Dim pFLayer As IFeatureLayer
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap
' Open the events source name object.
Set pName = pXYEventSourceName
Set pXYEventSource = pName.Open
' Create a new feature layer and add the layer to the display.
Set pFLayer = New FeatureLayer
Set pFLayer.FeatureClass = pXYEventSource
Dim pFeatureClass1 As IFeatureClass
Set pFeatureClass1 = pXYEventSource
Call ExportFeatureClassToShapeFileAndAddAsLayer(pFeatureClass1,Filename)'Creates
shapeffile after exporting it.
'pFLayer.Name = "XYEvent" 'This line code and code below this is used to add la
yer to if required
'pMap.AddLayer pFLayer

End Sub

Public Function GetFileName(flname As String) As String


'Get the filename without the path or extension.
'Input Values:
' flname - path and filename of file.
'Return Value:
' GetFileName - name of file without the extension.
Dim posn As Integer, i As Integer
Dim fName As String
posn = 0
'find the position of the last "\" character in filename
For i = 1 To Len(flname)
If (Mid(flname, i, 1) = "\") Then posn = i
Next i
'get filename without path
fName = Right(flname, Len(flname) - posn)
'get filename without extension
posn = InStr(fName, ".")
If posn <> 0 Then
fName = Left(fName, posn - 1)
End If
GetFileName = fName
End Function

Sub ExportFeatureClassToShapeFileAndAddAsLayer(pFc As IFeatureClass,Filename As


String)

Dim pINFeatureClassName As IFeatureClassName


Dim pDataset As IDataset
Dim pInDsName As IDatasetName
Dim pFeatureClassName As IFeatureClassName
Dim pOutDatasetName As IDatasetName
Dim pWorkspaceName As IWorkspaceName
Dim pExportOp As IExportOperation

'Get the FcName from the featureclass


Set pDataset = pFc
Set pINFeatureClassName = pDataset.FullName
Set pInDsName = pINFeatureClassName

'Define the output feature class name


Set pFeatureClassName = New FeatureClassName
Set pOutDatasetName = pFeatureClassName
pOutDatasetName.Name = Filename
Set pWorkspaceName = New WorkspaceName
pWorkspaceName.PathName = "c:\temp"
pWorkspaceName.WorkspaceFactoryProgID = _
"esriCore.shapefileworkspacefactory.1"
Set pOutDatasetName.WorkspaceName = pWorkspaceName
pFeatureClassName.FeatureType = esriFTSimple
pFeatureClassName.ShapeType = esriGeometryAny
pFeatureClassName.ShapeFieldName = "Shape"
'Export
Set pExportOp = New ExportOperation
pExportOp.ExportFeatureClass pInDsName, Nothing, Nothing, Nothing, pFeatureC
lassName, 0
End Sub

Vous aimerez peut-être aussi