Results 1 to 8 of 8

Thread: Reading data from a Word doc ???

  1. #1

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Question Reading data from a Word doc ???

    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 !!!

  2. #2
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Reading data from a Word doc ???

    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...

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  3. #3

  4. #4

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    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!

  5. #5

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    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

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Use the Microsoft Word Object Library.

    VB Code:
    1. Dim objWord As New Word.Application
    2. objWord.Documents.Open "C:\Test1.doc"
    3. objWord.Selection.GoTo What:=wdGoToBookmark, Name:="BookmarkName"
    4. objWord.Selection.MoveDown Unit:=wdLine, Count:=3, Extend:=wdExtend
    5. MsgBox objWord.Selection.Text
    6. Set objWord = Nothing

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Or if you don't have a bookmark to go to you can use the Find method.

    VB Code:
    1. wdApp.Selection.Find.ClearFormatting
    2.     With wdApp.Selection.Find
    3.         .Text = "I want to find this"
    4.         .Replacement.Text = ""
    5.         .Forward = True
    6.         .Wrap = wdFindContinue
    7.         .Format = False
    8.         .MatchCase = False
    9.         .MatchWholeWord = False
    10.         .MatchWildcards = False
    11.         .MatchSoundsLike = False
    12.         .MatchAllWordForms = False
    13.     End With
    14.     wdApp.Selection.Find.Execute
    If 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.

  8. #8

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width