Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: copy from Word to Access

  1. #1

    copy from Word to Access

    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
    Last edited by Aussiebear; 03-26-2023 at 11:13 AM. Reason: Adjusted the code tags

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Ed,

    Welcome to VBAX!

    [uvba]a[/uvba] I've taken the liberty of editing your post for that.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    thanks for the tip and the welcome! I'm much more familiar on Jeep boards

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

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

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Last edited by Aussiebear; 03-26-2023 at 11:14 AM. Reason: Adjusted the code tags
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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)
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    True enough, Tony.

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

    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
    Last edited by Aussiebear; 03-26-2023 at 11:14 AM. Reason: Adjusted the code tags
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  10. #10
    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?

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

  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I get squares as paragraph marks in the textbox. Variety is the spice of life, as they say!

  13. #13
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  14. #14

    I have a habit of uncovering previously unknown issues with everything I touch!


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

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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
    Last edited by Aussiebear; 03-26-2023 at 11:15 AM. Reason: Adjusted the code tags
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    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?

  17. #17
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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)")
    Quote Originally Posted by ejm91206
    .. 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
    Last edited by Aussiebear; 03-26-2023 at 11:16 AM. Reason: Adjusted the code tags
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  18. #18
    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!

  19. #19
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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")
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  20. #20
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Thanks, Ken!

    VBAExpress has an excellent syntax checker
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

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