By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

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

P: 82
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?
Aug 24 '10 #1
Share this Question
Share on Google+
35 Replies


NeoPa
Expert Mod 15k+
P: 31,489
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?
Aug 24 '10 #2

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

NeoPa
Expert Mod 15k+
P: 31,489
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.
Aug 24 '10 #4

P: 82
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
Aug 24 '10 #5

NeoPa
Expert Mod 15k+
P: 31,489
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)?
Aug 24 '10 #6

P: 82
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
Aug 24 '10 #7

NeoPa
Expert Mod 15k+
P: 31,489
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?
Aug 24 '10 #8

P: 82
This is what happens, at some point user wants to create a text file - i create it (Dim oApp As word.Application, 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
Aug 24 '10 #9

NeoPa
Expert Mod 15k+
P: 31,489
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.
Aug 24 '10 #10

P: 82
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
Aug 25 '10 #11

Expert Mod 2.5K+
P: 2,545
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
Aug 25 '10 #12

NeoPa
Expert Mod 15k+
P: 31,489
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.
Aug 25 '10 #13

P: 82
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?..
Aug 25 '10 #14

Expert Mod 2.5K+
P: 2,545
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:

Expand|Select|Wrap|Line Numbers
  1. 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
Aug 25 '10 #15

NeoPa
Expert Mod 15k+
P: 31,489
I'm frustrated because I know I've done this before with an Excel export, but I cannot find the code (after a fairly extensive search). From memory though, it wasn't too complicated to handle.

First you set up your error handling. On Error Resume Next is fine.
Next put the open code in a loop.
In the line after the open code test the Err object. If the error number matches that for when the file is already opened (run it beforehand with the file already opened and without error handling to find this value) then pop up a message for the operator and continue around the loop. You may also want to give the operator an option to abort at this stage if that's appropriate.
Otherwise, come out of the loop and continue as before.
Aug 25 '10 #16

P: 82
Hi guys, sorry for disappearing. I set down with my experienced colleague and in the process we remembered that my main goal is to keep the original styling of this word document. Meaning the report should display the word doc's text exactly as it appears there - with the bold/underlined/colored. She suggested to create the report in excel - we tried to paste into excel a designed piece of text and excel kept the styling, so i created excel file, but when copied through vba, the style is gone, it's just a plain text.. here's how i did it:
Expand|Select|Wrap|Line Numbers
  1.     ElseIf st("assignType") = 3 Then
  2.         Dim oApp As word.Application
  3.         Set oApp = CreateObject("Word.Application")
  4.         oApp.Documents.Open filePath & "\" & evNum & "_" & st("assignNum") & ".doc", , , , , , , , , , , False
  5.         docContent = oApp.Documents(1).Content
  6.         oApp.Documents(1).Content.MoveEnd
  7.         oApp.Documents(1).Close
  8.         Set oApp = Nothing
  9.         xlApp.Worksheets("Sheet1").Range("G" & lineNum) = docContent
  10.     End If
So i'm just lost, since the whole purpose of displaying this text in report is that it should maintain its style.
I guess i should open a new thread..
Aug 29 '10 #17

NeoPa
Expert Mod 15k+
P: 31,489
I'm afraid your posted code doesn't include the Dim line for docContent. This could be critical.

I would guess though, that your easiest, and most reliable, approach would be to use the clipboard. This can handle Rich Text as we know.
Sep 6 '10 #18

P: 82
Hi, the docContent is out of the code for now. Since then i discovere PasteSpecial, and right now i have this code which pastes the text that i want as it is, but.. as a picture. I of course need it to be pasted as text into cells.
Expand|Select|Wrap|Line Numbers
  1. ElseIf st("assignType") = 3 Then
  2.         Dim oApp As word.Application
  3.         Set oApp = CreateObject("Word.Application")
  4.         oApp.Documents.Open filePath & "\" & evNum & "_" & st("assignNum") & ".doc", , , , , , , , , , , False
  5.         oApp.Documents(1).Content.Copy
  6.         xlApp.ActiveSheet.PasteSpecial Format:="Microsoft Word 8.0 Document Object"
Can you please elaborate on rich text and how will it be helpful for me?
Sep 6 '10 #19

NeoPa
Expert Mod 15k+
P: 31,489
Rich Text (you may also see it as Rich Text Format or RTF) is essentially text with attributes. It's what I would have expected the Copy / Paste to use (It's what it uses on my PC anyway).

