Question : Import Word Doc with Line Numbers into Access

I have large Word documents that are routinely distributed to a several dozen people for comment and review. These documents are always printed with line numbers that are asigned assigned automatically using the line numbers feature in Word 2007 (restarting on each page).

What I would like to do is somehow import the document into an Access table with fields for the page number, line number, and the line text. So for example, if Page 1 of the document read as follows:

1  The quick brown fox jumped
2  over the lazy dogs.

Hopefully I would be able to import this as two separate records and add the page number as well.

Since the comments are always made on hard copies, I have to be careful not to do anything that might alter the Word document so the references remain correct. I have tried the transfertext method by converting the text to a table first, but I can't seem to get word to recognize a soft return to insert a new table row. Plus, the line numbers never get carried over.

I am reasonably comfortable writing code to do something like this, although not an expert by any means.

Thanks!

Answer : Import Word Doc with Line Numbers into Access

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
Public Sub ImportDocWithLineNos()
'Created by Helen Feddema 25-Jun-2010
'Last modified by Helen Feddema 25-Jun-2010

On Error GoTo ErrorHandler

   Dim appWord As Word.Application
   Dim doc As Word.Document
   Dim strDocName As String
   Dim rst As DAO.Recordset
   Dim intLineNo As Integer
   Dim dat As MSForms.DataObject
   Dim intLastLineNo As Integer
   Dim prps As Object
   Dim prp As Object
   
   Set dat = New MSForms.DataObject
   intLineNo = 1
   Set rst = CurrentDb.OpenRecordset("tblDocWithLineNos")
   strDocName = "G:\Documents\ExpertsExchange\Line Number Test.docx"
   Set appWord = GetObject(, "Word.Application")
   Set doc = appWord.Documents.Open(strDocName)
   Set prps = doc.BuiltinDocumentProperties
   intLastLineNo = prps("Number of lines")
   
   doc.Select
   appWord.Selection.HomeKey Unit:=wdStory

   For intLineNo = 1 To intLastLineNo
      With appWord.Selection
         .EndKey Unit:=wdLine, Extend:=wdExtend
         .Copy
         dat.GetFromClipboard
         rst.AddNew
         rst![LineNo] = intLineNo
         rst![LineText] = dat.GetText
         rst.Update
         .MoveRight Unit:=wdCharacter, Count:=1
      End With
   Next intLineNo
   
   rst.Close
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   If Err = 429 Then
      'Word is not running; open Word with CreateObject
      Set appWord = CreateObject("Word.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number _
         & " in ImportDocWithLineNos procedure" _
         & "; Description: " & Err.Description
      Resume ErrorHandlerExit
   End If

End Sub
Random Solutions  
 
programming4us programming4us