reading a word file-VBForums
Results 1 to 8 of 8

Thread: reading a word file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    161

    Question reading a word file

    I am using VB6 and MS Office 2003 Pro. I am trying to read a word document into vb6. The whole file. Then I want to look for certain words with the 'instr' function and manipulate the data (not back into word) . This is similar what I do when I read (parse) data from the internet. I can't seem to get the data into a string.

    Any help would be appreciated.

    Thanks

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    3,635

    Re: reading a word file

    Something like this?

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim wrd As Object
        Dim doc As Object
        Dim docText As Object
        Dim sFileSpec As String
        Dim sText As String
        '
        Const wdExtend = 1
        Const wdMove = 0
        Const wdStory = 6
    
        sFileSpec = "C:\Users\Elroy\Desktop\temp.docx"
    
    
        Set wrd = CreateObject("Word.Application")
        Set doc = wrd.Documents.Open(sFileSpec)
        Set docText = doc.ActiveWindow.ActivePane.Selection
    
        docText.HomeKey wdStory, wdMove
        docText.EndKey wdStory, wdExtend
    
        sText = docText.Text
    
        ' Clean up.
        Set docText = Nothing
        doc.Close False
        Set doc = Nothing
        wrd.Quit False
        Set wrd = Nothing
    
    
    
        MsgBox sText
    
        Unload Me
    End Sub
    
    
    EDIT1: Just as an FYI, I do all of my Word automation with late binding. Therefore, it's rather independent on the exact version of Word that might be on the computer. No references needed to make the above code work (other than some version of Word installed on the computer).

    EDIT2: Also, I used a MsgBox to display the text. That'll work fine so long as it's a small document, but I'm not sure what the text limit of a MsgBox is, but I suspect a Word document could easily overflow it.
    Last edited by Elroy; Feb 14th, 2018 at 11:08 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    161

    Re: reading a word file

    I cannot get past the Set wrd and the Set doc statements. The programs displaying a msg that another programs is using a file - not specifying what file. The task manager displays a 'File in use' entry with vb project icon. Set wrd executes, sort of when stepping thru the code. The set doc statement does not even light up.

    Any suggestions?

    Thanks

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    3,635

    Re: reading a word file

    Hi AccessShell,

    I'm going to guess that you terminated my example before it had a chance to execute the "Clean up" code. You've left an invisible copy of Word in memory which has that document open. To clean it up, you'll have to open your Task Manager and kill that copy of Word. Then, things should start working correctly.

    When you automate Word, you've got to be sure to close the document and then quit Word before you end your program.

    Good Luck,
    Elroy

    EDIT1: Also, in my little down-and-dirty example, I made the assumption that the document definitely exists. I assume you patched that part up to point at one of your documents. It's up to you to verify that the document exists before you try and open it with word. Also, if Word can't find it, it's still up to you to make sure Word gets correctly cleaned up from automation. You can't just have your program crashing with an error. All of that is somewhat beyond the scope of your question though.

    EDIT2: Just to elaborate a bit more, that "Set wrd = CreateObject("Word.Application")" line gets a copy of the Word program in memory and gives us access to it. The "Set doc = wrd.Documents.Open(sFileSpec)" line uses our copy of Word to open a file. Now, it's quite easy for us to release our object variables (wrd and doc) while still leaving our copy of Word in memory and possibly leaving our document open. To close the document, you must execute the "doc.Close False" line. And to shut down Word, you must execute the "wrd.Quit False" line.
    Last edited by Elroy; Feb 14th, 2018 at 02:32 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    161

    Re: reading a word file

    How do I delete something that is not visible in the task mgr?

    Also, I don't need the word document to be displayed. I just need to pull in the data.

    P.S. A string can hold a lot. I've used a string to hold the contents of an entire web page to parse.

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    3,635

    Re: reading a word file

    It's visible in the task manager. It's just not visible (by default) on the screen when you're doing automation.

    p.s. Yes, a string can hold up to 2 GB, but I think that would blow up the MsgBox.

    EDIT1: To make Word visible during automation, you could do: wrd.Visible = True
    Do that immediately after you get your wrd object set.

    EDIT2: Yeah, I just tested it. The MsgBox (sort of) blows up at 1023 characters. Actually, it just truncates, showing the first 1023 characters if the string to show is longer:

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
    
    
        Dim i As Long
        Dim s As String
        For i = 1 To 10000
            s = s & Format$(i Mod 10)
        Next i
    
        MsgBox s
    
    End Sub
    
    
    Last edited by Elroy; Feb 14th, 2018 at 02:42 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,266

    Re: reading a word file

    if the word document is already open you may be able to use getobject("fullpath\filename") to automate the open document, without affecting the document in use

    you can also use getobject to find not viisible word instances (showing in task manager), then make them visible or close them
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    161

    Resolved Re: reading a word file

    Ok, so I had a corrupt word file from a test crash before I got to close the connection to the word file. I had to delete the file and recreate it and it worked like a charm. I was using a string rather than a msgbox.

    What is happening, I was writing a program to access the web page and capture the data to parse. Usually I use the winhttp reference and store the data in a string I couldn't do that here because it was protected page. You could not get to the page in code. So I have to manually bring up the page in IE11, bring up the source and copy that into a word file. Then I use the program to parse the data and format it. And yes, I know getting data that way is risky because of the provider changing the web page format.

    Thanks to all who helped.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width