1. 新建openshp按钮,调用打开名为open的userform
2. 在userform中添加通用对话框,命名为dlgFile,添加名为cmdshowshp1的浏览按钮,添加代码:
Private Sub cmdshowshp1_Click()
'打开shp文件,并获取其文件路径、文件名等
dlgFile.DialogTitle = "打开文件"
dlgFile.Filter = "SHP文件|*.shp"
dlgFile.FilterIndex = 1
dlgFile.Showopen
shpAdd1 = dlgFile.FileName '获取文件路径(包含文件名)
tbshpadd.Value = shpAdd1 '文本框中显示路径
If Len(shpAdd1) <> 0 Then
shpname = dlgFile.FileTitle '获取文件名
shpAddress = Left$(shpAdd1, Len(shpAdd1) - Len(shpname)) '获取文件所在文件夹路径
shpname = Left$(shpname, Len(shpname) - 4) '获取文件名(去掉后缀名)
End If
End Sub
Private Sub Openshp(ByVal shpAddress As String, ByVal shpTitle As String, ByVal shpLayerName As String) '从硬盘打开shape文件
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Dim pWorkspace As IFeatureWorkspace
Set pWorkspace = pWorkspaceFactory.OpenFromFile(shpAddress, 0)
Dim pClass As IFeatureClass
Set pClass = pWorkspace.OpenFeatureClass(shpTitle)
Dim pLayer As IFeatureLayer
Set pLayer = New FeatureLayer
Set pLayer.FeatureClass = pClass
pLayer.Name = shpLayerName
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
pMxDoc.AddLayer pLayer
pMxDoc.ActivatedView.PartialRefresh esriViewGeograph, pLayer, Nothing
End Sub
Private Sub CommandButton2_Click()
'确定按钮
If (shpin.Value = True And Len(shpname) <> 0) Then
Call Openshp(shpAddress, shpname, "街区底图")
End If
End Sub
![](http://image56.360doc.cn/DownloadImg/2012/11/2315/28427687_1.jpg)