Hey,
How can I read a word document line by line using automation?
Links to examples would be great.
thanks,
Printable View
Hey,
How can I read a word document line by line using automation?
Links to examples would be great.
thanks,
Here is some pseudo code:
Add a Reference to Word Library, and make the appropriate changes (ie Application will become your Word Object reference)
VB Code:
Sub ReadWord() Dim strArr() As String Dim intCount As Integer Dim intIdx As Integer 'Capture the Word Text ActiveDocument.Select 'Load an Array with each line strArr() = Split(Selection.Text, vbCr) intCount = UBound(strArr) - 1 For intIdx = 0 To intCount MsgBox strArr(intIdx) Next End Sub
Here is a working version that dosn't require a Library Reference:
(Just have the instance of Word open)
VB Code:
Option Explicit 'Note: NO reference to MS Word Object Library x.x is required in this example Private Sub Form_Load() Dim objWord As Object Dim strArr() As String Dim intCount As Integer Dim intIdx As Integer On Error GoTo Err_Handler Set objWord = GetObject(, "Word.Application") 'Capture the Word Text objWord.ActiveDocument.Select 'Load an Array with each line strArr() = Split(objWord.Selection.Text, vbCr) intCount = UBound(strArr) - 1 For intIdx = 0 To intCount MsgBox strArr(intIdx) Next Set objWord = Nothing Exit Sub Err_Handler: If Not (objWord Is Nothing) Then Set objWord = Nothing MsgBox "Number: " & Err.Number & vbCrLf & _ "Description: " & Err.Description, vbOKOnly + vbCritical, "Error!" End Sub
Thanks a lot.
How about going through each page in the document?
Are the pages seperated by Page Breaks?
yes, each page is separated by a page break.
You might be able to get some ideas here:
http://support.microsoft.com/?kbid=293861
I've seen this article. I don't have any section breaks. I only have page breaks in my document. I have no control over the creation of the document, I just have to import the data.