Tuesday, November 11, 2008

Extract text from word doco


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*|pdf files (*.pdf)|*.pdf"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Me.TextBox1.Text = OpenFileDialog1.FileName
'myStream = OpenFileDialog1.OpenFile()
'If Not (myStream Is Nothing) Then
' ' Insert code to read the stream here.
' myStream.Close()
'End If
End If


Dim objWord As Microsoft.Office.Interop.Word.ApplicationClass
Dim strText As String
objWord = New Microsoft.Office.Interop.Word.Application()
With objWord
.Visible = False
.Documents.Open( _
Me.OpenFileDialog1.FileName, _
, _
True _
)
.WordBasic.EditSelectAll()
.WordBasic.SetDocumentVar( _
"MyVar", _
.ActiveDocument.ActiveWindow.Selection.Text _
)
strText = .WordBasic.GetDocumentVar("MyVar")
'TextBox1.Text = strText
TextBox1.Text = _
InsertNewLineChars(Strings.Left(strText, Len(strText) - 1))
.Documents.Close(0)
.Quit()
End With
End Sub

Private Function InsertNewLineChars( _
ByVal strText As String _
) As String
Dim pos As Integer, l As Integer
pos = 1
Do While pos < Len(strText)
l = Strings.InStr(pos, strText, vbCr)
If l = 0 Then Exit Do
strText = _
Strings.Left(strText, l - 1) & vbCrLf & _
Mid(strText, l + 1)
pos = l + 2
Loop
Return strText
End Function

End Class

No comments: