IStandaloneTable addThisTable = null;
IFeatureWorkspace fws = Editor3.EditWorkspace as IFeatureWorkspace;
if (fws != null)
{
addThisTable = fws.OpenTable("TableToBeAdded") as IStandaloneTable;
if (addThisTable != null)
return Result.Success();
}
bool isFound = false;
IEnumTableProperties pEnumTP = MxDoc.TableProperties.IEnumTableProperties;
pEnumTP.Reset();
ITableProperty3 pTP3 = pEnumTP.Next() as ITableProperty3;
while (pTP3 != null)
{
if (pTP3.StandaloneTable != null)
{
if (pTP3.StandaloneTable.Name.ToLower() == "TableToBeAdded")
{
isFound = true;
break;
}
}
pTP3 = pEnumTP.Next() as ITableProperty3;
}
if (!isFound)
{
pTP3 = new TableProperty() as ITableProperty3;
pTP3.StandaloneTable = addThisTable;
pTP3.SelectedTable = true;
MxDoc.TableProperties.Add(pTP3 as ITableProperty);
}
Greg,
To add or find a table in the map document you need to be using the IStandaloneTableCollection
Interface. You can loop with this to compare names of tables in the collection. There is no "find the table with this name" function you must enumerate the collection.
Duncan
Sub test() ' Get document Dim pMXDocument As IMxDocument Set pMXDocument = ThisDocument ' Get Map Dim pMap As IMap Set pMap = pMXDocument.FocusMap ' Get table collection Dim pStandaloneTableCollection As IStandaloneTableCollection Set pStandaloneTableCollection = pMap ' Get a count of tables Dim n As Integer Let n = pStandaloneTableCollection.StandaloneTableCount ' Step through each table and print name Dim pStandaloneTable As IStandaloneTable Dim i As Integer For i = 0 To (n - 1) Set pStandaloneTable = pStandaloneTableCollection.StandaloneTable(i) Debug.Print pStandaloneTable.Name Next i End Sub
Greg,
Here is a simple bit of code in VBA to get you going.Sub test() ' Get document Dim pMXDocument As IMxDocument Set pMXDocument = ThisDocument ' Get Map Dim pMap As IMap Set pMap = pMXDocument.FocusMap ' Get table collection Dim pStandaloneTableCollection As IStandaloneTableCollection Set pStandaloneTableCollection = pMap ' Get a count of tables Dim n As Integer Let n = pStandaloneTableCollection.StandaloneTableCount ' Step through each table and print name Dim pStandaloneTable As IStandaloneTable Dim i As Integer For i = 0 To (n - 1) Set pStandaloneTable = pStandaloneTableCollection.StandaloneTable(i) Debug.Print pStandaloneTable.Name Next i End Sub
Duncan
Sub test2()
' Get document
Dim pMXDocument As IMxDocument
Set pMXDocument = ThisDocument
' Get Map
Dim pMap As IMap
Set pMap = pMXDocument.FocusMap
' Get table collection
Dim pStandaloneTableCollection As IStandaloneTableCollection
Set pStandaloneTableCollection = pMap
' Create workspacefactory (for opening up dBase files)
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Dim pFeatureWorkSpace As IFeatureWorkspace
Set pFeatureWorkSpace = pWorkspaceFactory.OpenFromFile("C:\Temp", 0)
' Get dBase table
Dim pTable As ITable
Set pTable = pFeatureWorkSpace.OpenTable("testtable") ' note no .dbf
' Create StandaloneTable
Dim pStandaloneTable As IStandaloneTable
Set pStandaloneTable = New StandaloneTable
Set pStandaloneTable.Table = pTable
pStandaloneTable.Name = "My Table"
' Add table to Map
pStandaloneTableCollection.AddStandaloneTable pStandaloneTable
pMXDocument.UpdateContents
End SubITableWindow tabwindow = new TableWindow(); tabwindow.Application = Application; tabwindow.Table = Table; tabwindow.Show(true);