none
Validate caption labels RRS feed

  • Question

  • Hi, I'm making a word template in VSTO and I want to validate whether a CaptionLabels.

    The code I am using is the following and I will fail. Can you help me with this problem?

    Public Class frmIns_texto_docto
        Dim texto_insertar, tipo_texto_insertar As String
        Dim textorange As Word.Range = Globals.ThisDocument.Application.Selection.Range()


        Private Sub btn_aceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_aceptar.Click

            tipo_texto_insertar = lbl_tipo_texto.Text 'Variable para determinar el tipo de caso a realizar

            texto_insertar = txtIns_texto.Text 'Variable con el valor de texto a insertar

            If texto_insertar = "" Then

                Exit Sub

            End If

            Select Case tipo_texto_insertar

                Case "figura"
                    Dim captionlabelexist As Boolean

                    Dim captionlbl As Word.CaptionLabel = Globals.ThisDocument.Application.CaptionLabels

                    captionlabelexist = captionlbl("Figura")


                    If captionlabelexist = False Then
                        Globals.ThisDocument.Application.CaptionLabels.Add("Figura")

                    End If

                    With Globals.ThisDocument.Application.Selection
                        .InsertCaption(Label:="Figura", TitleAutoText:="InsertarTítulo1", Title:="", Position:=Word.WdCaptionPosition.wdCaptionPositionBelow, _
                                       ExcludeLabel:=0)
                        .TypeText(Text:=". " & texto_insertar)
                    End With
                    textorange.Select()
                    textorange.Style = "Figuras MI"

                Case "nota"

                    Globals.ThisDocument.Application.Selection.TypeText(Text:="Nota: " & texto_insertar)
                    textorange.Select()
                    textorange.Style = "Notas"

                Case Else

                    Exit Sub

            End Select

            Me.Close()
            Globals.ThisDocument.Application.ActiveWindow.SetFocus()
        End Sub

    End Class

    Thanks in advance

    Sunday, August 17, 2014 3:52 AM

Answers

  • Hi Gustavo

    The CaptionLabels collection doesn't work the way Fei Xue proposes. It's not possible to use a non-existant caption label as an indexer - you get the error message you report, rather than return Nothing/null as would be the case in a .NET Framework collection. This is often the case with collections in the Word object model, which dates back to the early 1990s...

    Instead, you have to LOOP (For or ForEach) the collection and compare the Name of each entry with the string you're interested in. If you loop through the entire collection without a match, then the member does not exist. Indeed, this is not efficient, but that's how it is! If you need to query the CaptionLabels more than once it's probably a good idea to loop the collection and write the members to an Array or other Framework entity that you can access more efficiently (will return Nothing/null if the member is not present).


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, August 19, 2014 5:10 PM
    Moderator
  • Hello Cindy,

    Thanks for you response.

    I already solved my issue, I use "try catch" to catch the exception null / nothing.

    Private Sub btnIns_tit_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIns_tit_img.Click

            Dim frmIns_texto_docto As New frmIns_texto_docto

            Try
                If CaptionLabelExists("Figura") = False Then
                    Globals.ThisDocument.Application.CaptionLabels.Add("Figura")
                End If

            Catch ex As Exception
                MsgBox(ex.ToString)

            End Try

            frmIns_texto_docto.Text = "Titulo de figura"
            frmIns_texto_docto.lblIns_texto_docto.Text = "Introduce el nombre de la figura:"
            frmIns_texto_docto.txtIns_texto.Text = ""
            frmIns_texto_docto.lbl_tipo_texto.Text = "figura"
            frmIns_texto_docto.ShowDialog()


        End Sub

    Regards

    Tuesday, September 2, 2014 2:38 PM
  • Hi Gustavo

    You can do that, however you need to be aware that invoking Try..Catch will also slow your code down. No idea if it's slower than the loop approach, so just FYI :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 10, 2014 5:14 PM
    Moderator

All replies

  • Hi Gustavo,

    The return value of CaptionLabels collection is an individual CaptionLabel object in a collection. We can pass it to a Boolean value. Here is the code to check whether it exists:

    If IsNothing(Globals.ThisAddIn.Application.CaptionLabels("Figure")) Then
    
    End If

    You can get more detail from link below:
    CaptionLabels.Item Method

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 18, 2014 8:27 AM
    Moderator
  • Hello Fei

    thanks, I tried the code you sent me but I still see the same error.

    The element of the set requested does not exist.

    error code -2146822347

    I'm working on a template word, so I use Globals.ThisDocument.Application code for my forms.

    Regards

    Monday, August 18, 2014 9:45 PM
  • Hi Gustavo

    The CaptionLabels collection doesn't work the way Fei Xue proposes. It's not possible to use a non-existant caption label as an indexer - you get the error message you report, rather than return Nothing/null as would be the case in a .NET Framework collection. This is often the case with collections in the Word object model, which dates back to the early 1990s...

    Instead, you have to LOOP (For or ForEach) the collection and compare the Name of each entry with the string you're interested in. If you loop through the entire collection without a match, then the member does not exist. Indeed, this is not efficient, but that's how it is! If you need to query the CaptionLabels more than once it's probably a good idea to loop the collection and write the members to an Array or other Framework entity that you can access more efficiently (will return Nothing/null if the member is not present).


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, August 19, 2014 5:10 PM
    Moderator
  • Hello Cindy,

    Thanks for you response.

    I already solved my issue, I use "try catch" to catch the exception null / nothing.

    Private Sub btnIns_tit_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIns_tit_img.Click

            Dim frmIns_texto_docto As New frmIns_texto_docto

            Try
                If CaptionLabelExists("Figura") = False Then
                    Globals.ThisDocument.Application.CaptionLabels.Add("Figura")
                End If

            Catch ex As Exception
                MsgBox(ex.ToString)

            End Try

            frmIns_texto_docto.Text = "Titulo de figura"
            frmIns_texto_docto.lblIns_texto_docto.Text = "Introduce el nombre de la figura:"
            frmIns_texto_docto.txtIns_texto.Text = ""
            frmIns_texto_docto.lbl_tipo_texto.Text = "figura"
            frmIns_texto_docto.ShowDialog()


        End Sub

    Regards

    Tuesday, September 2, 2014 2:38 PM
  • Hi Gustavo

    You can do that, however you need to be aware that invoking Try..Catch will also slow your code down. No idea if it's slower than the loop approach, so just FYI :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 10, 2014 5:14 PM
    Moderator