PDA

View Full Version : copy from Word to Access



ejm91206
02-22-2005, 03:33 PM
Trying to copy active word document contents to a field in Access w/one button click, using late binding.

I've gotten this far, but it doesn't work. I'm not a programmer, so I can only pick my way thru and pull stuff from other code I've downloaded, so speak down to me please! :)


Dim objWord As Object
Dim objDoc As Object
Set objWord = GetObject(, "Word.Application")
objWord.Visible = True
Set objDoc = objWord.activedocument
objDoc.Selection.HomeKey Unit:=wdStory
objDoc.Selection.Extend
Me!TxDetails.SetFocus Paste

Thanks in advance,

Ed

Ken Puls
02-22-2005, 03:37 PM
Hi Ed,

Welcome to VBAX!

a I've taken the liberty of editing your post for that.:)

ejm91206
02-22-2005, 05:47 PM
thanks for the tip and the welcome! I'm much more familiar on Jeep boards :D

TonyJollans
02-23-2005, 03:11 AM
Hi Ed,

You say "... copy active word document contents ..."

Do you want to run this from Access or Word? And will either or both applications be open? And if Word is open, will the document you want be open or do you want to open it, or do you want to select it from a file list? And what version of Access and Word are you using? Also you seem to want to copy into a control on an open Form - is this correct?

I guess what I'm saying is can you give a bit more detail please?

ejm91206
02-23-2005, 05:55 AM
Hi Tony,

yep, I wanna open a word doc, then go to a form in access, click a button, and have the contents of that doc copied and pasted to a field, via command button on the form.
Reason for this is to minimize the hunt and peck steps needed to get the information of said word docs to the database. The docs are brief text descriptions typed under full page size bitmaps and right now we're doing alot of scrolling and what not.
Personally, I don't mind but the boss man finds it frustrating, so whatever can be done to make it easier I want to try and implement it.

I know the bitmaps won't paste to the field, and I just want the text, so I figure something that copies the entire document to the specified field will do it.

I'm in Access 2003, but the code has to be in late binding for the other users who are on 2000. adding the Word 11 object library makes the database unusable for those folks.

TonyJollans
02-23-2005, 11:47 AM
Hi Ed,

It seems to me you're almost there.

- 1 - You don't need the objWord.Visible line - if you've opened Word manually, it's already visible.

- 2 - You are Selecting the Document, but not copying it. Instead of


objDoc.Selection.HomeKey Unit:=wdStory
objDoc.Selection.Extend

try just using


objDoc.Content.Copy

- 3 - That's really it. Just set your Object variables (objWord and objDoc) to Nothing before you finish.

Ken Puls
02-23-2005, 11:51 AM
Hi guys,

FYI, I tried fooling with this a bit myself, but not being a Word guy at all, I didn't get very far.

One thing I did encounter though, was that it gagged on the wdStory constant on my machine. If it still does, you may want to replace wdStory with 6 (it's numeric equivalent.) In my experience, running between apps with a late bind usually does gag on those (if using Option Explicit anyway) since it sees it as an undefined variable.

HTH,

TonyJollans
02-23-2005, 11:57 AM
It's a lot worse if you're not using Option Explicit, of course - it compiles ok, but doesn't work (unless you're lucky enough to be using a const with a value of 0)

Ken Puls
02-23-2005, 12:07 PM
True enough, Tony.

Always use Option Explicit so you know your errors at design time! :yes

Thanks to Tony's Word code, give this a shot:


Dim objWord As Object
Dim objDoc As Object
Set objWord = GetObject(, "Word.Application")
Set objDoc = objWord.activedocument
Me!txdetails.Value = objDoc.content
Set objDoc = Nothing
Set objWord = Nothing

ejm91206
02-23-2005, 02:49 PM
sweet! thanks! it works!

would I be pushing it it I asked it to not show the little ||| marks left for paragraphs in the page? :D

oh...and where do I mark this solved? Don't see an option in thread tools.

Ken Puls
02-23-2005, 02:58 PM
Interesting... I don't get those at all. Actually, for me the code just seems to ignore paragraphs all together so:

This

Hello
This is a new paragraph
And so is this!

Shows up in the field as this:


HelloThis is a new paragraphAnd so is this!

Although when you copy and paste it, the parapgraph breaks are there.:dunno

mdmackillop
02-23-2005, 03:16 PM
I get squares as paragraph marks in the textbox. Variety is the spice of life, as they say!

Ken Puls
02-23-2005, 03:21 PM
:doh:Nothing like consistency, eh?

I wonder how many other variations we can find. Maybe we should add a poll to this... What do you see? :rofl

ejm91206
02-23-2005, 03:22 PM
:)
I have a habit of uncovering previously unknown issues with everything I touch!


....maybe I should rephrase that....

TonyJollans
02-24-2005, 07:43 AM
I haven't done anything as rash as trying this, but what do you want instead of the little marks or squares or indeed voids that you see or don't see?

Providing you're using at least A2K you could try this:


Me!txdetails.Value = Replace(objDoc.content,vbCR,"(carriage return)")

.. replace the (carriage return) with whatever character(s) you want

ejm91206
02-24-2005, 08:44 AM
that works on the P marks, but there's still a couple hash marks where there were tabs. I tried using the same code to follow with vbTab in place of vbCr but it won't replace both.
Not a big deal, really. Now it's more a lesson in curiosity, and since you fellows are so obliging I figure I can take advantage of the situation and learn!

I'm sure there's also a way to do this without actually opening the document?

TonyJollans
02-24-2005, 10:13 AM
Don't know what you tried but there is no reason not to replace both ..


Me!txdetails.Value = Replace(Replace(objDoc.content,vbCR,"(carriage return)"),vbTab,"(tab)")


.. without actually opening the document
Don't push your luck :)

It amuses me when people ask to get data from a file without opening it. Although there are various ways to open files it is impossible to read a file without opening it. In the case of a Word document you really are best using Word - trying to interpret Word format data with another application is damned hard work to say the least. Now, you can do it all in code so as to avoid the manual opening if that's what you want.


Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open FileName:="path\to\file.doc"
Me!txdetails.Value = objDoc.content
objdoc.close
objword.quit
Set objDoc = Nothing
Set objWord = Nothing

ejm91206
02-24-2005, 10:16 AM
LOL I was just curious because someone said it was do-able thru DAO or something.
can't link to the doc because these are patient records and it's a different document each time.
Thanks Tony!

Ken Puls
02-24-2005, 10:39 AM
Hey there,

Just an FYI, I've fixed a small type in Tony's code ;) (just in case you've already copied it)

Set objWord = CreateObject("Word.Application")

TonyJollans
02-24-2005, 11:38 AM
Thanks, Ken!

VBAExpress has an excellent syntax checker :)

Ken Puls
02-24-2005, 11:42 AM
I know! :giggle I always write in the VBE and copy/paste it here, as I NEVER trust myself! :rotlaugh:

Hey Ed, have we got you all fixed up now? If so, you can follow the instructions in my sig line to mark it solved. ;)

If not though, let us know! :yes