Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am new in VBScript. i used a VBScript for reading MSWord file word by word. here is the code of my Script.

VB
Option Explicit
REM We use "Option Explicit" to help us check for coding mistakes
REM the Word Application
Dim objWord
REM the path to the Word file
Dim wordPath
REM the document we are currently reading data from
Dim currentDocument
REM the number of Words in the current document
Dim numberOfWords
Dim i
Dim objDialog
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "VBScript Scripts|*.vbs|All Files|*.*"
objDialog.FilterIndex = 1
objDialog.InitialDir = "C:\"
intResult = objDialog.ShowOpen
If intResult = 0 Then
    Wscript.Quit
Else
    Wscript.Echo objDialog.FileName
End If
REM where is the Word file located?
wordPath = "C:\Documents and Settings\sapuser\Desktop\harry.doc"
WScript.Echo "Extract Data from " & wordPath
REM Create an invisible version of Microsoft Word
Set objWord = CreateObject("Word.Application") 
REM don't display any messages about documents needing to be converted
REM from  old Word file formats
objWord.DisplayAlerts = 0
REM open the Word document as read-only
REM open (path, confirmconversions, readonly
objWord.Documents.Open wordPath, false, true
REM Access the document
Set currentDocument = objWord.Documents(1)
REM How many words are in the document
NumberOfWords = currentDocument.words.count               
WScript.Echo "There are " & NumberOfWords & " words " & vbCRLF
For i = 1 to NumberOfWords
	WScript.Echo currentDocument.words(i)
Next
REM Close the document
currentDocument.Close
REM Free memory used to store the document object
Set currentDocument = Nothing
REM exit Microsoft Word
objWord.Quit
Set objWord = Nothing


In this line we count the total number of words in my doc files
NumberOfWords = currentDocument.words.count

and this for loop display the contents of my doc file word by word
For i = 1 to NumberOfWords<br />
	WScript.Echo currentDocument.words(i)<br />
Next<br />


But I want to read my doc file line by line. Is there any function which gets a line rather than a word?
Please suggest me a way to get a line by line display of my doc.

Thanks.
Posted
Updated 18-Oct-11 23:37pm
v2
Comments
Prerak Patel 19-Oct-11 5:37am    
Don't use all CAPS and spend some time to format your question properly.

1 solution

Try using Paragraphs
VB
For Each p In currentDocument.Paragraphs
    WScript.Echo p.Range.Text
Next p
 
Share this answer
 
Comments
Member 8172875 21-Oct-11 4:27am    
thanks for the help.. this code is working fine. now i am getting the contents line by line but one problem is arising because of table.

When i am trying to read table a special character append at the end of the text. for ex..

1


Hemant rana


mca





2


Pritesh sinha


mca






this is a table with 3cols and 2rows

How can i skip these kind of characters?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900