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.