Solved

Word VBA  - read a specific line number

Posted on 2011-09-29
2
630 Views
Last Modified: 2012-05-12
With VBA, I'm trying to read a specific line number in a Word document.  See code snippet below.
What do i need to include to read a specific line number?  For example, how do I change the code to read from line 11 in the Word document and store to variable "strText"

Also need to be able to specify a particular page.  So page 2, line 11.




Set doc = wrd.Application.Documents.Open(strFilePath)
        
            strText = '......code to read all of line 11'
            
            doc.Close

Open in new window

0
Comment
Question by:carsRST
2 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 36816939
try this

 
Function GetLineFromFile(lineno)
Dim fso, result, i, oStream
fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists("C:\Test\test.doc") then
	Set oStream=fso.OpenTextFile("C:\Test\test.doc",1)
	i=0
	While Not oStream.EndOfStream
		strtext=oStream.ReadLine
		if i=lineno then
			result=strtext
		end if
		i+=1
	Wend
	GetLineFromFile = result

End Function

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36817064
Are the lines created by hitting enter at the end. In that case you could use paragraphs,

strText = doc.Paragraphs(11).Range.Text

There is no text line object in Word, because what is displayed on a line depends on so many things - printer, font, etc.

However, this function should return the text for the current environment



Function GetLineNumberText(doc As Document, l As Long) As String
    Dim rng As Range
    Set rng = Selection.Range
    doc.Range(0, 0).Select
    Selection.MoveDown wdLine, l
    Selection.EndKey wdLine, wdExtend
    GetLineNumberText = Selection.Text
    rng.Select 'restore selection
End Function

Open in new window

0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now