Solved

Word VBA  - read a specific line number

Posted on 2011-09-29
2
701 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

695 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