Select to view content in your preferred language

VBA Code Suddenly Not working, No changes, Ways to Fix?

1094
4
04-29-2010 07:15 AM
JoshuaWallin
New Contributor
I have a program written in Visual Basic through ArcGIS 9.3 which has worked fine for a few years and now suddenly I get an error every time I try to run it.  I have not updated my programs in the recent past and the programming language seems sound to me.  My other programs seem to work.  Can anyone look at this and help me figure out what might be causing a problem. I get Run-Time error '91' Object variable or With Block variable not set.  I have posted the code below, the code I have provided is supposed to go through each layer in my .mxd until it finds one with features selected in it.  I get an error on the very last line If pSelSet.Count > 0 Then Exit Do and I can't figure it out.

Public Sub UpdateData()
'On Error GoTo EH:
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pActiveView As IActiveView

Dim pFeatureLayer As IFeatureLayer
Dim pEnumLayer As IEnumLayer

Dim pLayer As ILayer
Dim pFeature As IFeature
Set pMxDoc = Application.Document
Set pEnumLayer = pMxDoc.FocusMap.Layers
Set pLayer = pEnumLayer.Next
Dim pFeatureSelection As IFeatureSelection
Dim pSelSet As ISelectionSet

Dim pID As New UID
pID = "esriCore.Editor"
Dim pEditor As IEditor
Dim pWorkspace As IWorkspace
Dim pDataset As IDataset
Dim pEnumFeature As IEnumFeature
Dim Phasenum As Integer
'========================================================

Do Until pLayer Is Nothing
  If TypeOf pLayer Is IFeatureLayer Then
   Set pFeatureSelection = pLayer
   Set pSelSet = pFeatureSelection.SelectionSet
   If pSelSet.Count > 0 Then Exit Do
End If
0 Kudos
4 Replies
NickClayton
New Contributor
I have a program written in Visual Basic through ArcGIS 9.3 which has worked fine for a few years and now suddenly I get an error every time I try to run it. I have not updated my programs in the recent past and the programming language seems sound to me. My other programs seem to work. Can anyone look at this and help me figure out what might be causing a problem. I get Run-Time error '91' Object variable or With Block variable not set. I have posted the code below, the code I have provided is supposed to go through each layer in my .mxd until it finds one with features selected in it. I get an error on the very last line If pSelSet.Count > 0 Then Exit Do and I can't figure it out.


Have you checked to see if pLayer.Selectable is true?
I'm not sure if that has an effect on the  SelectionSet, but that would be something to check.
0 Kudos
JohnHauck
Occasional Contributor II
Looks like you missed a few lines on the copy/paste. With the last few lines I added I don't get any errors. Does this happen for you in multiple mxds? Does this also happen if you just use the sub below without any additional code that may have also been in what you pulled this from?

Public Sub UpdateData()
'On Error GoTo EH:
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pActiveView As IActiveView

Dim pFeatureLayer As IFeatureLayer
Dim pEnumLayer As IEnumLayer

Dim pLayer As ILayer
Dim pFeature As IFeature
Set pMxDoc = Application.Document
Set pEnumLayer = pMxDoc.FocusMap.Layers
Set pLayer = pEnumLayer.Next
Dim pFeatureSelection As IFeatureSelection
Dim pSelSet As ISelectionSet

Dim pID As New UID
pID = "esriCore.Editor"
Dim pEditor As IEditor
Dim pWorkspace As IWorkspace
Dim pDataset As IDataset
Dim pEnumFeature As IEnumFeature
Dim Phasenum As Integer
'================================================= =======

Do Until pLayer Is Nothing
If TypeOf pLayer Is IFeatureLayer Then
Set pFeatureSelection = pLayer
Set pSelSet = pFeatureSelection.SelectionSet
If pSelSet.Count > 0 Then Exit Do
End If

''All I added
Set pLayer = pEnumLayer.Next
Loop
End Sub
0 Kudos
JoshuaWallin
New Contributor
Nice Catch, I had clipped the code

You are right, I tried the code in another mxd and it works ok.  I am going to try to remake the .mxd and see if that helps.

Thanks
0 Kudos
JoshuaWallin
New Contributor
Turns out the reason why my code was erroring out was I had a layer with missing source information because I had renamed it (and hidden it in a group layer). The program worked fine until it needed to layerEnum down to a layer below one of the ones with the missing source.  It works at it should now.
0 Kudos