Solved

Word VBA  - read a specific line number

Posted on 2011-09-29
2
685 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA Script to return Folder File and Folder count 8 40
WMIC Output Formatting 1 84
Access 2010 7 90
Can I increase Size of Advanced Search Button in Word 2013? 4 47
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 …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

734 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