[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Word VBA  - read a specific line number

Posted on 2011-09-29
2
Medium Priority
?
782 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

656 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