I am writing a program that reads paragraphs from a word document and puts them between XML tags. I have a class which handles the loading and reading of the word document. The document loads fine, and the program can read all the words into the program, but not on a paragraph by paragraph basis. The program registers all of the paragraphs in the document as well, but when I try using any variation of the

.paragraph(0).range.text

syntax I get an error, which is either "system.nullReferenceException" or "system.runtime.InteropServices.COMException(), the requested member of the collection does not exist". The code below returns the "System.nullReferenceException" error.

Public Class WordDocInfo

    Private MSword As New Microsoft.Office.Interop.Word.ApplicationClass()
    Private document As New Microsoft.Office.Interop.Word.Document()

    Private paragraphnumber As Integer = 0
    Private FileName As String


    'getters and setters ------------------------------------------------------------
    Property nameinfo() As String
        'sets filename and gets filename
        Get
            Return FileName
        End Get
        Set(ByVal Value As String)
            FileName = Value
            setwordDoc()
        End Set
    End Property
    'constructors -------------------------------------------------------------------
    'instantiate the filename 
    Public Sub New()
        FileName = "hello.doc"
    End Sub
    Public Sub New(ByVal Value As String)
        FileName = Value
    End Sub

    'sets the word document to the file path, an error will come up if the path is invalid
    Private Sub setwordDoc()
        Try
            document = MSword.Documents.Open(FileName, , True)
        Catch E As Exception
            MessageBox.Show("Error, file path is invalid")
        End Try
    End Sub

    'returns the current paragraph that the counter is on
    Public Function getParagraph() As String
        Try

            Dim x As Microsoft.Office.Interop.Word.Paragraphs
            Return x(paragraphnumber).Range.Text


        Catch e As Exception
            MessageBox.Show(e.ToString)
        End Try



    End Function

    'increments paragraph number
    Public Sub nextparagraph()
        paragraphnumber += 1
    End Sub

    'closes all word related processes 
    Public Sub close()
        MSword.Quit(False, System.Reflection.Missing.Value, System.Reflection.Missing.Value)


    End Sub
End Class

if anyone has suggestions that have not been mentioned it would be greatly appreciated.

Public Function getParagraph() As String
        Try
           Return document.Paragraphs(paragraphnumber).Range.Text
        Catch e As Exception
            MessageBox.Show(e.ToString)
        End Try
    End Function
Public Function getParagraph() As String
        Try
           Return document.Paragraphs(paragraphnumber).Range.Text
        Catch e As Exception
            MessageBox.Show(e.ToString)
        End Try
    End Function

Ironically that was the first way I tried it, it brings up the error " the requested member of the collection does not exist"

Call methods in following order:

Dim wd as New WordDocInfo
 wd.nameinfo="file.doc"
 wd.setwordDoc()
 Dim s as string  = ws.getParagraph()

That's the way the methods are called inherently. When declaring a new instance of WordDocInfo it assigns a file name and calls setwordDoc() internally, it is a private method so it can't be called outside of the class. The getparagraph function is then called after these things have been executed. I checked that as well to make sure I wasn't calling things out of order.

I have figured out the problem, and boy do I feel stupid. The index of the paragraphs() array starts at 1, while I was trying to access it at 0 (the place that most arrays start) the entire time. Because I automatically assumed that if there was nothing located at position zero of the array the entire array was empty I never bothered to check to see if the rest of the array had information in it!

This question has already been answered. Start a new discussion instead.