This sample exports all geographic feature layers and raster layers in the current map for use in ArcPad. Personal and SDE geodatabase layers can be copied out or checked out, all other feature and raster layers are copied out. This sample demonstrates how to use the IArcPadTransaction and IArcPadDataTransfer members.
How to use
- Open ArcMap.
- Paste the code into VBA and run the macro.
Public Sub CopyoutCheckout()
On Error GoTo ErrorHandler
'++ get a reference to the ArcPad extension
Dim pArcPadExtension As IArcPadExtension
Dim pID As New UID
pID = "editorExt.ArcPadExtension"
Set pArcPadExtension = Application.FindExtensionByCLSID(pID)
'++ ArcPad xfer error vars
Dim lErrorNum As Long
Dim sErrorDesc As String
'++ ArcPad transaction/xfer vars
Dim pAPTransaction As IArcPadTransaction
Set pAPTransaction = pArcPadExtension 'QI
Dim pArcPadXfer As IArcPadDataTransfer
'++ flag to know if any layers are exported for ArcPad
Dim bExported As Boolean
'++ set the output folder
Dim sOutputFolder As String
sOutputFolder = "C:\Temp\DataForArcPad"
'++ set edit form size to 1/4 VGA size
Dim iFormX As Long, iFormY As Long
iFormX = 130
iFormY = 130
pAPTransaction.SetFormSize iFormX, iFormY
'++ get an IBasicMap ptr to the focus map
Dim pMxDoc As IMxDocument
Dim pMap As IMap, pBasicMap As IBasicMap
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap
Set pBasicMap = pMap
'++ get the current view extent
Dim pActiveView As IActiveView
Dim pExtentEnv As IEnvelope
Set pActiveView = pMap
Set pExtentEnv = pActiveView.Extent
'++ spatial filter for checkout/copyout extent
Dim pSpatialFilter As ISpatialFilter
Set pSpatialFilter = New SpatialFilter
'++ layer definition query
Dim pFtrLayerDef As IFeatureLayerDefinition
Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter
'++ get checkout name
Dim sCheckoutName
sCheckoutName = InputBox("Please type the checkout name for this session.", "Checkout Name?")
If ("" = sCheckoutName) Then Exit Sub
'++ create ArcPad data xfer session
Set pArcPadXfer = pAPTransaction.CreateDataTransferSession(True, pBasicMap, sOutputFolder)
Dim sCurrOutputName As String
'++ first, handle all IGeoFeatureLayer layers
pID = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" 'IID for IGeoFeatureLayer
Dim pEnumLayer As IEnumLayer
Set pEnumLayer = pMap.Layers(pID, True)
If Not pEnumLayer Is Nothing Then
Dim pLayer As ILayer
Dim pFtrLayer As IFeatureLayer
Dim pFClass As IFeatureClass
Dim pDataSet As IDataset
Dim pSelSet As ISelectionSet
pEnumLayer.Reset
Set pLayer = pEnumLayer.Next
Do While Not pLayer Is Nothing
'++ get current display extent
Set pFtrLayer = pLayer 'QI
With pSpatialFilter
Set .Geometry = pExtentEnv
.GeometryField = pFtrLayer.FeatureClass.ShapeFieldName
.SpatialRel = esriSpatialRelIntersects
End With
'++ get layer definition query
Set pFtrLayerDef = pFtrLayer 'QI
pQueryFilter.WhereClause = pFtrLayerDef.DefinitionExpression
'++ get selection set
Set pFClass = pFtrLayer.FeatureClass
Set pSelSet = pFClass.Select(pSpatialFilter, esriSelectionTypeIDSet, esriSelectionOptionNormal, Nothing)
'++ give option to checkout or copyout gdb, copyout all others
Set pDataSet = pFClass 'QI
Select Case pDataSet.Category
Case "Personal Geodatabase Feature Class", "SDE Feature Class":
If MsgBox("Yes to Checkout, No to Copyout.", vbYesNo, pFtrLayer.Name) = vbYes Then
pArcPadXfer.CheckoutFeatures pLayer, pQueryFilter, pSelSet, True, False, sCheckoutName, "", sCurrOutputName
Else
pArcPadXfer.CopyoutFeatures pLayer, pQueryFilter, pSelSet, True, True, "", sCurrOutputName
End If
Case Else:
pArcPadXfer.CopyoutFeatures pLayer, pQueryFilter, pSelSet, True, True, "", sCurrOutputName
End Select
'++ report success or error
Select Case lErrorNum
Case 0:
MsgBox sCurrOutputName & " created.", vbInformation, pFtrLayer.Name
bExported = True
Case Else:
MsgBox lErrorNum, vbExclamation, pFtrLayer.Name & " not exported"
lErrorNum = 0
End Select
Set pLayer = pEnumLayer.Next
Loop
End If
'++ next, copyout IRasterLayer layers
pID = "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}" 'IID for IRasterLayer
Set pEnumLayer = pMap.Layers(pID, True)
If Not pEnumLayer Is Nothing Then
pEnumLayer.Reset
Set pLayer = pEnumLayer.Next
Do While Not pLayer Is Nothing
pArcPadXfer.CopyoutRaster pLayer, pExtentEnv, sCurrOutputName
'++ report success or error
Select Case lErrorNum
Case 0:
MsgBox sCurrOutputName & " created.", vbInformation, pLayer.Name
bExported = True
Case Else:
MsgBox sErrorDesc, vbExclamation, pLayer.Name & " not created"
lErrorNum = 0
End Select
Set pLayer = pEnumLayer.Next
Loop
End If
'++ end the transaction
pArcPadXfer.Flush
If bExported Then
MsgBox "Operation complete.", vbInformation, sCheckoutName
Else
MsgBox "No layers were exported for ArcPad.", vbExclamation, sCheckoutName
End If
Exit Sub
ErrorHandler:
Select Case Err.Number
'++ handle ArcPad xfer errors
Case -2147221247 To -2147221242:
lErrorNum = Err.Number
sErrorDesc = Err.Description
Resume Next
'++ handle E_FAIL; likely cause is use of IMap::Layers when no layers match the IID
Case -2147467259:
Set pEnumLayer = Nothing
Resume Next
'++ handle all other errors
Case Else:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error"
End Select
End Sub