Have you tested this manually, or did you go straight to code?
Sep 6 '10 #20

P: 82
I meant how exactly would i use the RTF in my case of copying the text from word to excel (or access), a little direction, if you have a specific idea maybe.
Have i tested what manually?
Sep 6 '10 #21

NeoPa
Expert Mod 15k+
P: 31,489
TravelingCat:
I meant how exactly would i use the RTF in my case of copying the text from word to excel (or access), a little direction, if you have a specific idea maybe.
Have i tested what manually?
Have you tested the Copy/Paste manually?

The point is it should work naturally. I don't know why you're trying to use Paste-Special in this context (It's extremely useful in places - but I wouldn't expect it to be of help in this situation).
Sep 6 '10 #22

P: 82
Yes i tried to copy paste from word to excel manually, i wrote it here in some post, only because it does work we were thinking to go in this direction. Regarding the paste special, i'm just trying to implement whatever i find in the net, but it doesn't mean i know how to do it. That's why i ask here. So if you do have something in mind, please, do share, i will very much appreciate it! You mentioned clipboard, rtf, i'd love to hear how this may solve the problem. Or anything else..
Sep 6 '10 #23

NeoPa
Expert Mod 15k+
P: 31,489
As I mentioned in post #6 I'm no expert in Word, but I know that both Word and Excel have facilities whereby you can record what you do as an operator and it will create (admittedly pretty basic) VBA code for you. I would suggest you explore this route using Copy and Paste as you have checked it and found it to work.

Clipboard and RTF are just covering the same area really. the Clipboard is where items are copied to and pasted from. You copy an item, and it sits on the Clipboard until you decide what you want to do with it. I explained RTF in post #21. It is the format I would expect the data to be held in when Copy/Pasting normally. It is a format that supports transferring not only the textual data itself, but also any associatted formatting of the text.
Sep 6 '10 #24

P: 82
NeoPa, you seem to not understand me. I wasn't asking what rtf and clipboard are, i know what they are. I was asking for help solving my specific problem with them, as only knowing what they are unfortunately doesn't help me a bit. But since you are no expert in word as you say, i'll leave you alone, you've wasted enough time on this as it is, thank you.
Sep 7 '10 #25

NeoPa
Expert Mod 15k+
P: 31,489
I'm sorry I don't seem to be understanding you clearly. I'm afraid I only have your posts to work with, and I felt I responded to them directly. You don't explain why what I suggested doesn't fit in the category of a solution for you. Actually you don't even say if you tried it and if so, what results you got or where you got stuck. Frankly it's quite difficult to work with such sparsity of information, especially in an area of quite limited expertise myself.

At the end of the day though, if you feel there is no scope here for progress, I can only wish you well and hope you manage to find what you're after.
Sep 7 '10 #26

P: 82
Just pointing to rtf was too general for me. I can't tell you whether i tried something or not when i'm not even sure how i'm supposed to try it. Maybe others can take a direction and find a solution, but i can't, my knowledge of this area is practically zero, so i wanted more specific steps. I realize you told me several times that you are no expert in this area, and that's ok, not every question here can get a fully satisfying answer, and still this is a great place for getting help.
Sep 7 '10 #27

NeoPa
Expert Mod 15k+
P: 31,489
I don't want to push this, because if you're done with it then I'm not going to try to change that. I do get the feeling there may be some mis-communication here though, which may benefit from being cleared up (Well, technically it won't benefit, but we may).

My comments about RTF etc were simply my attempts at clarification. They were not intended as instructions for you to follow. That would be pretty obscure and I would expect even fairly experienced programmers to fall at such a hurdle.

My instructions/directions/whatever, were contained in the first paragraph of post #25 and they were about recording yourelf (From Word use Tools / Macro / Record new macro...) as an operator doing what you want your code eventually to do for you. When you have completed this step, use Alt-F11 to open the code window (The VBA IDE window) and review the code that was created for you. With minor modifications (or maybe not even) this code should do the job you require of it.

When you've done this for the Copying part, you'd need to go through the equivalent steps in Excel to handle the Pasting part.

As I say, you needn't feel obliged to continue with this if it seems complicated. I just felt that I had perhaps not explained myself clearly enough earlier and, especially as you're not an experienced programmer, it may help to put it in clearer terms.
Sep 7 '10 #28

TheSmileyCoder
Expert Mod 100+
P: 2,321
In access 2003, you cannot copy data from word into Access and retain the formatting.
In Access 2007 (and probably 2010) you can have Rich Text fields, but whether they will retain word formatting when being copied into Access I can't tell.

