• vb.net – Object reference not set to an instance

    Home » Forums » Developers, developers, developers » DevOps Lounge » vb.net – Object reference not set to an instance

    Author
    Topic
    #467059

    hello

    when I run the below code the error message displays “Object reference not set to an instance of an object”

    its on the line
    With wordList.Application.ListGalleries(WdListGalleryType.wdBulletGallery).ListTemplates(1).ListLevels(1)

    I understand that I must set this line to something eg a selection.

    However Im unsure on how to do this.

    Previously when I defined

    ‘Dim Word As Microsoft.Office.Interop.Word.Application

    This worked, however what was occuring in the background a new instance of Winword.exe was being created and didn not close.

    Can a second set of eyes pls look at my code and let me know what/where Im going wrong.

    many thanks

    diana

    Code:
    Public Sub OnActionCorrsWordApplyStyle(ByVal control As IRibbonControl)
            Try
                Dim wordDoc As WordDocument = WordLibrary.ActiveDocument
                Dim selectionStyle As MSWord.Style = CType(wordDoc.Library.Application.Application.Selection.Style, Microsoft.Office.Interop.Word.Style)
                Dim style2 As MSWord.Style = wordDoc.GetStyle("Normal")
    
                Dim wordList As Microsoft.Office.Interop.Word.ListGalleries
    
    
                'Dim Word As Microsoft.Office.Interop.Word.Application
    
                'Dim WordApp As Microsoft.Office.Interop.Word.Application
    
                'Dim wordList As MSWord.ListGalleries
                
                'Dim selection As MSWord.Selection = wordDoc.Document.Application.Selection
    
    
                Dim selection As MSWord.Selection = wordDoc.Library.Application.Selection
    
    
    
                If control.Tag = "Corrs Bullet" Or control.Tag = "Corrs Number" Then
    
                   
    
    
                    With wordList.Application.ListGalleries(WdListGalleryType.wdBulletGallery).ListTemplates(1).ListLevels(1)
    
                        .NumberFormat = ChrW(61623)
                        .ResetOnHigher = CInt(False)
                        .StartAt = 1
                        .LinkedStyle = ""
                        .TrailingCharacter = WdTrailingCharacter.wdTrailingTab
                        .NumberStyle = WdListNumberStyle.wdListNumberStyleBullet
                        .Alignment = WdListLevelAlignment.wdListLevelAlignLeft
                        .TextPosition = wordList.Application.CentimetersToPoints(1.5)
                        .TabPosition = wordList.Application.CentimetersToPoints(1.5)
                        With .Font
                            .Name = "Symbol"
                        End With
    
                    End With
                    wordList.Application.ListGalleries(WdListGalleryType.wdBulletGallery).ListTemplates(1).Name = ""
    
    
                    'reset on higher to false so each time
                    'Corrs numbering doesnt restart everytime its inserted
                    With wordList.Application.ListGalleries(WdListGalleryType.wdNumberGallery).ListTemplates(1).ListLevels(1)
                        .NumberFormat = "%1"
                        .TrailingCharacter = WdTrailingCharacter.wdTrailingTab
                        .NumberStyle = WdListNumberStyle.wdListNumberStyleArabic
                        .NumberPosition = wordList.Application.CentimetersToPoints(0)
                        .Alignment = WdListLevelAlignment.wdListLevelAlignLeft
                        .TextPosition = wordList.Application.CentimetersToPoints(1.5)
                        .TabPosition = wordList.Application.CentimetersToPoints(1.5)
                        .ResetOnHigher = CInt(False)
                        .StartAt = 1
    
                        'ensure number is not bold
                        With .Font
                            .Bold = CInt(False)
                        End With
                    End With
                    wordList.Application.ListGalleries(WdListGalleryType.wdNumberGallery).ListTemplates(1).Name = ""
    
    
    
                    With selection
                        .ParagraphFormat.LeftIndent = 0
                        .ParagraphFormat.TabIndent(CShort(1.5))
                    End With
    
    
     
    
                    If control.Tag = "Corrs Bullet" Then
    
                        If selectionStyle.NameLocal = "Corrs Bullet" Then
                            wordDoc.Document.Application.Selection.Style = style2
                            Exit Sub
    
                        Else
    
                            If selection.Application.Selection.Range.ListFormat.ListType = 0 Then
    
                                If selectionStyle.NameLocal = "Normal Indent" Then
                                    wordDoc.Document.Application.Selection.Style = "Normal"
                                End If
                                selection.Application.ListGalleries(WdListGalleryType.wdBulletGallery).ListTemplates(1).Name = ""
    
                            Else
                                selection.Application.Application.Selection.Range.ListFormat.RemoveNumbers()
    
                            End If
                            Exit Sub
    
                        End If
    
                    End If
    
    
                    If control.Tag = "Corrs Number" Then
    
                        If selectionStyle.NameLocal = "Corrs Number" Then
                            wordDoc.Document.Application.Selection.Style = style2
                            Exit Sub
    
                        Else
    
                            If selection.Application.Selection.Range.ListFormat.ListType = 0 Then
    
                                If selectionStyle.NameLocal = "Normal Indent" Then
                                    wordDoc.Document.Application.Selection.Style = "Normal"
                                End If
                                selection.Application.ListGalleries(WdListGalleryType.wdNumberGallery).ListTemplates(1).Name = ""
    
                            Else
                                selection.Application.Selection.Range.ListFormat.RemoveNumbers()
    
                            End If
                            Exit Sub
    
                        End If
    
                    End If
                End If
    
    
                Init()
    
                Dim style As MSWord.Style = WordLibrary.ActiveDocument.GetStyleNoError(control.Tag)
                If style Is Nothing Then
                    WordLibrary.Application.OrganizerCopy(Source:= _
            "C:Program FilesCorrsOfficeTemplatesDocument.dotx", Destination:= _
            WordLibrary.Application.ActiveDocument.FullName, Name:=control.Tag, Object:=Microsoft.Office.Interop.Word.WdOrganizerObject.wdOrganizerObjectStyles)
                    style = WordLibrary.ActiveDocument.GetStyle(control.Tag)
    
                End If
                If style IsNot Nothing Then m_documentstyleController.SetFontStyle(WordLibrary.ActiveDocument, style)
            Catch ex As Exception
                Logger.Message(ex.Message)
            End Try
        End Sub
    Viewing 1 reply thread
    Author
    Replies
    • #1216663

      Hi Diana.

      It may be something as simple as doing a “Dim wordlist as New Microsoft.Office.Interop.Word.ListGalleries”. Try using “New” as my experience with Office objects is that simply dimming them isn’t enough.

      Hope that helps!

      Jeff

      • #1217006

        thanks Jeff

        yes i have to read up on defining in vb.net 😮

        many thanks again Jeff

        diana

    • #1217020

      The Set keyword is not supported under VB .NET, nor is the companion Let keyword So, the .NET syntax looks like like Jeff says.

      From VB .NET Language in a Nutshell: Appendix A: What’s New and Different in VB .NET

      Object Creation
      VB 6 supports a form of object creation called implicit object creation. If an object variable is declared using the New keyword:

      Dim obj As New SomeClass

      then the object is created the first time it is used in code. More specifically, the object variable is initially given the value Nothing, and then every time the variable is encountered during code execution, VB checks to see if the variable is Nothing. If so, the object is created at that time.

      VB .NET does not support implicit object creation. If an object variable contains Nothing when it is encountered, it is left unchanged, and no object is created.

      In VB .NET, we can create an object in the same statement as the object-variable declaration, as the following code shows:

      Dim obj As SomeClass = New SomeClass

      As a shorthand, we can also write:

      Dim obj As New SomeClass

      If the object’s class constructor takes parameters, then they can be included, as in the following example:

      Dim obj As SomeClass = New SomeClass(argument1, argument2,…)

      As a shorthand, we can also write:

      Dim obj As New SomeClass(argument1, argument2,…)


      Alan

      • #1217021

        many thanks Alan

        At lunch time Im heading out to the bookstore to buy that book

        thanks again Alan

        diana

    Viewing 1 reply thread
    Reply To: vb.net – Object reference not set to an instance

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: