Click to See Complete Forum and Search --> : Reading data from a Word doc ???
NeedSomeAnswers
May 6th, 2004, 09:19 AM
Hello all,
Does anybody know of the best way to read data out of a Word Document into a string in VB??
I need to be able to read in say the top 6 lines of a document
or possibly a range say from lines 2 - 6 something like that!
Oh and it cant be using Bookmarks, as the doc i will be reading have already been created, and haven't got them!
Any help would (as always) be greatly appreciated !!!
Ruku
May 6th, 2004, 09:40 AM
Originally posted by NeedSomeAnswers
Hello all,
Does anybody know of the best way to read data out of a Word Document into a string in VB??
I need to be able to read in say the top 6 lines of a document
or possibly a range say from lines 2 - 6 something like that!
Oh and it cant be using Bookmarks, as the doc i will be reading have already been created, and haven't got them!
Any help would (as always) be greatly appreciated !!!
I think the old word (.doc files) format comming with windows can be read similarily as a rtf... try it, I'm not sure, but I think it's similar... :)
MartinLiss
May 6th, 2004, 09:58 AM
How will you know how many lines you need to read? Is there some indicator in the document?
NeedSomeAnswers
May 6th, 2004, 10:37 AM
Thanks for the prompt replies, will respond in order:
1, Can you read rtf, like txt files ??
2, All the documents are derived from the same template, and so the Lines i need should be in the same place in all the documents!
NeedSomeAnswers
May 6th, 2004, 10:40 AM
Also - afterthought - i am using word XP, although both Word 2000 & XP are deployed where i work so it would need to work with them !
Cheers
Richard
Negative0
May 6th, 2004, 10:52 AM
Use the Microsoft Word Object Library.
Dim objWord As New Word.Application
objWord.Documents.Open "C:\Test1.doc"
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="BookmarkName"
objWord.Selection.MoveDown Unit:=wdLine, Count:=3, Extend:=wdExtend
MsgBox objWord.Selection.Text
Set objWord = Nothing
MartinLiss
May 6th, 2004, 12:22 PM
Or if you don't have a bookmark to go to you can use the Find method.
wdApp.Selection.Find.ClearFormatting
With wdApp.Selection.Find
.Text = "I want to find this"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wdApp.Selection.Find.ExecuteIf either of those was don't work for you then manually open one of the documents, start recording a macro, and do whatever is necessary to get to the lines you want to locate. Once you have them, stop the macro and edit/copy it to see how Word did it.
NeedSomeAnswers
May 6th, 2004, 03:48 PM
Thanks guys,
will try both these option out when i get in to work tommorow !
I have found out that thier are 3 different templates that docs are based on so i might end up using both of these methods for different templates.
I have something actually similar to Negitive0's code, but not Martins.
Cheers
Richard
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.