May I ask why you do not just print the word doc?
Sep 7 '10 #29

P: 82
NeoPa, thank you for the effort:) Frankly i'm just a little tired of this task and i wish i didn't have to do it.. About the macro, this time you indeed clarified yourself and i will check it up, but i have no experience with macros as well.
TheSmileyOne, and what about copying data from Word to Excel and retain the format? Do you know if THAT is possible? (access 2003)
Sure, because the final goal is to issue a report, which contains all kinds of data from access, and this text in its original format will be only a part of it. Since we came to a conclusion it's impossible in access, the idea arose to dynamically create a report in excel (identical to its view in access). What i need now is the same as before, somehow keep text's original format when copying it, only now in excel.
Sep 7 '10 #30

TheSmileyCoder
Expert Mod 100+
P: 2,321
@TravelingCat, I have not tried such a thing, but im fairly sure some things are possible. A simple test of manually copying some formatted text over shows that some things such as text highlighting did not get transferred into excel.

Now I don't know what information it is you store in these word files, but you should be aware that I foresee alot of potential problems in copying this information.
1) What if users add a header/footer to word?
2) What if they add a table?
3) What if they add an image?
4) What if they add a comment?
All of those things im 99.9% sure would get f***ed (sorry) up in a copy.

Also Excel is NOT a text editor. For instance some limitations you need to be aware of if some of your text is large:
5) Excel can only handle cell heights up to around 546 pixels (im not 100% sure its pixels, but there IS a limit)
6) Word wrap in Excel only works on the first 1024 charecters on a cell. After that the line which contains the 1025th charecter is not wordwrapped. It looks somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. This is word wrapped
  2. This is word wrapped
  3. This is word wrapped
  4. This is word wrapped
  5. Suddenly word wrap stops working because this very long line contains the cells's 1025th charecter

I hope im not shooting down your motivation, I just want you to be informed of possible problems. Could you maybe post a screenshot of roughly how you want your report to look, then maybe I can make an access suggestion that would work for you. Specifally if possible an examle of the word documents you need incorporated into the report.
Sep 7 '10 #31

NeoPa
Expert Mod 15k+
P: 31,489
Smiley:
In access 2003, you cannot copy data from word into Access and retain the formatting.
I tried a pretty basic test changing the colour, the size and setting the text to Bold, and it seemed to pick up all of these attributes when I pasted it into Excel 2003 (from Word 2003). I tried it with a whole paragraph, and then just a word. Both worked fine as far as I could see.

I don't want to put you off by any means, I just thought I'd comment on that as I was almost certain it should work.

** Edit **
Sorry. I didn't notice it was Access that was brought into the conversation. I read what I expected instead of what was in front of me. Ooops.
My comments above relate to Word and Excel specifically of course.
Sep 7 '10 #32

P: 82
Thank you a lot for your response, i really didn't think about all possible scenarios such as header/footer, /tables.. Excel wasn't even my idea, but since i had no idea of my own cause i don't know much - here we are. We did make a test of copy paste manually, also saw what you saw (highlighting), but it did keep the rest of the style.
I attached the screenshot of my report which is currently in access. It's not in english, but i guess that's not important. Now you see under the purple line there are 3 types, the first is decision (underlined and bold - "executive decision"), the second one in the table is assignment, and the third is text. Under the underlined "Text" header supposed to be the content of that word document we are talking about (for now i'm just putting therethe word "text").
I will be really glad to hear your ideas
NeoPa, i think theSmileyOne did talk about copying from word to excel and indeed the text highlighting is not transferred; other styles that you mentioned, are.
Attached Images
File Type: jpg sampleReport.jpg (40.6 KB, 742 views)
Sep 7 '10 #33

P: 82
Any news?..
Sep 12 '10 #34

NeoPa
Expert Mod 15k+
P: 31,489
I don't believe I have much more I can add I'm afraid TC.

Text-highlighting is not so much an attribute of the text as such, but more an indicator of the status within the application of where the selected text is. It's essentially application context rather than data property. I hope that makes sense. Otherwise, I have nothing further to add to this discussion as it's beyond my experience.
Sep 13 '10 #35

P: 82
Thanks, i was just hoping TheSmileyOne would have some idea after i posted the report
Sep 13 '10 #36

Post your reply

Sign in to post your reply or Sign up for a free account.