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
|