How to Read the MS Word file line by line - urgent

S

SpiderSwamy

Hi plz, tell me how to read MS Word file line by line.
I am able to open the file using the following code.

code:

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objTbl As Word.Table

Set objWord = New Word.Application
objWord.Documents.Add
objWord.Visible = True
Set objDoc = objWord.ActiveDocument
Call objWord.Documents.Open(sFilepath, , True, True)
 
S

SpiderSwamy

This need to done in VB6. anyone who knows it plz, reply it will be
very help ful..
 
C

Charles Kenyon

Hello,

You need to learn Word before you start programming it.
Word files are not line by line. The concept doesn't make sense in the Word
object model.
If the Word file were to be converted to a text file with a CRLF at the end
of each converted line, you would have something you could do this with I
suppose.

What, exactly, are you trying to do? Why?
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
S

SpiderSwamy

Hi,

i am writing a program in VB6 where i need to search for a keyword
line by line i have done it for .txt file.

If you provide me the code from which Word file were converted to a
text file. it would be a great help.

Thanks in Advance
 
J

Jonathan West

SpiderSwamy said:
Hi,

i am writing a program in VB6 where i need to search for a keyword
line by line i have done it for .txt file.

If you provide me the code from which Word file were converted to a
text file. it would be a great help.

Thanks in Advance

The following code will put the whole of the unformatted text of the
document (excluding headers footers and textboxes) into the string variable
strText. You can then do whatever you want with that variable.

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim strText as String

Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Open(sFilepath, , True, True)
strText = objDoc.Content.Text


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

SpiderSwamy

Hi,
in test.doc I have the following text.

User Prabhakar
Optiva search
Test this is for test

using this code :

Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Open(sFilepath, , True, True)
strText = objDoc.Content.Text

strText = "User Prabhakar Optiva search Test this is for test"

then I am using IntSplitArray = Split(strText, , -1, 1) then getting
the following

: IntSplitArray(0) : "User" : String : Form2.Find
: IntSplitArray(1) : "Prabhakar Optiva" : String : Form2.Find
: IntSplitArray(2) : "search Test" : String : Form2.Find
: IntSplitArray(3) : "this" : String : Form2.Find
: IntSplitArray(5) : "for" : String : Form2.Find
: IntSplitArray(6) : "test" : String : Form2.Find

what function can i use to get it like

: IntSplitArray(0) : "User Prabhakar " : String : Form2.Find
: IntSplitArray(1) : "Optiva search" : String : Form2.Find
: IntSplitArray(3) : "Test this is for test" : String : Form2.Find
 
O

Oliver Townshend

in test.doc I have the following text.
User Prabhakar
Optiva search
Test this is for test

Are these separate paragraphs? What if you had a line that word-wrapped?

Is the purpose of the search to say "Hey the word appears on line 6, 5th
word?" Or to act upon the words you find?

Oliver Townshend
 
S

SpiderSwamy

in test.doc I have the following text.
User Prabhakar
Optiva search
Test this is for test

these 3 are lines one next to another not paragraphs..

and when i find "Optiva" i need do display "search" or till end of line
after Optiva keyword.

the code:
IntSplitArray = Split(strText, vbCrLf, -1, 1)

is not making the difference its displaying the content in a single
line.
its giving the following output..

IntSplitArray(0) : "User Prabhakar Optiva search Test this is for test"
 
J

Jonathan West

Word doesn't think in terms of lines - lines will wrap where they will and
are dynamically updated according to the page size, margins, font etc. The
location of these automatic line breaks is not stored in the document.

If you really need to find the location of these automatic line breaks, then
it is possible, but complex and not necessarily reliable.

But I get the impression that you are not talking about automatic line
breaks, but rather of text that has been deliberately split into separate
lines. If this is the case, what we need to discover is the character(s)
that are used to separate the lines. The most likely candidates are the
string represented by the VB constant vbCr, which represents a paragraph
break in Word, or Chr$(11), which represents a manual line break (inserted
by pressing Shift-Enter in Word).

Whichever character is being used is the character you need for the
delimiter in the Split function.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

SpiderSwamy

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim strText As String


Set objWord = New Word.Application
'objWord.Visible = True
Set objDoc = objWord.Documents.Open(sFilepath, , , True)
strText = objDoc.Content.Text
strText = objDoc.Content.Text
readlineword:

objDoc.Close

IntSplitArray = Split(strText, vbCr, -1, 1)

this part of code has workred fine vbCr is the one which is vbCr
Chr(13) Carriage return is used for seperation of lines..

But i am unable to close the WINWORD.EXE which is created by Set
objWord = New Word.Application

I am only able to close the objDoc.Close( i.e. the test.doc file )

do you know how to close the WINWORD.EXE which is created by Set
objWord = New Word.Application.

plz reply because so many WINWORD.EXE are visible in my process( i.e.
in the task manager)


Thanks
Manju
 
J

Jonathan West

SpiderSwamy said:
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim strText As String


Set objWord = New Word.Application
'objWord.Visible = True
Set objDoc = objWord.Documents.Open(sFilepath, , , True)
strText = objDoc.Content.Text
strText = objDoc.Content.Text
readlineword:

objDoc.Close

IntSplitArray = Split(strText, vbCr, -1, 1)

this part of code has workred fine vbCr is the one which is vbCr
Chr(13) Carriage return is used for seperation of lines..

But i am unable to close the WINWORD.EXE which is created by Set
objWord = New Word.Application

I am only able to close the objDoc.Close( i.e. the test.doc file )

do you know how to close the WINWORD.EXE which is created by Set
objWord = New Word.Application.

plz reply because so many WINWORD.EXE are visible in my process( i.e.
in the task manager)

when you are finished with Word, after objDoc.Close, include the following
lines

objWord.Quit SaveChanges:=wdDoNotSaveChanges
Set objWord = Nothing


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

SpiderSwamy

Thank you very much man. The code did the magic i was doing only Set
objWord = Nothing. then i saw your code i was great man, i am very much
thankfull to you Jonathan West. you have understanded my problem and
gave me the solution.
Thanks a lot lot lot lot lot lot lot..................
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top