You are here

Scripts for the Non-Programmer: Retrieving an Existing IWorkspace

When writing code or running scripts for ArcGIS products, you’re likely to need to define an IWorkspace. An IWorkspace can be created from scratch or manually loaded, but the most useful task is to take the workspace from the environment you are already working in. When you obtain an existing workspace, you’re getting information about your current environment. This includes all of the feature classes, object classes, and datasets in your geodatabase.

Getting and defining your existing workspace as an IWorkspace object will allow you to perform a variety of very useful scripts. With this information in place, we will define many of these scripts in future issues.

In this article, we’ll be creating a VBA module to retrieve the workspace. The functions for this module can be saved and used with other scripts to easily obtain an IWorkspace. Each method has different requirements, so be sure to use the one that’s most convenient for you!

As a reminder, follow these steps to create a module:

  • Within ArcMap, drop down the Tools menu and choose MacrosVisual Basic Editor.
  • Within the Visual Basic environment, expand the Normal.mxt file and insert a new Module within the Modules folder.
  • Copy and paste each applicable function into the module depending on which scenarios you may encounter. Later, you may selectively choose from the functions based on your needs.

Retrive from last edit session

You must be editing within an edit session for this method to work, and you must use this method if your script will be editing any versioned data.

Public Function GetEditWorkspace()
    Dim pEditor As IEditor
    Dim pID As New UID

    pID = "esriEditor.Editor"
    Set pEditor = Application.FindExtensionByCLSID(pID)

    If pEditor.EditState <> esriStateEditing Then
        MsgBox "Must be editing!"
        Exit Function
    End If

    Set
GetEditWorkspace = pEditor.EditWorkspace
End Function

Retrieve from Layers

This method retrieves the workspace from the layers in your table of contents:

useful-scripts-layers

The script will loop through each layer and take the workspace from the first feature layer that contains one. If you’re using data from additional sources (for example, importing a shapefile or comparing data from two geodatabase connections), be sure that the layer in the desired workspace is at the topmost position in this list.

Public Function GetLayerWorkspace()
    Dim mxDoc As IMxDocument
    Set mxdoc = ThisDocument

    Dim Layers As IEnumLayer
    Dim ds As IDataset

    Dim pID As New UID
    pID.value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"

    Dim layer As IFeatureLayer
    Set layer = mxDoc.FocusMap.layers(pID, True).Next
    Set ds = layer.FeatureClass
    Set GetLayerWorkspace = ds.workspace
End Function

Retrieve from ArcFM™ Login

If you use the Telvent ArcFM™ extension for ArcGIS, ArcFM™’s login prompt (pictured below) is one of the easiest ways to obtain an IWorkspace object.

useful-scripts-arcfm-login

Once a user is logged into ArcFM™, the workspace can be obtained with the following function:

Public Function GetArcFMWorkspace()
    Dim loginUtils As IMMLoginUtils
    Set loginUtils = New MMLoginUtils
    Set GetArcFMWorkspace = loginUtils.LoginWorkspace
End Function

By inserting these functions into a module, you will be able to reference them in all of your future scripts. Now that you are in possession of these scripts, we can provide you with a wide variety of useful VBA scripts in the coming issues. Stay tuned!

Author Information

  • Corey Blakeborough

    Corey Blakeborough

    Corey Blakeborough is a Senior Consultant at the Utility & Telecommunications GIS consulting company SSP Innovations in Centennial, Colorado. Corey has over five years of experience with SSP, providing a variety of GIS and work management solutions.

    See all items created by this author >

    Connect with me on:

Add new comment