Select to view content in your preferred language

Copy labeling properties from layer A to layer B

1237
7
05-31-2010 01:13 AM
MartijnSenden
Emerging Contributor
Hi All,

I'm trying to copy labeling properties from layer A to layer B using VB-code in an mxd I'm creating.
I tried the following code:

'Apply labeling properties from 'Layer_A' to 'Layer_B'
[INDENT]Dim IGeoFeatureLayerA, IGeoFeatureLayerB As IGeoFeatureLayer[/INDENT]
'Find layers A and B
[INDENT]Set IGeoFeatureLayerA = GetLayerByName("Layer_A")
Set IGeoFeatureLayerB = GetLayerByName("Layer B")[/INDENT]

'Apply labeling settings from feature layer Layer_A to feature layer Layer_B
[INDENT]Dim pAnnoLayerPropsCollA, pAnnoLayerPropsCollB As IAnnotateLayerPropertiesCollection
Dim pAnnoLayerPropsA As IAnnotateLayerProperties
Set pAnnoLayerPropsCollA = IGeoFeatureLayerA.AnnotationProperties
Set pAnnoLayerPropsCollB = IGeoFeatureLayerB.AnnotationProperties
pAnnoLayerPropsCollA.QueryItem 0, pAnnoLayerPropsA, Nothing, Nothing

pAnnoLayerPropsCollB.Add pAnnoLayerPropsA
IGeoFeatureLayer1.DisplayAnnotation = True[/INDENT]


And the custom function GetLayerByName:
Public Function GetLayerByName(strLayerName As String) As IGeoFeatureLayer
'Returns the layer based on the given name. The layer may be in a grouplayer, which itself may also be in a group layer.
'Deeper nesting is not supported and would need adjustment of the code below

Dim pDoc As IMxDocument
Dim pMap As IMap
Dim pLayer As ILayer
Dim pFeatureLayer As IFeatureLayer
Dim i As Integer

Set pDoc = ThisDocument
Set pMap = pDoc.FocusMap

For i = 0 To pMap.LayerCount - 1
    Set pLayer = pMap.Layer(i)
    If TypeOf pLayer Is IGeoFeatureLayer Then 'Layer is a geofeature layer.
        Set pFeatureLayer = pLayer
        If pFeatureLayer.Name = strLayerName Then 'Layer has the right name.
            Set GetLayerByName = pFeatureLayer
            Exit Function
        End If
    ElseIf TypeOf pLayer Is IGroupLayer Then 'Layer is a grouplayer. Check its contents.
        Dim pCompLayer As ICompositeLayer
        Set pCompLayer = pLayer
        Dim j As Integer
        For j = 0 To pCompLayer.Count - 1
            If TypeOf pCompLayer.Layer(j) Is IGeoFeatureLayer Then 'Sublayer is a geofeature layer.
                Set pFeatureLayer = pCompLayer.Layer(j)
                If pFeatureLayer.Name = strLayerName Then 'Sublayer has the right name.
                    Set GetLayerByName = pFeatureLayer
                    Exit Function
                End If
            ElseIf TypeOf pCompLayer.Layer(j) Is IGroupLayer Then 'Sublayer is a grouplayer. Check its contents.
                Dim pCompLayer2 As ICompositeLayer
                Set pCompLayer2 = pCompLayer.Layer(j)
                Dim k As Integer
                For k = 0 To pCompLayer2.Count - 1
                    If TypeOf pCompLayer2.Layer(k) Is IGeoFeatureLayer Then 'Subsublayer is a geofeature layer.
                        Set pFeatureLayer = pCompLayer2.Layer(k)
                        If pFeatureLayer.Name = strLayerName Then 'Subsublayer has the right name.
                            Set GetLayerByName = pFeatureLayer
                            Exit Function
                        End If
                    End If
                Next k
            End If
        Next j
    End If
Next i

End Function



Now when I run this code, I get a Compile error: ByRef argument type mismatch. I get it on this line, specifically on the variable pAnnoLayerPropsA:
pAnnoLayerPropsCollA.QueryItem 0, pAnnoLayerPropsA, Nothing, Nothing

