Object reference not set to an instance

dd*

New member
Joined
Feb 22, 2010
Messages
3
Programming Experience
10+
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



VB.NET:
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 Files\CorrsOffice\Templates\document.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
 
You must call the word object and initiate it:
VB.NET:
Dim Word As [U][COLOR="Red"]New[/COLOR][/U] Microsoft.Office.Interop.Word.Application

If you having trouble releasing the process use this:
VB.NET:
Public Shared Sub ReleaseObject(ByVal o As Object)
    Dim i As Integer
    If Not o Is Nothing Then
      Try
        i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
        While i > 0
          i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
        End While
      Catch
      Finally
        o = Nothing
      End Try
    End If
End Sub
 
Back
Top