How to access word document's content (without opening it)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TravelingCat
    New Member
    • Feb 2010
    • 82

    How to access word document's content (without opening it)

    Hello,
    Say there is a word document saved on my computer. I need to display text from this document in a report,
    without opening it (not that i know how to do it while it's opened, but anyway it shouldn't be).
    So does anyone know how to access word doc's content?
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32636

    #2
    I assume you're just tripping up over your words here as I cannot imagine you expect to access data from any type of file without opening that file. Are you trying to say without opening Word? Or even without opening Word so that it's visible on the desktop?

    Comment

    • TravelingCat
      New Member
      • Feb 2010
      • 82

      #3
      Well i just have no idea how it works, thought maybe it's possible.. is it possible if i open the file first?

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32636

        #4
        You can open the file in Word invisibly (using Application Automation). That's the closest thing I can think of to what you're describing.

        Newer versions of Word (2007 and onwards) store their documents in XML format (.docx, .xlsx, etc) so there may be further scope for parsing the file with other software (as XML is a general standard), but I couldn't help with that direction I'm afraid.

        Comment

        • TravelingCat
          New Member
          • Feb 2010
          • 82

          #5
          Thank you so far, NeoPa
          I have office 2003. Will be glad to hear more ideas about accessing the content of (now opened) word document

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32636

            #6
            I will help where I can, but I should tell you that I am no Word expert in VBA. I use it as an operator from time to time, but haven't coded in it since before it used VBA (Pre-Word 97).

            Have you got anywhere yet with opening the file at least (from the link I posted)?

            Comment

            • TravelingCat
              New Member
              • Feb 2010
              • 82

              #7
              I don't think opening the file would be a problem, since i already do it, for other purposes. I'm more interested in the second part

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32636

                #8
                Originally posted by TC
                TC:
                I don't think opening the file would be a problem, since i already do it, for other purposes.
                That doesn't make a lot of sense to me. Can you explain? Do you use Application Automation elsewhere already?

                Comment

                • TravelingCat
                  New Member
                  • Feb 2010
                  • 82

                  #9
                  This is what happens, at some point user wants to create a text file - i create it (Dim oApp As word.Applicatio n, Dim oWord As word.Document), user types whatever he needs, file is saved. At completely another point, user wants to see report, one of report's contents should be the text from the file earlier. Hope it clears up something

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32636

                    #10
                    It certainly clears up that you are reasonably familiar with Application Automation.

                    What I suggest you do now then, as I am not very (barely at all) familiar with the object structure and the available procedures in Word, is to do a dry run of the basics of what you require (I would guess something like highlight the whole of the current document and possibly copy it to the Clipboard) but with the macro recorder running. This should create for you a very basic VBA procedure within the code of your document (You needn't save this). When you have this code available you would use it as a basis for developing the code within your Access module.

                    Things to bear in mind while doing this are :
                    1. You will want a reference set up to Word in your Access project.
                    2. You may need to be more explicit when dealing with foreign (Word related) items in your Access code. The defaults available in Word are unlikely to be available as defaults when running from within Access. In many cases you need to be more specific.
                    3. Compiling your code, from the point where you expect it basically to work, is always a good idea. It may help you notice more quickly any problems inherent in your code. It may also point you to solutions.


                    I hope all this is of some help. As I say I am not a Word code expert by any means, but hopefully some of these general tips will prove useful.

                    Comment

                    • TravelingCat
                      New Member
                      • Feb 2010
                      • 82

                      #11
                      Thanks for putting time and thought into this, but sorry it doesn't help me that much.. it's too vague and i was hoping to see specific steps that i need to take

                      Comment

                      • Stewart Ross
                        Recognized Expert Moderator Specialist
                        • Feb 2008
                        • 2545

                        #12
                        Hi TravelingCat. In the circumstances what NeoPa has given so far has been a model of clarity, given what little you've told us about your true requirements.

                        You mention a 'text file' but since you are using Word via automation I guess you mean a Word Document.

                        You give no indication of what you mean when you say the 'file is saved'. Is this under a name provided by the user? Is it in a standard location? Is it really a .DOC file or is it text?

                        You mention opening a report. What report is that? Is it a Word document with space for user-inserted content, or what? How would you decide what content to include in your report? Is it by user, by date, by file type, by keyword, or what?

                        Where in your report do you want the new text to go? In general, if you want to place text at a specific location in a word document via automation you need to define one or more bookmarks - named locations - within your Word document, as you won't simply be appending the new text the way that is most straightforward (at the beginning or end of the existing document).

                        These are just some of the questions which I would ask from what you've mentioned so far. I hope you can tell from this that your requirements need to be clearly stated and laid out for us to help you. Without that clarity it is not practicable to hand you a 'do this, then that' solution in response.

                        -Stewart

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32636

                          #13
                          Originally posted by TravelingCat
                          TravelingCat:
                          Thanks for putting time and thought into this, but sorry it doesn't help me that much.. it's too vague and i was hoping to see specific steps that i need to take
                          That's reasonably fair comment. I was never going to be able to give much instruction on Word specifics due to my lack of experience (which I announced when I started).

                          That said, I believe the steps I laid out, while quite general and not doing the job for you, should be enough to help you find what you need. The concept itself is remarkably illuminating. I used it to get myself into Excel coding a number of years ago and haven't looked back since.

                          I can only recommend you give it a try. I doubt you'd be disappointed. What you decide is entirely your choice of course.

                          Comment

                          • TravelingCat
                            New Member
                            • Feb 2010
                            • 82

                            #14
                            Stewart, thank you for replying, but before i answer those questions, i want to say that after me and NeoPa established the word document should indeed be open to do whatever with it, basically all i needed was this one line: something = ActiveDocument. Content (only now found it), which gives me what i wanted. Now my problem is that in order for me to use this text for my purposes user has to finish typing and close the doc. I guess i need to reopen it afer that, but how would i know that he already closed it? I would really like to deal with this within the same sub. Any ideas?..

                            Comment

                            • Stewart Ross
                              Recognized Expert Moderator Specialist
                              • Feb 2008
                              • 2545

                              #15
                              If what you're doing at present is getting you the result you want then by all means keep on doing it. If you have Word running in visible mode ready for user interaction this does complicate things, as you can't be sure what your users will do with the open Word document.

                              Normally, if the Office applications are running as automation servers they are not running in fully interactive mode. The Visible property, for example, is normally set False when called from VBA code running in another application, so the instance concerned won't even be seen by the user (unless the Visible property is set True).

                              Anyhow, given that you've got interaction with the Word instance you'll need to capture the events which Word generates and establish your own event handlers. That way you will be able to react appropriately to your user closing the Word instance which is receiving your text, for instance.

                              You do this by declaring your object variable with events:

                              Code:
                              Dim WithEvents objWord as Word.Application
                              then establishing local event handlers in your VBA application to receive the events concerned. This Microsoft MVP article gives some further details:

                              http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm.

                              Good luck with your investigations.

                              -Stewart

                              Comment

                              Working...