I don't understand. I though the second argument for IAnnotateLayerPropertiesCollection.QueryItem should be an IAnnotateLayerProperties object. So, two questions:


  1. Why do I get the error mentioned above? How can I prevent it from occurring?

  2. Is the approach I'm taking for the problem (getting the labeling properties from Layer A to Layer B) the best way to go? Do you have suggestions for improvements. I have experience in programming in VBA for MS Access and MS Excel, but I'm new to programming in ArcGis.


  3. Thanks in advance for any help! 

    Best regards, 
    Martijn Senden. 
0 Kudos
7 Replies
MartijnSenden
Emerging Contributor
Hi All,

Does anyone have any ideas about my questions above? Do I need to provide more information? Again, any help will be greatly appreciated! Thanks in advance.

Best regards,
Martijn Senden.
0 Kudos
jessemaps
Regular Contributor
Set IGeoFeatureLayerA = GetLayerByName("Layer_A")
Set IGeoFeatureLayerB = GetLayerByName("Layer B")


"Layer B" is missing the underscore.
0 Kudos
MartijnSenden
Emerging Contributor
"Layer B" is missing the underscore.


Hi, thanks for your reply. I don't think this is the problem. The names Layer_A and Layer_B are not the real names of the layers. I just used these in the post. In the real code there are no typos in the naming of the layers...

Anyone else have any idea on what the solution for the problem indicated above could be?

Thanks in advance!

Best regards,
Martijn Senden.
0 Kudos
KirkKuykendall
Deactivated User
Did you try IAnnotateLayerPropertiesCollection2.QueryItem instead?
0 Kudos
MartijnSenden
Emerging Contributor
Hi,

Thanks for the reply! I didn't tyr that before, but I did now. I still get the same error message.

Any more ideas?

Best regards,
Martijn Senden.
0 Kudos
NeilClemmons
Honored Contributor
I've never tried it but have you tried just copying the whole thing:

Set IGeoFeatureLayerB.AnnotationProperties = IGeoFeatureLayerA.AnnotationProperties

If that seems to work then you'll really want to make a deep copy of the collection so that the two layers aren't sharing the same object reference:

Dim oc As IObjectCopy
Set oc = New ObjectCopy
Set IGeoFeatureLayerB.AnnotationProperties = oc.Copy(IGeoFeatureLayerA.AnnotationProperties)
0 Kudos
MartijnSenden
Emerging Contributor
Hi again,

Sorry for reacting so late to this post! Due to all sorts of circumstances at work, I have not been able to work on this project for several months. I now have to finish what I started back in July however. I am still having problems copying over the labeling from layer A to layer B.

Neil, I tried your suggestion and hope I may have some succes with it. I haven't got it working entirely though. I tried this:

'Apply labeling properties to new layer 01_Symbols
Dim IGeoFeatureLayerA, IGeoFeatureLayerB As IGeoFeatureLayer
Dim pAnnoLayerPropsCollA As IAnnotateLayerPropertiesCollection
Dim oc As IObjectCopy

'Find layers 01_Symbols and Symbols
Set IGeoFeatureLayerB = GetLayerByName("01_Symbols")
Set IGeoFeatureLayerA = GetLayerByName("Symbols")

'Apply labeling settings from feature layer Symbols to feature layer 01_Symbols
Set oc = New ObjectCopy
Set pAnnoLayerPropsCollA = oc.Copy(IGeoFeatureLayerA.AnnotationProperties)
MsgBox (pAnnoLayerPropsCollA.Count) 'Just for error trapping >> this returns 1, so the object exists?
Set IGeoFeatureLayerB.AnnotationProperties = pAnnoLayerPropsCollA
IGeoFeatureLayerB.DisplayAnnotation = True


The error I get is this:
"Run-time error '424': Object required"
on this line:
Set IGeoFeatureLayerB.AnnotationProperties = oc.Copy(IGeoFeatureLayerA.AnnotationProperties)


I put in the messagebox for testing purposes after I got this error. It returns 1, so the object "pAnnoLayerPropsCollA" seems to exist.

So, I am sort of at a loss at what to do right now. Any ideas anyone?

Thanks in advance, any help will be appreciated!

Best regards,
Martijn Senden.
0 Kudos